Data Structures Using C++1 Chapter 7 Stacks. Data Structures Using C++2 Chapter Objectives Learn about stacks Examine various stack operations Learn how.

Slides:



Advertisements
Similar presentations
TK1924 Program Design & Problem Solving Session 2011/2012
Advertisements

Stacks, Queues, and Linked Lists
DATA STRUCTURES USING C++ Chapter 5
CS 240Chapter 6 - StacksPage 21 Chapter 6 Stacks The stack abstract data type is essentially a list using the LIFO (last-in-first-out) policy for adding.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Data Structures & Algorithms
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 18: Stacks and Queues
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Chapter 18: Stacks and Queues
Data Structures Using C++ 2E
Chapter 17: Stacks and Queues
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
What is a Stack? n Logical (or ADT) level: A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only.
Chapter 7 Stacks Dr. Youssef Harrath
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
Chapter 18: Stacks and Queues
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Data Structures Using C++
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Data Structures Using Java1 Chapter 6 Stacks. Data Structures Using Java2 Chapter Objectives Learn about stacks Examine various stack operations Learn.
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 Chapter 17: Stacks and Queues Learn about stacks Examine various stack operations Learn how to implement a stack as an array Learn how to implement a.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Chapter 5 (Part 1) ADT Stack 1 Fall Stacks TOP OF THE STACK.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Chapter 16: Linked Lists.
Chapter 4 Stacks
Data Structures Using C++ 2E
C++ Programming:. Program Design Including
Data Structures Using C++ 2E
Stacks and Queues Chapter 4.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Data Structures Array Based Stacks.
Stacks Stack: restricted variant of list
Stack.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Data Structures Array Based Stacks.
Stacks Data structure Elements added, removed from one end only
Stacks CS-240 Dick Steflik.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Presentation transcript:

Data Structures Using C++1 Chapter 7 Stacks

Data Structures Using C++2 Chapter Objectives Learn about stacks Examine various stack operations Learn how to implement a stack as an array Learn how to implement a stack as a linked list Discover stack applications Learn to use a stack to remove recursion Become aware of the STL class stack

Data Structures Using C++3 Stacks Definition: list of homogeneous elements, wherein the addition and deletion of elements occur only at one end, called the top of the stack Last In First Out (LIFO) data structure Used to implement function calls Used to convert recursive algorithms (especially not tail recursive) into nonrecursive algorithms

Data Structures Using C++4 Various Types of Stacks

Data Structures Using C++5 LIFO Last In First Out (LIFO) data structure –Top element of stack is last element to be added to stack –Elements added and removed from one end (top) –Item added last are removed first

Data Structures Using C++6 Empty Stack

Data Structures Using C++7 Stack Operations

Data Structures Using C++8 Basic Operations on a Stack initializeStack: Initializes the stack to an empty state destroyStack: Removes all the elements from the stack, leaving the stack empty isEmptyStack: Checks whether the stack is empty. If empty, it returns true; otherwise, it returns false

Data Structures Using C++9 Basic Operations on a Stack isFullStack: Checks whether the stack is full. If full, it returns true; otherwise, it returns false push: –Add new element to the top of the stack –The input consists of the stack and the new element. –Prior to this operation, the stack must exist and must not be full

Data Structures Using C++10 Basic Operations on a Stack top: Returns the top element of the stack. Prior to this operation, the stack must exist and must not be empty. pop: Removes the top element of the stack. Prior to this operation, the stack must exist and must not be empty.

Data Structures Using C++11 Example of a Stack

Data Structures Using C++12 Empty Stack

Data Structures Using C++13 initializeStack and destroyStack template void stackType ::initializeStack() { stackTop = 0; }//end initializeStack template void stackType ::destroyStack() { stackTop = 0; }//end destroyStack

Data Structures Using C++14 emptyStack and fullStack template bool stackType ::isEmptyStack() { return(stackTop == 0); }//end isEmptyStack template bool stackType ::isFullStack() { return(stackTop == maxStackSize); }//end isFullStack

Data Structures Using C++15 Push

