1 / 19

Stack Lesson xx

Stack Lesson xx. Objectives. Difference between a queue and a stack Building a stack Pushing items onto a stack LIFO vs FIFO Stack variation. Queue vs. Stack. e. 0. Head. Node added at the end. queue. a. b. c. d. Head. e. a. b. c. d 0. Node added at the beg.

shelly
Download Presentation

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. Stack Lesson xx

  2. Objectives • Difference between a queue and a stack • Building a stack • Pushing items onto a stack • LIFO vs FIFO • Stack variation

  3. Queue vs. Stack e 0 Head Node added at the end queue a b c d Head e a b c d 0 Node added at the beg. stack

  4. (Part 1) Program to Create a Stack struct entry { int value;   entry* next; }; entry* stackAdd(entry*, entry*); // prototype void printList(const entry*); // prototype int main() { entry* temp, * head = 0; // initially empty list   for (inti = 0; i < 5; i++) //loop to create 5 nodes   {     temp = new entry; // dynamic memory allocation cin >> temp->value; // initialize data value     head = stackAdd(head, temp);   } printList(head);   return 0; }

  5. (Part 2) Program to Create a Stack // function to add a new node to the head of a linked list entry* stackAdd(entry* h, entry* t) {   t->next = h; // put new node at front of list   h = t; // denote new node as new head of list   return h; // return new head's address } void printList(const entry* h) // prints the list {   for (const entry* p = h; p; p = p->next)   { cout << "node address: " << p       << " value " << p->value       << " next " << p->next << endl;   } }

  6. Starting the Stack struct entry { int value;   entry* next; }; int main() {   entry* temp, * head = 0; // initially empty list temp head 0

  7. Loop for Input and Creating Additional Nodes    for (inti = 0; i < 5; i++) //loop to create 5 nodes   {     temp = new entry; // dynamic memory allocation cin >> temp->value; // initialize data value     head = stackAdd(head, temp);   } (76c) 76c 11 temp head 0

  8. Calling stackAdd( ) Function        head = stackAdd(head, temp); entry* stackAdd(entry* h, entry* t) h 0 (76c) head 11 0 76c 76c temp t

  9. entry* stackAdd(entry* h, entry* t) {   t->next = h; // put new node at front of list   h = t; // denote new node as new head of list   return h; // return new head's address } stackAdd( ) Function h h 0 76c (76c) (76c) head head 11 11 0 0 0 76c 76c 76c 76c t temp temp t

  10. entry* stackAdd(entry* h, entry* t) {   t->next = h; // put new node at front of list   h = t; // denote new node as new head of list   return h; // return new head's address } Returning a pointer from a Function h 76c (76c) (76c) head head 11 0 11 0 76c 0 76c 76c 76c temp temp t

  11. 2nd iteration of the for loop for (inti = 0; i < 5; i++) //loop to create 5 nodes   {     temp = new entry; // dynamic memory allocation cin >> temp->value; // initialize data value     head = stackAdd(head, temp);   } (76c) (76c) head head 11 0 11 0 76c 76c (99b) 76c 22 99b temp temp

  12. 2nd Call to Function stackAdd( ) entry* stackAdd(entry* h, entry* t) {   t->next = h; // put new node at front of list   h = t; // denote new node as new head of list   return h; // return new head's address } h 76c (76c) head 11 0 76c (99b) 22 99b t 99b temp

  13. After Executing First 2 Lines of the 2nd Call to stackAdd () Function entry* stackAdd(entry* h, entry* t) {   t->next = h; // put new node at front of list   h = t; // denote new node as new head of list   return h; // return new head's address } h 99b (76c) head 11 0 76c (99b) 22 76c 99b t 99b temp

  14. After Executing First 2 Lines of the 2nd Call to stackAdd () Function entry* stackAdd(entry* h, entry* t) {   t->next = h; // put new node at front of list   h = t; // denote new node as new head of list   return h; // return new head's address } (76c) head 11 0 99b (99b) 22 76c 99b temp

  15. End of the Loop head 55 44 33 22 11 0

  16. Printing the Linked List   // traverse the list for (entry* p = head; p; p = p->next)   { cout << "node address: " << p       << " value " << p->value       << " next " << p->next << endl;   } head 55 44 33 22 11 0 p temp

  17. Output node address: 5ae value 55 next 88b node address: 88b value 44 next 4af node address: 4af value 33 next 57d node address: 57d value 22 next 76c node address: 76c value 11 next 0 (5ae) (57d) (88b) (4af) (76c) 22 76c 11 0 44 4af 55 88b 33 57d head 5ae 4af temp

  18. Variations of a Linked List   for (inti = 0; i < 5; i++)   {     temp = new entry; cin >> temp->value;     head = stackAdd(head, temp);   } int n; cout << “How many nodes ? “; cin >> n;   for (inti = 0; i < n; i++)   {     temp = new entry; cin >> temp->value;     head = stackAdd(head, temp);   }

  19. Summary Difference between a queue and a stack Building a stack Pushing items onto a stack LIFO vs FIFO Stack variation

More Related