Beruflich Dokumente
Kultur Dokumente
` ` ` ` ` `
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
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
` ` `
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
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
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
10
11
12
public void initializeStack() { for(int i = 0; i < stackTop; i++) list[i] = null; stackTop = 0; }//end initializeStack
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
16
public DataElement top() throws StackUnderflowException { if(isEmptyStack()) throw new StackUnderflowException(); DataElement temp = list[stackTop - 1].getCopy(); return temp; }//end top
17
public void pop() throws StackUnderflowException { if(isEmptyStack()) throw new StackUnderflowException(); stackTop--; //decrement stackTop list[stackTop] = null; }//end pop
18
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
20
//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
21
//default constructor public StackClass() { maxStackSize = 100; stackTop = 0; //set stackTop to 0 list = new DataElement[maxStackSize]; //create array }//end default constructor
22
public void copyStack(StackClass otherStack) { if(this != otherStack) //avoid self-copy copy(otherStack); }//end copyStack
23
Input The program reads an input file consisting of each students GPA, followed by the students 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 Java 24
1. 2. 3. 4. 5.
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
25
6.
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 }
26
7. 8.
Output the highest GPA. Output the names of the students having the highest GPA.
27
28
Output Highest GPA = 3.90 The students holding the highest GPA are: Doc Minnie Andy
29
30
31
public void initializeStack() { stackTop = null; }//end initializeStack public boolean isEmptyStack() { return(stackTop == null); } public boolean isFullStack() { return false; }
Data Structures Using Java 32
33
34
public DataElement top() throws StackUnderflowException { if(stackTop == null) throw new StackUnderflowException(); return stackTop.info.getCopy(); }//end top
35
` `
37
38
Stack after retrieving the top two elements and popping twice
Stack after retrieving the top two elements and popping twice
` ` ` `
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
41
42
` ` ` ` ` ` `
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
43