Data Structures Using C++16 Push template void stackType ::push(const Type& newItem) { if(!isFullStack()) { list[stackTop] = newItem; //add newItem at the top //of the stack stackTop++; //increment stackTop } else cerr<<"Cannot add to a full stack."<<endl; }//end push

Data Structures Using C++17 Return Top Element template Type stackType ::top() { assert(stackTop != 0); //if the stack is empty, //terminate the program return list[stackTop - 1]; //return the element of the //stack indicated by //stackTop - 1 }//end top

Data Structures Using C++18 Pop template void stackType ::pop() { if(!isEmptyStack()) stackTop--; //decrement stackTop else cerr<<"Cannot remove from an empty stack."<<endl; }//end pop

Data Structures Using C++19 Pop

Data Structures Using C++20 copyStack template void stackType ::copyStack(const stackType & otherStack) { delete [] list; maxStackSize = otherStack.maxStackSize; stackTop = otherStack.stackTop; list = new Type[maxStackSize]; assert(list != NULL); //copy otherStack into this stack for(int j = 0; j < stackTop; j++) list[j] = otherStack.list[j]; }//end copyStack

Data Structures Using C++21 Copy Constructor template stackType ::stackType(const stackType & otherStack) { list = NULL; copyStack(otherStack); }//end copy constructor

Data Structures Using C++22 Overloading the Assignment Operator (=) template const stackType & stackType ::operator= (const stackType & otherStack) { if(this != &otherStack) //avoid self-copy copyStack(otherStack); return *this; }//end operator=

Data Structures Using C++23 Time-Complexity of Operations of class stackType

Data Structures Using C++24 Stack Header File //Header file: myStack.h #ifndef H_StackType #define H_StackType #include using namespace std; //Place the definition of the class template stackType, as given //previously in this chapter, here. //Place the definitions of the member functions, as discussed in //this chapter, here. #endif

Data Structures Using C++25 Programming Example: Highest GPA InputThe program reads an input file consisting of each student’s GPA, followed by the student’s name. Sample data is: 3.8 Lisa 3.6 John 3.9 Susan 3.7 Kathy 3.4 Jason 3.9 David 3.4 Jack

Data Structures Using C++26 Programming Example: Highest GPA (Algorithm) 1.Declare the variables. 2.Open the input file. 3.If the input file does not exist, exit the program. 4.Set the output of the floating-point numbers to a fixed decimal format with a decimal point and trailing zeroes. Also, set the precision to two decimal places. 5.Read the GPA and student name. 6. highestGPA = GPA; 7.Initialize the stack.

Data Structures Using C++27 Programming Example: Highest GPA (Algorithm) 8.while (not end of file) { 8.1 if (GPA > highestGPA) { destroyStack(stack); push(stack, student name); highestGPA = GPA; } 8.2 else if(GPA is equal to highestGPA) push(stack, student name); 8.3 Read the GPA and student name; }

Data Structures Using C++28 Programming Example: Highest GPA (Algorithm) 9.Output the highest GPA. 10.Output the names of the students having the highest GPA.

Data Structures Using C++29 Programming Example: Highest GPA (Sample Run) Input File (Ch7_HighestGPAData.txt) 3.4 Holt 3.2 Bolt 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Pluto 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Goofy 3.9 Doc 3.4 Danny

Data Structures Using C++30 Programming Example: Highest GPA (Sample Run) Output Highest GPA = 3.90 The students holding the highest GPA are: Doc Minnie Andy

Data Structures Using C++31 Empty and Nonempty Linked Stack Empty linked stackNonempty linked stack

Data Structures Using C++32 Default Constructor template //default constructor linkedStackType ::linkedStackType() { stackTop = NULL; }

Data Structures Using C++33 Destroy Stack template void linkedStackType ::destroyStack() { nodeType *temp; //pointer to delete the node while(stackTop != NULL) //while there are elements //in the stack { temp = stackTop; //set temp to point to //the current node stackTop = stackTop->link; //advance stackTop //to the next node delete temp; //deallocate the memory //occupied by temp } }//end destroyStack

Data Structures Using C++34 initializeStack and isStackEmpty template void linkedStackType :: initializeStack() { destroyStack(); } template bool linkedStackType ::isEmptyStack() { return(stackTop == NULL); } template bool linkedStackType ::isFullStack() { return false;

Data Structures Using C++35 Push Stack before the push operation Stack and newNode

Data Structures Using C++36 Push Stack after the statement newNode->link = stackTop; executes Stack after the statement stackTop = newNode; executes

Data Structures Using C++37 Return Top Element template Type linkedStackType ::top() { assert(stackTop != NULL); //if the stack is empty, //terminate the program return stackTop->info; //return the top element }//end top

Data Structures Using C++38 Pop Stack before the pop operation

Data Structures Using C++39 Pop Stack after the statements temp = stackTop ; and stackTop = stackTop->link; execute Stack after the statement delete temp; executes

Data Structures Using C++40 Application of Stacks: Postfix Expression Calculator

Data Structures Using C++41 Application of Stacks: Postfix Expression Calculator Stack after pushing 6 Stack after pushing 3 Stack after retrieving the top two elements and popping twice Stack after pushing the result of op1 + op2, which is 9

Data Structures Using C++42 Application of Stacks: Postfix Expression Calculator Stack after pushing 2 Stack after retrieving the top two elements and popping twice Stack after pushing the result of op1 * op2, which is 18 Stack after popping the element

Data Structures Using C++43 Postfix Expression Calculator (Main Algorithm)

Data Structures Using C++44 Nonrecursive Algorithm to reverse linked list current = first; while(current != NULL) { stack.push(current); current = current->link; } llistType, *newfirst = stack.pop(); current = newfirst; while (!stack.empty()) current->link = stack.pop(); current->link = NULL;

Data Structures Using C++45 List After Execution of Statement current = first;

Data Structures Using C++46 Repeated Execution of: stack.push(current); current = current->link;

Data Structures Using C++47 STL class stack (Stack Container Adapter) Standard Template Library (STL) provides a class to implement a stack in a program Name of the class defining a stack is “stack” Name of the header file containing the definition of the class stack is “stack”

Data Structures Using C++48 Operations on a stack Object