Popping Items Off a Stack Lesson xx

Slides:



Advertisements
Similar presentations
Doubly linked list concept Node structure Insertion sort Insertion sort program with a doubly linked list.
Advertisements

Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
February 11, 2005 More Pointers Dynamic Memory Allocation.
1 Writing a Good Program 8. Elementary Data Structure.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
1 Working with Pointers An exercise in destroying your computer.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
STACKS & QUEUES for CLASS XII ( C++).
Chapter 4 Stacks
Chapter 1.2 Introduction to C++ Programming
Pointers and Dynamic Arrays
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Popping Items Off a Stack Using a Function Lesson xx
Pointers and Linked Lists
Chapter 12 – Data Structures
Pointers and Linked Lists
Chapter 1.2 Introduction to C++ Programming
5.13 Recursion Recursive functions Functions that call themselves
Copy Constructor / Destructors Stacks and Queues
Chapter 9: Pointers.
Two-Dimensional Arrays Lesson xx
Basic Elements of C++.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Objectives Identify the built-in data types in C++
Linked lists.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Stack and Queue APURBO DATTA.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
void Pointers Lesson xx
Pointers and Linked Lists
Basic Elements of C++ Chapter 2.
Chapter 16-2 Linked Structures
Arrays & Functions Lesson xx
Structures Lesson xx In this module, we’ll introduce you to structures.
Returning Structures Lesson xx
Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.
One-Dimensional Array Introduction Lesson xx
File I/O with Records Lesson xx
Passing Structures Lesson xx
Chapter 9: Pointers.
Pointers and Linked Lists
Value returning Functions
Pointers, Dynamic Data, and Reference Types
Strings A collection of characters taken as a set:
Pointer to Structures Lesson xx
Functions Pass By Value Pass by Reference
Stacks Data structure Elements added, removed from one end only
Pointers & Dynamic Data Structures
Arrays Arrays A few types Structures of related data items
Pointers and dynamic objects
Linked lists.
Pointers, Dynamic Data, and Reference Types
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

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

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().

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.

Popping Diagram head head x 55 44 33 22 11 0 44 33 22 11 0 55 11 0 head 44 33 22 11 0 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

Steps to Pop 1st Node head head x 3 2 55 44 33 22 11 0 44 33 22 11 0 4 11 0 head 44 33 22 11 0 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

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.

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.

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.

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.

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

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.

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.

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.

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.

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

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

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

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.