180 likes | 303 Views
Popping Items Off a Stack Lesson xx. Objectives. Building a truly dynamic stack Popping items off a stack in main ( ). Definition of Pop. Popping means we take the contents of the first node and store it somewhere for processing.
E N D
Objectives • Building a truly dynamic stack • Popping items off a stack in main ( )
Definition of Pop • Popping means we take the contents of the first node and store it somewhere for processing. • When you pop something off the stack, it implies that the 1st node is to be deleted. • The second node is then the new head of the list.
Popping Diagram head 55 44 33 22 11 0 head 44 33 22 11 0 55 x
Steps to Pop 1st Node 3 2 head 55 44 33 22 11 0 head 44 33 22 11 0 4 1 55 x
Program Definition 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.
Listing of Popping ProgramPart 1 #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
Listing of Popping Program Part 2 • // 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; }
Program Analysis & Variable Declarations • char str[15]; • entry* head = 0; str head 0
1st Input • while (1) { cout << "enter a value (s = stop) “; cin >> str; if (str[0] == 's') break; str ‘1’ ‘1’ ‘\0’ [0] [1] [2] head 0
1st Node of Stack • 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 76c (76c) str 11 temp ‘1’ ‘1’ ‘\0’ [0] [1] [2] head 0
1st Node of Stack • 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 76c (76c) str 11 0 temp ‘1’ ‘1’ ‘\0’ [0] [1] [2] head 76c
After Executing while loop 2nd Time 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 } str (99b) head 22 76c 99b ‘2’ ‘2’ ‘\0’ [0] [1] [2] (76c) 11 0 99b temp
After User Enters 5 Items and Wants to Quit str (76c) (f3c) (2fe) (4af) (88b) ‘s’ ‘\0’ [0] [1] [2] 22 76c 11 0 44 4af 55 2fe 33 88b head f3c
Output 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 (76c) (f3c) (2fe) (4af) (88b) 22 76c 11 0 44 4af 55 2fe 33 88b head f3c
Code for Popping 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) 2 (76c) (f3c) (2fe) (4af) (88b) 22 76c 11 0 44 4af 55 2fe 33 88b 3 head n f3c 2fe 1 55 x
Code for Popping 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) head 2fe (76c) (2fe) (4af) (88b) 22 76c 11 0 44 4af 33 88b n 2fe 55 x
Summary • Building a truly dynamic stack • Popping items off a stack in main ( )