Presentation is loading. Please wait.

Presentation is loading. Please wait.

Popping Items Off a Stack Lesson xx

Similar presentations


Presentation on theme: "Popping Items Off a Stack Lesson xx"— Presentation transcript:

1 Popping Items Off a Stack Lesson xx
 In the last module, we learned how to build a stack. This module is about popping items off the stack

2 Objectives Building a truly dynamic stack
Popping items off a stack in main ( ) Our goal in this presentation is to build a truly dynamic stack and show you how to pop an item off the 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. 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 head x 55 44 33 22 11 0 44 33 22 11 0 55
head 44 33 22 The top diagram is a stack. If we want to pop the 1st node into x, we need to land up with the 2nd diagram where head points to the original 2nd node. The variable x should contain 55 and the 1st node is released back to the heap. 55 x

5 Steps to Pop 1st Node head head x 3 2 55 44 33 22 11 0 44 33 22 11 0 4
head 44 33 22 Here is the procedure for popping the 1st node into x. Step 1) put the contents of the 1st node into x. Step 2) Set up a pointer to the 2nd node. Step 3) Delete the node that head is pointing to Step 4) Make head point to the original 2nd node. 4 55 x 1

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. Now, let’s write a program to build a stack.

7 Listing of Popping Program Part 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 This is the code for the 1st part of the program which builds the stack. The while (1) loop allows the user to create a stack that is any size. When the user presses the ‘s’ key, input is terminated.

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; } This is the 2nd part of the code to pop the 1st node into x. Afterwards, we print the stack to verify that the 1st node was popped off.

9 Program Analysis & Variable Declarations
char str[15];   entry* head = 0; str head Let’s think about how we are going to do this problem. In a stack, you always need a pointer to the beginning of the list, therefore, we have the statement: entry *head = 0; Our input is going to be numbers except that the user will type in the letter ‘s’ to stop entering numbers. We cannot read a character into an integer variable but, we can read a number into a character location. The statement: char str [15]; sets up a character array where we can store our input. More on this topic later.

10 1st Input while (1)   {     cout << "enter a value (s = stop) “;     cin >> str;     if (str[0] == 's')       break; str ‘1’ ‘\0’ [0] [1] [2] The while (1) loop is used to continually read in input and push items onto the stack. The 1st time we enter the loop, we prompt the user for input and read it into the character array str. If the user enters the # 11, the character 1 will be in str[0] and str [1]. str[2] will have a null. We’ll convert the characters into a number in a moment. Before we do that, we use the if statement to see if the user typed in the letter ‘s’. If they did, it will be stored in str[0] and we break out of the while (1) loop. head

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 ‘1’ ‘\0’ str [0] [1] [2] 76c temp head 11 (76c) If the user didn’t typed in the letter ‘s’ we’ll land up at this piece of code. Entry * temp = new entry; creates a new node and makes temp point to it. Temp->value = atoi (str); takes the string of characters in str and converts them into an integer which is stored in temp->value. Atoi stands for ascii to integer. In the next slide, we’ll demonstrate the last two lines of this code.

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 ‘1’ ‘\0’ str [0] [1] [2] 76c temp head 11 (76c) Temp->next = head; puts the null that was in head into temp->next. Head = temp; makes head point to the newly allocated node. This is the picture you will get after the user has entered the first item of input. We have pushed the first item onto the stack.

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   } ‘2’ ‘\0’ str [0] [1] [2] (99b) 11 (76c) 99b temp head 22 76c If we execute the while(1) loop the 2nd time and the user enters the # 22, we will get this picture. Head will point to the 2nd node, the 2nd node will point to the first node.

14 After User Enters 5 Items and Wants to Quit
‘\0’ str [0] [1] [2] f3c head (76c) 2fe 55 44 (f3c) 4af 88b 33 (4af) 76c 22 11 (2fe) (88b) After the user has entered 5 items of input, our stack will look like the picture drawn. In order to terminate input, the user enters the letter ‘s’ and it will be stored in str[0]. Since str[0] is equal to ‘s’, we exit the while (1) loop.

15 Output node address: f3c value 55 next 2fe
node address: 2fe value next 4af node address: 4af value next b node address: 88b value next c node address: 76c value next 0 f3c head (f3c) (2fe) (4af) (88b) (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 that 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. 2fe 44 4af 33 88b 22 76c 11

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)   f3c head 2fe n 2 (f3c) (2fe) (4af) (88b) (76c) 55 Now, let’s look at the 2nd part of the code that pops the 1st node into x. Step 1 is to pput the contents of the 1st node into x and this is done with the statement x= head->value; Step 2, we need to set up a pointer called n and make it point to the second node. This is accomplished with the statement: entry*n = head->next; In step 3, we need to release the memory occupied by the 1st node with delete head;. 2fe 44 4af 33 88b 22 76c 11 3 55 x 1

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 n 2fe (2fe) (4af) (88b) (76c) The last operation in popping is to make head point to the original 2nd node. As you can see, we have kept track of that node with the pointer n. So, head = n; makes head point to the 2nd node. There you have popping. You can verify that the 1st node is popped off by calling the printList() function. 44 4af 33 88b 22 76c 11 55 x

18 Summary Building a truly dynamic stack
Popping items off a stack in main ( ) In this presentation, we have showed you how to build a truly dynamic stack. We also learned about popping items off a stack in main(). In the next module, we will learn about popping items off in a function.


Download ppt "Popping Items Off a Stack Lesson xx"

Similar presentations


Ads by Google