1 / 18

Popping Items Off a Stack Lesson xx

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.

anthea
Download Presentation

Popping Items Off a Stack Lesson xx

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Popping Items Off a StackLesson xx

  2. Objectives • Building a truly dynamic stack • Popping items off a stack in main ( )

  3. 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.

  4. Popping Diagram head 55 44 33 22 11 0 head 44 33 22 11 0 55 x

  5. Steps to Pop 1st Node 3 2 head 55 44 33 22 11 0 head 44 33 22 11 0 4 1 55 x

  6. 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.

  7. 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

  8. 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; }

  9. Program Analysis & Variable Declarations • char str[15]; • entry* head = 0; str head 0

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. Summary • Building a truly dynamic stack • Popping items off a stack in main ( )

More Related