Chapter Objectives Chapter 7 Stacks 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++ 1 Data Structures Using C++ 2 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 Various Types of Stacks Data Structures Using C++ 3 Data Structures Using C++ 4 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 Empty Stack Data Structures Using C++ 5 Data Structures Using C++ 6
Stack Operations 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++ 7 Data Structures Using C++ 8 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 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++ 9 Data Structures Using C++ 10 Example of a Stack Empty Stack Data Structures Using C++ 11 Data Structures Using C++ 12
initializestack and destroystack void stacktype<type>::initializestack() stacktop = 0; //end initializestack void stacktype<type>::destroystack() stacktop = 0; //end destroystack Data Structures Using C++ 13 emptystack and fullstack bool stacktype<type>::isemptystack() return(stacktop == 0); //end isemptystack bool stacktype<type>::isfullstack() return(stacktop == maxstacksize); //end isfullstack Data Structures Using C++ 14 Push Push 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 Data Structures Using C++ 15 Data Structures Using C++ 16 Return Top Element Pop Type stacktype<type>::top() assert(stacktop!= 0); return list[stacktop - 1]; //end top //if the stack is empty, //terminate the program //return the element of the //stack indicated by //stacktop - 1 void stacktype<type>::pop() if(!isemptystack()) stacktop--; //decrement stacktop else cerr<<"cannot remove from an empty stack."<<endl; //end pop Data Structures Using C++ 17 Data Structures Using C++ 18
Pop copystack 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 Data Structures Using C++ 19 Data Structures Using C++ 20 Copy Constructor Overloading the Assignment Operator (=) stacktype<type>::stacktype(const stacktype<type>& otherstack) list = NULL; copystack(otherstack); //end copy constructor const stacktype<type>& stacktype<type>::operator= (const stacktype<type>& otherstack) if(this!= &otherstack) //avoid self-copy copystack(otherstack); return *this; //end operator= Data Structures Using C++ 21 Data Structures Using C++ 22 Time-Complexity of Operations of class stacktype //Header file: mystack.h Stack Header File #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. Data Structures Using C++ 23 //Place the definitions of the member functions, as discussed in //this chapter, here. #endif Data Structures Using C++ 24
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 Lisa 3.6 John 3.9 Susan 3.7 Kathy 3.4 Jason 3.9 David 3.4 Jack Data Structures Using C++ 25 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++ 26 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; Data Structures Using C++ 27 GPA (Algorithm) 9. Output the highest GPA. 10. Output the names of the students having the highest GPA. Data Structures Using C++ 28 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 GPA (Sample Run) 3.4 Danny Data Structures Using C++ 29 Data Structures Using C++ 30 Output Highest GPA = 3.90 The students holding the highest GPA are: Doc Minnie Andy
Empty and Nonempty Linked Stack Default Constructor //default constructor linkedstacktype<type>::linkedstacktype() stacktop = NULL; Empty linked stack Nonempty linked stack Data Structures Using C++ 31 Data Structures Using C++ 32 Destroy Stack 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 Data Structures Using C++ 33 initializestack and isstackempty void linkedstacktype<type>:: initializestack() destroystack(); bool linkedstacktype<type>::isemptystack() return(stacktop == NULL); bool linkedstacktype<type>::isfullstack() return false; Data Structures Using C++ 34 Push Push Stack before the push operation Stack and newnode Stack after the statement newnode->link = stacktop; executes Stack after the statement stacktop = newnode; executes Data Structures Using C++ 35 Data Structures Using C++ 36
Return Top Element Pop Type linkedstacktype<type>::top() assert(stacktop!= NULL); return stacktop->info; //end top //if the stack is empty, //terminate the program //return the top element Stack before the pop operation Data Structures Using C++ 37 Data Structures Using C++ 38 Pop Application of Stacks: Postfix Expression Calculator Stack after the statements temp = stacktop; and stacktop = stacktop->link; execute Stack after the statement delete temp; executes Data Structures Using C++ 39 Data Structures Using C++ 40 Application of Stacks: Postfix Expression Calculator Application of Stacks: Postfix Expression Calculator Stack after pushing 6 Stack after retrieving the top two elements and popping twice Stack after pushing 2 Stack after pushing the result of op1 * op2, which is 18 Stack after pushing 3 Stack after pushing the result of op1 + op2, which is 9 Data Structures Using C++ 41 Stack after retrieving the top two elements Stack after popping the element and popping twice Data Structures Using C++ 42
Postfix Expression Calculator (Main Algorithm) 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++ 43 Data Structures Using C++ 44 List After Execution of Statement current = first; Repeated Execution of: stack.push(current); current = current->link; Data Structures Using C++ 45 Data Structures Using C++ 46 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 Operations on a stack Object Data Structures Using C++ 47 Data Structures Using C++ 48