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

Slides:



Advertisements
Similar presentations
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
Advertisements

Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Pointers and dynamic objects COMP171 Fall Pointers and dynamic objects/ Slide 2 Topics * Pointers n Memory addresses n Declaration n Dereferencing.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Pointers OVERVIEW.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
1 Working with Pointers An exercise in destroying your computer.
Copyright © 2002 Pearson Education, Inc. Slide 1.
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.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
 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.
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
  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.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
STACKS & QUEUES for CLASS XII ( C++).
Popping Items Off a Stack Using a Function Lesson xx
Pointers and Linked Lists
Chapter 12 – Data Structures
Pointers and Linked Lists
5.13 Recursion Recursive functions Functions that call themselves
Copy Constructor / Destructors Stacks and Queues
Two-Dimensional Arrays Lesson xx
Data Structure and Algorithms
Doubly Linked List Review - We are writing this code
Dr. Bernard Chen Ph.D. University of Central Arkansas
Objectives In this lesson, you will learn to: Define stacks
Linked lists.
Stacks and Queues.
Programming Abstractions
Pointers and Pointer-Based Strings
Stack and Queue APURBO DATTA.
Student Book An Introduction
CSCE 210 Data Structures and Algorithms
Prof. Neary Adapted from slides by Dr. Katherine Gibson
void Pointers Lesson xx
CMSC 341 Lecture 5 Stacks, Queues
Chapter 16-2 Linked Structures
Arrays & Functions Lesson xx
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
Popping Items Off a Stack Lesson xx
Value returning Functions
Programming Abstractions
Functions Pass By Value Pass by Reference
Chapter 16 Linked Structures
Abstract Data Types and Stacks
Recall: stacks and queues
Stacks and Queues.
Pointers and Pointer-Based Strings
Data Structures & Algorithms
Chapter 18 Recursion.
List Iterator Implementation
CSCS-200 Data Structure and Algorithms
Linked lists.
LINEAR DATA STRUCTURES
Presentation transcript:

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

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.

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 0 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

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.

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;   } }  

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

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.

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

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

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

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

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

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

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

End of the Loop head 55 44 33 22 11 0 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.

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

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

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.

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