480 likes | 555 Views
Chapter 6. Stacks. 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. Stacks. Definition:
E N D
Chapter 6 Stacks Data Structures Using Java
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 Data Structures Using Java
Stacks • Definition: • list of homogeneous elements • addition and deletion of elements occurs only at one end, called the top of the stack • Last In First Out (LIFO) data structure • Used to implement method calls • Used to convert recursive algorithms (especially not tail recursive) into nonrecursive algorithms Data Structures Using Java
Various Types of Stacks Data Structures Using Java
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 Java
Empty Stack Data Structures Using Java
Stack Operations Data Structures Using Java
Basic Operations on a Stack • initializeStack: Initializes the stack to an empty state • isEmptyStack: Checks whether the stack is empty. If empty, it returns true; otherwise, it returns false • isFullStack: Checks whether the stack is full. If full, it returns true; otherwise, it returns false Data Structures Using Java
Basic Operations on a Stack • 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 Java
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 Java
Example of a Stack Data Structures Using Java
Empty Stack Data Structures Using Java
initializeStack public void initializeStack() { for(int i = 0; i < stackTop; i++) list[i] = null; stackTop = 0; }//end initializeStack Data Structures Using Java
emptyStack and fullStack public boolean isEmptyStack() { return(stackTop == 0); }//end isEmptyStack public boolean isFullStack() { return(stackTop == maxStackSize); }//end isFullStack Data Structures Using Java
Push Data Structures Using Java
Push public void push(DataElement newItem) throws StackOverflowException { if(isFullStack()) throw new StackOverflowException(); list[stackTop] = newItem.getCopy(); //add newItem at the //top of the stack stackTop++; //increment stackTop }//end push Data Structures Using Java
Return Top Element public DataElement top() throws StackUnderflowException { if(isEmptyStack()) throw new StackUnderflowException(); DataElement temp = list[stackTop - 1].getCopy(); return temp; }//end top Data Structures Using Java
Pop public void pop() throws StackUnderflowException { if(isEmptyStack()) throw new StackUnderflowException(); stackTop--; //decrement stackTop list[stackTop] = null; }//end pop Data Structures Using Java
Pop Data Structures Using Java
copy private void copy(StackClass otherStack) { list = null; System.gc(); maxStackSize = otherStack.maxStackSize; stackTop = otherStack.stackTop; list = new DataElement[maxStackSize]; //copy otherStack into this stack for(int i = 0; i < stackTop; i++) list[i] = otherStack.list[i].getCopy(); }//end copy 6 Data Structures Using Java
Constructors //constructor with a parameter public StackClass(int stackSize) { if(stackSize <= 0) { System.err.println(“The size of the array to implement “ + “the stack must be positive.”); System.err.println(“Creating an array of size 100.”); maxStackSize = 100; } else maxStackSize = stackSize; //set the stack size to //the value specified by //the parameter stackSize stackTop = 0; //set stackTop to 0 list = new DataElement[maxStackSize]; //create the array }//end constructor Data Structures Using Java
Constructors //default constructor public StackClass() { maxStackSize = 100; stackTop = 0; //set stackTop to 0 list = new DataElement[maxStackSize]; //create array }//end default constructor Data Structures Using Java
Copy Constructor and copyStack public StackClass(StackClass otherStack) { copy(otherStack); }//end copy constructor public void copyStack(StackClass otherStack) { if(this != otherStack) //avoid self-copy copy(otherStack); }//end copyStack Data Structures Using Java
Time Complexity of Operations of class stackType Data Structures Using Java
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 Lisa 3.6 John 3.9 Susan 3.7 Kathy 3.4 Jason 3.9 David 3.4 Jack Data Structures Using Java
Programming Example: Highest GPA (Algorithm) • Declare the variables • Create a DecimalFormat object to output a decimal number to two decimal places • Open the input file • If the input file does not exist, exit the program • Read the next input line Data Structures Using Java
Highest GPA (Algorithm) • while (not end of file) { 6.a. Tokenize the input line 6.b. Get the next GPA 6.c. Get the next name 6.d. if (GPA > highestGPA) { 6.d.i initialize stack 6.d.ii push(stack, student name) 6.d.iii highestGPA = GPA } 6.e. else if(GPA is equal to highestGPA) push(stack, student name) 6.f Read the next input line } Data Structures Using Java
Programming Example: Highest GPA (Algorithm) • Output the highest GPA. • Output the names of the students having the highest GPA. Data Structures Using Java
Programming Example: Highest GPA (Sample Run) Input File (Ch6_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 Java
Programming Example: Highest GPA (Sample Run) Output Highest GPA = 3.90 The students holding the highest GPA are: Doc Minnie Andy Data Structures Using Java
Empty and Nonempty Linked Stack Empty linked stack Nonempty linked stack Data Structures Using Java
Default Constructor public LinkedStackClass() { stackTop = null; } Data Structures Using Java
initializeStack, isStackEmpty, and isStackFull public void initializeStack() { stackTop = null; }//end initializeStack public boolean isEmptyStack() { return(stackTop == null); } public boolean isFullStack() { return false; } Data Structures Using Java
Push Stack before the push operation Stack and newNode Data Structures Using Java
Push Stack after the statement newNode.link = stackTop; executes Stack after the statement stackTop = newNode; executes Data Structures Using Java
Return Top Element public DataElement top() throws StackUnderflowException { if(stackTop == null) throw new StackUnderflowException(); return stackTop.info.getCopy(); }//end top Data Structures Using Java
Pop Stack after the statement stackTop = stackTop.link; executes Stack after popping the top element Stack before the popoperation Data Structures Using Java
Application of Stacks:Postfix Expression Calculator • Prefix/Polish Notation • Suffix/Postfix/Reverse Polish Notation Data Structures Using Java
Application of Stacks:Postfix Expression Calculator Data Structures Using Java
Application of Stacks:Postfix Expression Calculator Stack after pushing 6 Stack after retrieving the top two elements and popping twice Stack after pushing 3 Stack after pushing the result of op1 + op2, which is 9 Data Structures Using Java
Application of Stacks:Postfix Expression Calculator Stack after pushing 2 Stack after pushing the result of op1 * op2, which is 18 Stack after retrieving the top two elements and popping twice Stack after popping the element Data Structures Using Java
Postfix Expression Calculator (Main Algorithm) Get the next expression while more data to process { a. initialize the stack b. process the expression c. output the result d. get the next expression } Data Structures Using Java
Nonrecursive Algorithm to Print Linked List current = first; //Line 1 while(current != NULL) //Line 2 { stack.push(current); //Line 3 current = current.link; //Line 4 } Data Structures Using Java
List After Execution of Statementcurrent = first; Data Structures Using Java
Repeated Execution of:stack.push(current);current = current.link; Data Structures Using Java
Java class Stack • Java provides a class to implement a stack in a program • The name of the Java class defining a stack is Stack • The class Stack is contained in the package java.util • Table 6-3 lists the members of the class Stack Data Structures Using Java
Java class Stack Data Structures Using Java
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 • Java class Stack Data Structures Using Java