48
Data Structures Using C++ 1 Chapter 7 Stacks

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

Embed Size (px)

Citation preview

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

Data Structures Using C++ 1

Chapter 7

Stacks

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

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

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

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

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

Data Structures Using C++ 4

Various Types of Stacks

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

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

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

Data Structures Using C++ 6

Empty Stack

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

Data Structures Using C++ 7

Stack Operations

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

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

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

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

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

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.

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

Data Structures Using C++ 11

Example of a Stack

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

Data Structures Using C++ 12

Empty Stack

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

Data Structures Using C++ 13

initializeStack and destroyStack

template<class Type>void stackType<Type>::initializeStack(){ stackTop = 0;}//end initializeStack

template<class Type>

void stackType<Type>::destroyStack()

{

stackTop = 0;

}//end destroyStack

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

Data Structures Using C++ 14

emptyStack and fullStack

template<class Type>bool stackType<Type>::isEmptyStack(){ return(stackTop == 0);}//end isEmptyStack

template<class Type>

bool stackType<Type>::isFullStack()

{

return(stackTop == maxStackSize);

}//end isFullStack

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

Data Structures Using C++ 15

Push

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

Data Structures Using C++ 16

Pushtemplate<class Type>void stackType<Type>::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

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

Data Structures Using C++ 17

Return Top Element

template<class Type>Type stackType<Type>::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

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

Data Structures Using C++ 18

Pop

template<class Type>void stackType<Type>::pop(){ if(!isEmptyStack()) stackTop--; //decrement stackTop else cerr<<"Cannot remove from an empty stack."<<endl;}//end pop

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

Data Structures Using C++ 19

Pop

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

Data Structures Using C++ 20

copyStacktemplate<class Type>void stackType<Type>::copyStack(const stackType<Type>& 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

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

Data Structures Using C++ 21

Copy Constructor

template<class Type>stackType<Type>::stackType(const stackType<Type>& otherStack){ list = NULL; copyStack(otherStack);}//end copy constructor

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

Data Structures Using C++ 22

Overloading the Assignment Operator (=)

template<class Type>const stackType<Type>& stackType<Type>::operator= (const stackType<Type>& otherStack){ if(this != &otherStack) //avoid self-copy copyStack(otherStack); return *this;}//end operator=

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

Data Structures Using C++ 23

Time-Complexity of Operations of class stackType

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

Data Structures Using C++ 24

Stack Header File//Header file: myStack.h

#ifndef H_StackType#define H_StackType

#include <iostream>#include <cassert>

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

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

Data Structures Using C++ 25

Programming Example: Highest GPA

Input The program reads an input file consisting of each student’s GPA, followed by the student’s name. Sample data is:

3.8 Lisa3.6 John3.9 Susan3.7 Kathy3.4 Jason3.9 David3.4 Jack

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

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.

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

Data Structures Using C++ 27

Programming Example: Highest GPA (Algorithm)

8. while (not end of file) { 8.1 if (GPA > highestGPA) { 8.1.1 destroyStack(stack); 8.1.2 push(stack, student name); 8.1.3 highestGPA = GPA; } 8.2 else if(GPA is equal to highestGPA) push(stack, student name); 8.3 Read the GPA and student name;}

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

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.

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

Data Structures Using C++ 29

Programming Example: Highest GPA (Sample Run)

Input File (Ch7_HighestGPAData.txt)3.4 Holt3.2 Bolt2.5 Colt3.4 Tom3.8 Ron3.8 Mickey3.6 Pluto3.5 Donald3.8 Cindy3.7 Dome3.9 Andy3.8 Fox3.9 Minnie2.7 Goofy3.9 Doc3.4 Danny

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

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

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

Data Structures Using C++ 31

Empty and Nonempty Linked Stack

Empty linked stack Nonempty linked stack

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

Data Structures Using C++ 32

Default Constructor

template<class Type> //default constructor

linkedStackType<Type>::linkedStackType()

{

stackTop = NULL;

}

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

Data Structures Using C++ 33

Destroy Stacktemplate<class Type>void linkedStackType<Type>::destroyStack(){ nodeType<Type> *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

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

Data Structures Using C++ 34

initializeStack and isStackEmptytemplate<class Type>void linkedStackType<Type>:: initializeStack(){ destroyStack();}

template<class Type>

bool linkedStackType<Type>::isEmptyStack()

{

return(stackTop == NULL);

}

template<class Type>

bool linkedStackType<Type>::isFullStack()

{

return false;

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

Data Structures Using C++ 35

Push

Stack before the push operation

Stack and newNode

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

Data Structures Using C++ 36

Push

Stack after the statement newNode->link = stackTop; executes

Stack after the statement stackTop = newNode; executes

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

Data Structures Using C++ 37

Return Top Element

template<class Type>Type linkedStackType<Type>::top(){ assert(stackTop != NULL); //if the stack is empty, //terminate the program return stackTop->info; //return the top element}//end top

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

Data Structures Using C++ 38

Pop

Stack before the pop operation

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

Data Structures Using C++ 39

Pop

Stack after the statements temp = stackTop; and stackTop = stackTop->link; execute

Stack after the statement delete temp; executes

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

Data Structures Using C++ 40

Application of Stacks:Postfix Expression Calculator

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

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

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

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

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

Data Structures Using C++ 43

Postfix Expression Calculator (Main Algorithm)

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

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;

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

Data Structures Using C++ 45

List After Execution of Statementcurrent = first;

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

Data Structures Using C++ 46

Repeated Execution of:stack.push(current);current = current->link;

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

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”

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

Data Structures Using C++ 48

Operations on a stack Object