Chapter 7 Stacks Data Structures Using C 1

  • Slides: 50
Download presentation
Chapter 7 Stacks Data Structures Using C++ 1

Chapter 7 Stacks Data Structures Using C++ 1

Chapter Objectives • • Learn about stacks Examine various stack operations Learn how to

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

Stacks • Definition: list of homogeneous elements, wherein the addition and deletion of elements

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

Various Types of Stacks Data Structures Using C++ 4

Various Types of Stacks Data Structures Using C++ 4

LIFO • Last In First Out (LIFO) data structure – Top element of stack

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

Empty Stack Data Structures Using C++ 6

Empty Stack Data Structures Using C++ 6

Stack Operations Data Structures Using C++ 7

Stack Operations Data Structures Using C++ 7

Basic Operations on a Stack • initialize. Stack: Initializes the stack to an empty

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

Basic Operations on a Stack • is. Full. Stack: Checks whether the stack is

Basic Operations on a Stack • is. Full. Stack: 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++ 9

Basic Operations on a Stack • top: Returns the top element of the stack.

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

Example of a Stack Data Structures Using C++ 11

Example of a Stack Data Structures Using C++ 11

Empty Stack Data Structures Using C++ 12

Empty Stack Data Structures Using C++ 12

initialize. Stack and destroy. Stack template<class Type> void stack. Type<Type>: : initialize. Stack() {

initialize. Stack and destroy. Stack template<class Type> void stack. Type<Type>: : initialize. Stack() { stack. Top = 0; }//end initialize. Stack template<class Type> void stack. Type<Type>: : destroy. Stack() { stack. Top = 0; }//end destroy. Stack Data Structures Using C++ 13

empty. Stack and full. Stack template<class Type> bool stack. Type<Type>: : is. Empty. Stack()

empty. Stack and full. Stack template<class Type> bool stack. Type<Type>: : is. Empty. Stack() { return(stack. Top == 0); }//end is. Empty. Stack template<class Type> bool stack. Type<Type>: : is. Full. Stack() { return(stack. Top == max. Stack. Size); }//end is. Full. Stack Data Structures Using C++ 14

Push Data Structures Using C++ 15

Push Data Structures Using C++ 15

Push template<class Type> void stack. Type<Type>: : push(const Type& new. Item) { if(!is. Full.

Push template<class Type> void stack. Type<Type>: : push(const Type& new. Item) { if(!is. Full. Stack()) { list[stack. Top] = new. Item; //add new. Item at the top //of the stack. Top++; //increment stack. Top } else cerr<<"Cannot add to a full stack. "<<endl; }//end push Data Structures Using C++ 16

Return Top Element template<class Type> Type stack. Type<Type>: : top() { assert(stack. Top !=

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

Pop template<class Type> void stack. Type<Type>: : pop() { if(!is. Empty. Stack()) stack. Top-;

Pop template<class Type> void stack. Type<Type>: : pop() { if(!is. Empty. Stack()) stack. Top-; //decrement stack. Top else cerr<<"Cannot remove from an empty stack. "<<endl; }//end pop Data Structures Using C++ 18

Pop Data Structures Using C++ 19

Pop Data Structures Using C++ 19

copy. Stack template<class Type> void stack. Type<Type>: : copy. Stack(const stack. Type<Type>& other. Stack)

copy. Stack template<class Type> void stack. Type<Type>: : copy. Stack(const stack. Type<Type>& other. Stack) { delete [] list; max. Stack. Size = other. Stack. max. Stack. Size; stack. Top = other. Stack. stack. Top; list = new Type[max. Stack. Size]; assert(list != NULL); //copy other. Stack into this stack for(int j = 0; j < stack. Top; j++) list[j] = other. Stack. list[j]; }//end copy. Stack Data Structures Using C++ 20

Copy Constructor template<class Type> stack. Type<Type>: : stack. Type(const stack. Type<Type>& other. Stack) {

Copy Constructor template<class Type> stack. Type<Type>: : stack. Type(const stack. Type<Type>& other. Stack) { list = NULL; copy. Stack(other. Stack); }//end copy constructor Data Structures Using C++ 21

Overloading the Assignment Operator (=) template<class Type> const stack. Type<Type>& stack. Type<Type>: : operator=

Overloading the Assignment Operator (=) template<class Type> const stack. Type<Type>& stack. Type<Type>: : operator= (const stack. Type<Type>& other. Stack) { if(this != &other. Stack) //avoid self-copy. Stack(other. Stack); return *this; }//end operator= Data Structures Using C++ 22

Time-Complexity of Operations of class stack. Type Data Structures Using C++ 23

Time-Complexity of Operations of class stack. Type Data Structures Using C++ 23

Stack Header File //Header file: my. Stack. h #ifndef H_Stack. Type #define H_Stack. Type

Stack Header File //Header file: my. Stack. h #ifndef H_Stack. Type #define H_Stack. Type #include <iostream> #include <cassert> using namespace std; //Place the definition of the class template stack. Type, 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++ 24

Programming Example: Highest GPA Input The program reads an input file consisting of each

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 3. 6 3. 9 3. 7 3. 4 3. 9 3. 4 Lisa John Susan Kathy Jason David Jack Data Structures Using C++ 25

Programming Example: Highest GPA (Algorithm) 1. 2. 3. 4. Declare the variables. Open the

Programming Example: Highest GPA (Algorithm) 1. 2. 3. 4. Declare the variables. Open the input file. If the input file does not exist, exit the program. 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. highest. GPA = GPA; 7. Initialize the stack. Data Structures Using C++ 26

Programming Example: Highest GPA (Algorithm) 8. while (not end of file) { 8. 1

Programming Example: Highest GPA (Algorithm) 8. while (not end of file) { 8. 1 if (GPA > highest. GPA) { 8. 1. 1 destroy. Stack(stack); 8. 1. 2 push(stack, student name); 8. 1. 3 highest. GPA = GPA; } 8. 2 else if(GPA is equal to highest. GPA) push(stack, student name); 8. 3 Read the GPA and student name; } Data Structures Using C++ 27

Programming Example: Highest GPA (Algorithm) 9. Output the highest GPA. 10. Output the names

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

Programming Example: Highest GPA (Sample Run) Input File (Ch 7_Highest. GPAData. txt) 3. 4

Programming Example: Highest GPA (Sample Run) Input File (Ch 7_Highest. GPAData. txt) 3. 4 3. 2 2. 5 3. 4 3. 8 3. 6 3. 5 3. 8 3. 7 3. 9 3. 8 3. 9 2. 7 3. 9 3. 4 Holt Bolt Colt Tom Ron Mickey Pluto Donald Cindy Dome Andy Fox Minnie Goofy Doc Danny Data Structures Using C++ 29

Programming Example: Highest GPA (Sample Run) Output Highest GPA = 3. 90 The students

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

Empty and Nonempty Linked Stack Empty linked stack Nonempty linked stack Data Structures Using

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

Default Constructor template<class Type> //default constructor linked. Stack. Type<Type>: : linked. Stack. Type() {

Default Constructor template<class Type> //default constructor linked. Stack. Type<Type>: : linked. Stack. Type() { stack. Top = NULL; } Data Structures Using C++ 32

Destroy Stack template<class Type> void linked. Stack. Type<Type>: : destroy. Stack() { node. Type<Type>

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

initialize. Stack and is. Stack. Empty template<class Type> void linked. Stack. Type<Type>: : initialize.

initialize. Stack and is. Stack. Empty template<class Type> void linked. Stack. Type<Type>: : initialize. Stack() { destroy. Stack(); } template<class Type> bool linked. Stack. Type<Type>: : is. Empty. Stack() { return(stack. Top == NULL); } template<class Type> bool linked. Stack. Type<Type>: : is. Full. Stack() { return false; Data Structures Using C++ 34

Push Stack before the push operation Stack and new. Node Data Structures Using C++

Push Stack before the push operation Stack and new. Node Data Structures Using C++ 35

Push Stack after the statement new. Node ->link = stack. Top; executes Stack after

Push Stack after the statement new. Node ->link = stack. Top; executes Stack after the statement stack. Top = new. Node; executes Data Structures Using C++ 36

Return Top Element template<class Type> Type linked. Stack. Type<Type>: : top() { assert(stack. Top

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

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

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

Pop Stack after the statements temp = stack. Top; and stack. Top = stack.

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

Application of Stacks: Postfix Expression Calculator • Prefix/Polish Notation • Suffix/Postfix/Reverse Polish Notation Data

Application of Stacks: Postfix Expression Calculator • Prefix/Polish Notation • Suffix/Postfix/Reverse Polish Notation Data Structures Using C++ 40

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

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

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 op 1 + op 2, which is 9 Data Structures Using C++ 42

Application of Stacks: Postfix Expression Calculator Stack after pushing the result of op 1

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

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

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

Nonrecursive Algorithm to Print Linked List current = first; while(current != NULL) { stack.

Nonrecursive Algorithm to Print Linked List current = first; while(current != NULL) { stack. push(current); current = current->link; } Data Structures Using C++ //Line 1 //Line 2 //Line 3 //Line 4 45

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

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

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

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 Data Structures Using C++ 49

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

Chapter Summary • • Stack Data Structure Last In First Out (LIFO) Stacks Implemented

Chapter Summary • • Stack Data Structure Last In First Out (LIFO) Stacks Implemented as Arrays Stacks Implemented as Linked Lists Postfix Expression Calculator Nonrecursive Algorithm to Print Linked List STL class stack Data Structures Using C++ 50