stack lesson xx

19
Stack Lesson xx

Upload: shelly

Post on 07-Jan-2016

24 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Slide 1

Stack Lesson xx

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

1ObjectivesDifference between a queue and a stackBuilding a stackPushing items onto a stackLIFO vs FIFOStack variation

Our goal in this presentation is to show you the difference between a queue and a stack. Then, welll write code to build a stack and push items onto the stack. Finally well look at a different way of building a stack. 2Queue vs. StackHeadHeadabcd e0Node added at the end abcd 0eNode added at the beg. queuestack

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.3Program 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 (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; }

(Part 1)

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.

4Program 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 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.7Calling stackAdd( ) Function head = stackAdd(head, temp);

entry* stackAdd(entry* h, entry* t)

76ctemp0head11(76c)76c 0 ht

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.8 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 } 76ctemp0head110(76c)76c 76c ht0 76c ht76ctemp0head11(76c)

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. Its the blue 0 in the picture on the right.H=t; puts the address thats in t in the variable called h. in our example, 76c is stored in h. The blue items represent the happenings in the function.9 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 } 76ctemp76chead110(76c)76ctemp0head110(76c)76c 76c ht

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 right10 2nd iteration of the for loopfor (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); }

76ctemp76chead110(76c)99btemp76chead110(76c)22(99b)

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

99btemp76chead110(76c)22(99b)76c h99b t

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. 12 After Executing First 2 Lines of the 2nd Call to stackAdd () Functionentry* 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 }

99btemp76chead110(76c)2276c(99b)99b h99b t

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. Well redraw everything for you in the next slide.

13 After Executing First 2 Lines of the 2nd Call to stackAdd () Functionentry* 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 }

99btemp99bhead110(76c)2276c(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.

14End of the Loop5544332211 0head

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.15Printing the Linked List5544332211 0headtemp // traverse the list for (entry* p = head; p; p = p->next) { cout n;

for (int i = 0; i < n; i++) { temp = new entry; cin >> temp->value; head = stackAdd(head, temp); }

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

18SummaryDifference between a queue and a stackBuilding a stackPushing items onto a stackLIFO vs FIFOStack variation

We learned about the following topics:Difference between a queue and a stackBuilding a stackPushing items onto a stackLIFO vs FIFOStack variation

19