Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.

Similar presentations


Presentation on theme: "Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack."— Presentation transcript:

1 Stack Lesson xx This module shows you the basic elements of a type of linked list called a stack.

2 Objectives Difference between a queue and a stack Building a stack
Pushing items onto a stack LIFO vs FIFO Stack variation Our goal in this presentation is to show you the difference between a queue and a stack. Then, wel’ll write code to build a stack and push items onto the stack. Finally we’ll look at a different way of building a stack.

3 Queue vs. Stack queue stack Head Head a b c d e Node added at the end
Node added at the end queue Head a b c d e Node added at the beg. In the previous module, we built a linked list called a queue. In a queue, you add nodes at the end of the list. In the top illustration node e is added at the end of the list. In a stack, nodes are added at the beginning of the list as in the bottom picture. You can see that node e is added before node a. stack

4 Program to Create a Stack
(Part 1) 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 (int i = 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; } Here is the complete stack program. The first part of the program builds a stack that contains 5 nodes. At the end, we call our printList() function to print out the stack.

5 Program to Create a Stack
(Part 2) // 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 head temp struct entry { int value; entry* next; };
int main() {   entry* temp, * head = 0; // initially empty list head In the 1st part of our program, we declare the structure of each node. Each node contains an integer and a pointer to another node that looks like itself. Entry * temp means that we want to set up a pointer to a structure called entry. Entry * head = 0 means that we are going to set up another pointer called head and it is initialized to Null to indicate that our linked list is empty. temp

7 Loop for Input and Creating Additional Nodes
   for (int i = 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 temp head 11 (76c) Here is the code that sets up a for loop to execute 5 times. In the body of the loop, we create additional nodes and read in input . Temp = new entry; allocates a node from the heap and makes temp point to it. After we execute the statement: cin >> temp->value; we will have the following picture: temp will be pointing to a newly allocated node. If the user enter the #11, temp->value will contain the # 11.

8 Calling stackAdd( ) Function
       head = stackAdd(head, temp); entry* stackAdd(entry* h, entry* t) h (76c) head 11 The last statement of the loop is: head = stackAdd (head, temp); calls the stackAdd function which adds the newly allocated node to the linked list. We pass in head and temp and they go into h and t respectively. The red items indicate the happenings in the function. 76c 76c temp t

9 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 76c temp head 11 (76c) h t (76c) head We have redrawn the picture from the previous slide on the left for you so you can see how the memory is set up upon entry into the stackAdd function. We pass in head and temp and they go into h and t respectively. The first statement in the function is t->next = h; this takes the null from h and puts it in t->next. It’s the blue 0 in the picture on the right. H=t; puts the address that’s in t in the variable called h. in our example, 76c is stored in h. The blue items represent the happenings in the function. 11 76c 76c temp t

10 Returning a pointer from a 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 temp head 11 (76c) h t (76c) head After the computer has executed the statement h = t; you will have the picture on the left. H will point to the node that we have added. However, H and t are local variables and they are destroyed when you exit the function. If you return back to main at this point, head will still have a null in it. Head needs to be pointing the first node. The statement: return h; will send the pointer in h back to head and you will have the picture on the right 11 76c 76c temp

11 2nd iteration of the for loop
for (int i = 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 temp head 11 (76c) (76c) head 11 When we return from the function head will be pointing to the first node and you get the picture on the left. When we execute the loop the 2nd time you will have the picture on the right. Head will be pointing to the first node and temp will be pointing to the second node to be inserted into the list. 76c (99b) 99b 22 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 } 76c h 99b temp 76c head 11 (76c) 22 (99b) When we enter the stackAdd function the second time. We will have this picture: t is pointing to the 2nd node to be inserted and h is pointing the current head of the list. t 99b

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 } 99b h 99b temp 76c head 11 (76c) 22 (99b) In the second call to the stackAdd() function, after we have executed the first 2 lines, we will have this picture: t->next will point to the old head of the list and h will point to the node we want to push on top of the stack. This looks like a mess. We’ll redraw everything for you in the next slide. t 99b

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 } 22 76c (99b) 11 (76c) head 99b After we execute the statement: return h; the address in h, in our case 99b is returned back to head in main. Local variables disappear when we exit the function. Here is the picture of our stack redrawn when we exit the stackAdd() function the 2nd time. head is pointing to the 2nd node and the 2nd node is pointing to the 1st node. The items in red, represent the operations that were performed in the function. 99b temp

15 End of the Loop head 55 44 33 22 When we finish the for loop in main(), our stack will will have 5 nodes. Notice that the first number that we entered is at the end of the list. The 2nd number is in the next to the last node. We call this pushing numbers on the stack. head is pointing to the node that contains the last item of input.

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 Instead of using a while loop to traverse the list, we’ll use a for loop so we can show you another method. For entry * p = head means that we are setting up a pointer called p and making it point to the same node that pointed to by head. We’ll use p to traverse the list. The second argument of the for statement is p, this means that while p does not contain a null, we are to continue in the loop. The 3rd argument of the for loop is: P = p->next; makes p point to the next node in the linked list. p temp

17 Output node address: 5ae value 55 next 88b
node address: 88b value next 4af node address: 4af value next d node address: 57d value next c node address: 76c value next 0 5ae head (5ae) (88b) (4af) (57d) (76c) 55 If the stack is built according to our drawing, the computer will print output as shown. If our input was entered in the order 11,22, 33, 44, and 55 you can see the output of the nodes will be in the reverse order. In a stack, items are added at the beginning of the list. We call the process “LIFO”. In a queue, we have “FIFO”, items are added at the end of the list. 88b 44 4af 33 57d 22 76c 11 4af temp

18 Variations of a Linked List
  for (int i = 0; i < 5; i++)   {     temp = new entry;     cin >> temp->value;     head = stackAdd(head, temp);   } int n; cout << “How many nodes ? “; cin >> n;   for (int i = 0; i < n; i++)   {     temp = new entry;     cin >> temp->value;     head = stackAdd(head, temp);   } Let’s rewrite this program to make it truly dynamic. Our original code that creates 5 nodes is listed in the box on the left. We have a for loop that runs exactly 5 times. We can modify our code a bit, ask the user for the number of nodes, read the # into n and now our program will create a stack any size the user desires because our for loop runs from 0 to n.

19 Summary Difference between a queue and a stack Building a stack
Pushing items onto a stack LIFO vs FIFO Stack variation We learned about the following topics: Difference between a queue and a stack Building a stack Pushing items onto a stack LIFO vs FIFO Stack variation


Download ppt "Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack."

Similar presentations


Ads by Google