# Popping

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

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

80 εμφανίσεις

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

44

33

22

11 0

55

x

55

44

33

22

11 0

44

33

22

11 0

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

// remove the head of the list (popping)

int

-
>value;

-
>next; // get pointer to new head

= n; // assign new head

cout

<<
endl
;

printList

return 0;

}

void
printList
(const entry* h)

{

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

cout

<< p
-
>value <<
endl
;

}

char
str
[15];

0

str

while (1)

{

cout

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

cin

>>
str
;

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

break;

0

‘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

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

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

22

76c

‘2’

‘2’

\
0’

str

[0]

[1]

[2]

f3c

(76c)

2fe

55

44

(f3c)

4af

88b

33

(4af)

76c

22

0

11

(2fe)

(88b)

‘s’

\
0’

str

[0]

[1]

[2]

f3c

(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

-
>value; //
(1)

-
>next; // get pointer to new head
(2)

(3)

(4)

f3c

(2fe)

(88b)

55

x

1

2fe

n

2

3

(76c)

44

4af

88b

33

(4af)

76c

22

0

11

int

-
>value; //
(1)

-
>next; // get pointer to new head
(2)

(3)

(4)

2fe

(2fe)

(88b)

55

x

2fe

n

Building a truly dynamic stack

Popping items off a stack in main ( )