Popping

mangledcobwebΛογισμικό & κατασκευή λογ/κού

14 Δεκ 2013 (πριν από 3 χρόνια και 5 μήνες)

57 εμφανίσεις


Building a truly dynamic stack



Popping items off a stack in main ( )





1.
Popping means we take the contents of the
first node and store it somewhere for
processing.


2.
When you pop something off the stack, it
implies that the 1
st

node is to be deleted.


3.
The second node is then the new head of the
list.

55

44

33

22

11 0

head

44

33

22

11 0

head

55

x

55

44

33

22

11 0

head

44

33

22

11 0

head

55

x

1

2

3

4

Write a program that will build a
stack. When the user enters a
number, create a node and push it
onto the top of the stack. The user
may enter as many numbers as they
wish. The user signifies the end of
input by typing the letter ‘s’ for stop.


After the stack is built pop the first
item of the stack into x.

#include <
iostream
>

using std::
cin
;

using std::
cout
;

using std::
endl
;

#include <
cstdlib
>


struct

entry

{



int

value;



entry* next;

};


void
printList
(const entry*); // prototype

int

main()

{



char
str
[15];



entry* head = 0; // initially empty list




while(1) // create nodes



{



cout

<< "enter a value (s = stop) “;



cin

>>
str
;



if (
str
[0] == 's')



break;



entry* temp = new entry; // dynamic memory allocation



temp
-
>value =
atoi
(
str
);



temp
-
>next = head; // put new node at front of list



head = temp; // denote new node as new head of list



}




printList
(head); // print list




// remove the head of the list (popping)


int

x = head
-
>value;

entry* n = head
-
>next; // get pointer to new head

delete head; // delete old head

head

= n; // assign new head




cout

<<
endl
;

printList
(head); // print after head replaced

return 0;

}


void
printList
(const entry* h)

{



for (const entry* p = h; p; p = p
-
>next)



cout

<< p
-
>value <<
endl
;

}


char
str
[15];



entry* head = 0;

0

head

str

while (1)



{



cout

<< "enter a value (s = stop) “;



cin

>>
str
;



if (
str
[0] == 's')



break;

0

head

‘1’

‘1’


\
0’

str

[0]

[1]

[2]


entry* temp = new entry; // dynamic memory allocation


temp
-
>value =
atoi
(
str
);


temp
-
>next = head; // put new node at front of list


head = temp; // denote new node as new head of list

‘1’

‘1’


\
0’

str

[0]

[1]

[2]

76c

temp

0

head

11

(76c)


entry* temp = new entry; // dynamic memory allocation

temp
-
>value =
atoi
(
str
);


temp
-
>next = head; // put new node at front of list


head = temp; // denote new node as new head of list

‘1’

‘1’


\
0’

str

[0]

[1]

[2]

76c

temp

76c

head

11

0

(76c)



while(1) // create nodes



{



cout

<< "enter a value (s = stop) “;



cin

>>
str
;



if (
str
[0] == 's')



break;



entry* temp = new entry; // dynamic memory allocation



temp
-
>value =
atoi
(
str
);



temp
-
>next = head; // put new node at front of list



head = temp; // denote new node as new head of list



}




(99b)

11

0

(76c)

99b

temp

99b

head

22

76c

‘2’

‘2’


\
0’

str

[0]

[1]

[2]

f3c

head

(76c)

2fe

55

44

(f3c)

4af

88b

33

(4af)

76c

22

0

11

(2fe)

(88b)

‘s’


\
0’

str

[0]

[1]

[2]

f3c

head

(76c)

2fe

55

44

(f3c)

4af

88b

33

(4af)

76c

22

0

11

node address: f3c value 55 next 2fe

node address: 2fe value 44 next 4af

node address: 4af value 33 next 88b

node address: 88b value 22 next 76c

node address: 76c value 11 next 0

(2fe)

(88b)

(76c)

2fe

55

44

(f3c)

4af

88b

33

(4af)

76c

22

0

11

int

x = head
-
>value; //
(1)


entry* n = head
-
>next; // get pointer to new head
(2)

delete head; // delete old head
(3)


head = n; // assign new head

(4)




f3c

head

(2fe)

(88b)

55

x

1

2fe

n

2

3

(76c)

44

4af

88b

33

(4af)

76c

22

0

11

int

x = head
-
>value; //
(1)


entry* n = head
-
>next; // get pointer to new head
(2)

delete head; // delete old head
(3)


head = n; // assign new head

(4)




2fe

head

(2fe)

(88b)

55

x

2fe

n





Building a truly dynamic stack



Popping items off a stack in main ( )