chapter 6

48
Data Structures Using Jav a 1 Chapter 6 Stacks

Upload: winifred-cohen

Post on 31-Dec-2015

21 views

Category:

Documents


0 download

DESCRIPTION

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: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 6

Data Structures Using Java 1

Chapter 6

Stacks

Page 2: Chapter 6

Data Structures Using Java 2

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

Page 3: Chapter 6

Data Structures Using Java 3

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

Page 4: Chapter 6

Data Structures Using Java 4

Various Types of Stacks

Page 5: Chapter 6

Data Structures Using Java 5

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

Page 6: Chapter 6

Data Structures Using Java 6

Empty Stack

Page 7: Chapter 6

Data Structures Using Java 7

Stack Operations

Page 8: Chapter 6

Data Structures Using Java 8

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

Page 9: Chapter 6

Data Structures Using Java 9

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

Page 10: Chapter 6

Data Structures Using Java 10

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

Page 11: Chapter 6

Data Structures Using Java 11

Example of a Stack

Page 12: Chapter 6

Data Structures Using Java 12

Empty Stack

Page 13: Chapter 6

Data Structures Using Java 13

initializeStack

public void initializeStack(){ for(int i = 0; i < stackTop; i++) list[i] = null; stackTop = 0;}//end initializeStack

Page 14: Chapter 6

Data Structures Using Java 14

emptyStack and fullStack

public boolean isEmptyStack(){ return(stackTop == 0);}//end isEmptyStack

public boolean isFullStack()

{

return(stackTop == maxStackSize);

}//end isFullStack

Page 15: Chapter 6

Data Structures Using Java 15

Push

Page 16: Chapter 6

Data Structures Using Java 16

Pushpublic 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

Page 17: Chapter 6

Data Structures Using Java 17

Return Top Element

public DataElement top() throws StackUnderflowException{ if(isEmptyStack()) throw new StackUnderflowException(); DataElement temp = list[stackTop - 1].getCopy(); return temp; }//end top

Page 18: Chapter 6

Data Structures Using Java 18

Pop

public void pop() throws StackUnderflowException{ if(isEmptyStack()) throw new StackUnderflowException(); stackTop--; //decrement stackTop list[stackTop] = null;}//end pop

Page 19: Chapter 6

Data Structures Using Java 19

Pop

Page 20: Chapter 6

Data Structures Using Java 20

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 copy6

Page 21: Chapter 6

Data Structures Using Java 21

Constructors //constructor with a parameterpublic 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

Page 22: Chapter 6

Data Structures Using Java 22

Constructors

//default constructor

public StackClass()

{

maxStackSize = 100;

stackTop = 0; //set stackTop to 0

list = new DataElement[maxStackSize]; //create array

}//end default constructor

Page 23: Chapter 6

Data Structures Using Java 23

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

Page 24: Chapter 6

Data Structures Using Java 24

Time Complexity of Operations of class stackType

Page 25: Chapter 6

Data Structures Using Java 25

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 Lisa3.6 John3.9 Susan3.7 Kathy3.4 Jason3.9 David3.4 Jack

Page 26: Chapter 6

Data Structures Using Java 26

Programming Example: Highest GPA (Algorithm)

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

Page 27: Chapter 6

Data Structures Using Java 27

Highest GPA (Algorithm)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 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

}

Page 28: Chapter 6

Data Structures Using Java 28

Programming Example: Highest GPA (Algorithm)

7. Output the highest GPA.

8. Output the names of the students having the highest GPA.

Page 29: Chapter 6

Data Structures Using Java 29

Programming Example: Highest GPA (Sample Run)

Input File (Ch6_HighestGPAData.txt)3.4 Holt3.2 Bolt2.5 Colt3.4 Tom3.8 Ron3.8 Mickey3.6 Pluto3.5 Donald3.8 Cindy3.7 Dome3.9 Andy3.8 Fox3.9 Minnie2.7 Goofy3.9 Doc3.4 Danny

Page 30: Chapter 6

Data Structures Using Java 30

Programming Example: Highest GPA (Sample Run)

Output

Highest GPA = 3.90

The students holding the highest GPA are:

Doc

Minnie

Andy

Page 31: Chapter 6

Data Structures Using Java 31

Empty and Nonempty Linked Stack

Empty linked stack Nonempty linked stack

Page 32: Chapter 6

Data Structures Using Java 32

Default Constructor

public LinkedStackClass()

{

stackTop = null;

}

Page 33: Chapter 6

Data Structures Using Java 33

initializeStack, isStackEmpty, and isStackFull

public void initializeStack(){ stackTop = null;}//end initializeStack

public boolean isEmptyStack(){ return(stackTop == null);}public boolean isFullStack(){ return false;}

Page 34: Chapter 6

Data Structures Using Java 34

Push

Stack before the push operation

Stack and newNode

Page 35: Chapter 6

Data Structures Using Java 35

Push

Stack after the statement newNode.link = stackTop; executes

Stack after the statement stackTop = newNode; executes

Page 36: Chapter 6

Data Structures Using Java 36

Return Top Element

public DataElement top() throws StackUnderflowException{ if(stackTop == null) throw new StackUnderflowException(); return stackTop.info.getCopy();}//end top

Page 37: Chapter 6

Data Structures Using Java 37

Pop

Stack before the pop operation

Stack after the statement stackTop = stackTop.link; executes

Stack after popping the top element

Page 38: Chapter 6

Data Structures Using Java 38

Application of Stacks:Postfix Expression Calculator

• Prefix/Polish Notation

• Suffix/Postfix/Reverse Polish Notation

Page 39: Chapter 6

Data Structures Using Java 39

Application of Stacks:Postfix Expression Calculator

Page 40: Chapter 6

Data Structures Using Java 40

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 op1 + op2, which is 9

Page 41: Chapter 6

Data Structures Using Java 41

Application of Stacks:Postfix Expression Calculator

Stack after pushing 2

Stack after retrieving the top two elements and popping twice

Stack after pushing the result of op1 * op2, which is 18

Stack after popping the element

Page 42: Chapter 6

Data Structures Using Java 42

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

}

Page 43: Chapter 6

Data Structures Using Java 43

Nonrecursive Algorithm to Print Linked List

current = first; //Line 1while(current != NULL) //Line 2{ stack.push(current); //Line 3 current = current.link; //Line 4}

Page 44: Chapter 6

Data Structures Using Java 44

List After Execution of Statementcurrent = first;

Page 45: Chapter 6

Data Structures Using Java 45

Repeated Execution of:stack.push(current);

current = current.link;

Page 46: Chapter 6

Data Structures Using Java 46

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

Page 47: Chapter 6

Data Structures Using Java 47

Java class Stack

Page 48: Chapter 6

Data Structures Using Java 48

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