200 likes | 337 Views
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.
E N D
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. stack
(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; }
(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; } }
Starting the Stack struct entry { int value; entry* next; }; int main() { entry* temp, * head = 0; // initially empty list temp head 0
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
Calling stackAdd( ) Function head = stackAdd(head, temp); entry* stackAdd(entry* h, entry* t) h 0 (76c) head 11 0 76c 76c temp t
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
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
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
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
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
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
End of the Loop head 55 44 33 22 11 0
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
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
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); }
Summary Difference between a queue and a stack Building a stack Pushing items onto a stack LIFO vs FIFO Stack variation