8/3/2019 DSchap06
1/43
Chapter 6
stack
1
8/3/2019 DSchap06
2/43
` 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 2
8/3/2019 DSchap06
3/43
` Definition: list of homogeneous elements
addition and deletion of elements occurs only at oneend, 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
3
8/3/2019 DSchap06
4/43
Data Structures Using Java 4
8/3/2019 DSchap06
5/43
` 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 5
8/3/2019 DSchap06
6/43
Data Structures Using Java 6
8/3/2019 DSchap06
7/43
Data Structures Using Java 7
8/3/2019 DSchap06
8/43
` initializeStack: Initializes the stack to an emptystate
` 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 8
8/3/2019 DSchap06
9/43
` 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 9
8/3/2019 DSchap06
10/43
` 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. Priorto this operation, the stack must exist and must
not be empty
Data Structures Using Java 10
8/3/2019 DSchap06
11/43
Data Structures Using Java 11
8/3/2019 DSchap06
12/43
Data Structures Using Java 12
8/3/2019 DSchap06
13/43
public void initializeStack(){
for(int i = 0; i < stackTop; i++)
list[i] = null;stackTop = 0;
}//end initializeStack
13Data Structures Using Java
8/3/2019 DSchap06
14/43
public boolean isEmptyStack(){
return(stackTop == 0);
}//end isEmptyStack
public boolean isFullStack()
{return(stackTop == maxStackSize);
}//end isFullStack
14Data Structures Using Java
8/3/2019 DSchap06
15/43
15Data Structures Using Java
8/3/2019 DSchap06
16/43
Data Structures Using Java 16
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
8/3/2019 DSchap06
17/43
public DataElement top() throws StackUnderflowException
{if(isEmptyStack())
throw new StackUnderflowException();DataElement temp = list[stackTop - 1].getCopy();
return temp;}//end top
Data Structures Using Java 17
8/3/2019 DSchap06
18/43
Data Structures Using Java 18
public void pop() throws StackUnderflowException
{
if(isEmptyStack())throw new StackUnderflowException();
stackTop--; //decrement stackTop
list[stackTop] = null;
}//end pop
8/3/2019 DSchap06
19/43
19Data Structures Using Java
8/3/2019 DSchap06
20/43
Data Structures Using Java 20
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
8/3/2019 DSchap06
21/43
//constructor with a parameterpublic StackClass(int stackSize)
{
if(stackSize
8/3/2019 DSchap06
22/43
//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 22
8/3/2019 DSchap06
23/43
Data Structures Using Java 23
public StackClass(StackClass otherStack)
{
copy(otherStack);
}//end copy constructor
public void copyStack(StackClass otherStack)
{
if(this != otherStack) //avoid self-copy
copy(otherStack);
}//end copyStack
8/3/2019 DSchap06
24/43
Data Structures Using Java 24
Input The program reads an input file consisting of each
students GPA, followed by the students 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
8/3/2019 DSchap06
25/43
1. Declare the variables2. Create a DecimalFormat object to output a
decimal number to two decimal places3. Open the input file4. If the input file does not exist, exit the program5. Read the next input line
Data Structures Using Java 25
8/3/2019 DSchap06
26/43
6. while (not end of file)
{6.a. Tokenize the input line6.b. Get the next GPA6.c. Get the next name6.d. if (GPA > highestGPA)
{6.d.i initialize stack6.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 26
8/3/2019 DSchap06
27/43
7. Output the highest GPA.
8. Output the names of the students having thehighest GPA.
Data Structures Using Java 27
8/3/2019 DSchap06
28/43
Input File (Ch6_HighestGPAData.txt)3.4 Holt
3.2 Bolt
2.5 Colt
3.4 Tom
3.8 Ron3.8 Mickey
3.6 Pluto
3.5 Donald
3.8 Cindy
3.7 Dome
3.9 Andy
3.8 Fox3.9 Minnie
2.7 Goofy
3.9 Doc
3.4 Danny
Data Structures Using Java 28
8/3/2019 DSchap06
29/43
Output
Highest GPA = 3.90
The students holding the highest GPA are:
DocMinnie
Andy
Data Structures Using Java 29
8/3/2019 DSchap06
30/43
Data Structures Using Java 30
Empty linked stack Nonempty linked stack
8/3/2019 DSchap06
31/43
public LinkedStackClass()
{stackTop = null;
}
Data Structures Using Java 31
8/3/2019 DSchap06
32/43
public void initializeStack(){
stackTop = null;}//end initializeStack
public boolean isEmptyStack(){
return(stackTop == null);}
public boolean isFullStack(){return false;
}
32Data Structures Using Java
8/3/2019 DSchap06
33/43
Data Structures Using Java 33
Stack before thepush
operation
Stack and newNode
8/3/2019 DSchap06
34/43
Data Structures Using Java 34
Stack after the statementnewNode.link =
stackTop; executes
Stack after the statement
stackTop = newNode;
executes
8/3/2019 DSchap06
35/43
public DataElement top() throwsStackUnderflowException
{
if(stackTop == null)throw new StackUnderflowException();
return stackTop.info.getCopy();}//end top
Data Structures Using Java 35
8/3/2019 DSchap06
36/43
36Data Structures Using Java
Stack before thepop operation
Stack after the statementstackTop = stackTop.link; executes
Stack after popping the top element
8/3/2019 DSchap06
37/43
` Prefix/Polish Notation
` Suffix/Postfix/Reverse Polish Notation
Data Structures Using Java 37
8/3/2019 DSchap06
38/43
Data Structures Using Java 38
8/3/2019 DSchap06
39/43
39Data Structures Using Java
Stack after pushing 6
Stack after pushing 3
Stack after retrieving the top two elements
and popping twice
Stack after pushing the result ofop1 +
op2, which is 9
8/3/2019 DSchap06
40/43
40Data Structures Using Java
Stack after pushing 2
Stack after retrieving the top two elements
and popping twice
Stack after pushing the result ofop1 *
op2, which is 18
Stack after popping the element
8/3/2019 DSchap06
41/43
` Java provides a class to implement a stack in aprogram
` The name of the Java class defining a stack isStack
` The class Stack is contained in the packagejava.util
` Table 6-3 lists the members of the class Stack
Data Structures Using Java 41
8/3/2019 DSchap06
42/43
Data Structures Using Java 42
8/3/2019 DSchap06
43/43
` Stack Data Structure
` Last In First Out (LIFO)
` Stacks Implemented asArrays
` Stacks Implemented as Linked Lists` Postfix Expression Calculator
` NonrecursiveAlgorithm to Print Linked List
` Java class Stack
Data Structures Using Java 43