36
COMP 121 Week 13: Stacks

COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Embed Size (px)

Citation preview

Page 1: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

COMP 121

Week 13: Stacks

Page 2: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Objectives Learn about the stack data type and how

to use its four methods: push, pop, peek, and empty

Understand how Java implements a stack Learn how to implement a stack using an

underlying array or a linked list See how to use a stack to perform various

applications

Page 3: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Stack Abstract Data Type Can be compared to a Pez dispenser

Only the top item can be accessed (added or removed)

Can only extract one item at a time LIFO: Last-In, First-Out

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 4: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Specification of the Stack Abstract Data Type Only the top element of a stack is visible,

therefore the number of operations performed by a stack are few

Need the ability to: Inspect the top elementRetrieve the top elementPush a new element on the stackTest for an empty stack

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 5: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Specification of the Stack Abstract Data Type (continued)

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 6: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Stack Application: Palindrome Finder Palindrome: A string that reads the same

in either directionExample: “Able was I ere I saw Elba”

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 7: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

PalindromeFinder

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 8: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

PalindromeFinderpublic class PalindromeFinder { private String inputString; private Stack < Character > charStack = new Stack < Character > ();

/** Store the argument string in a stack of characters. @param str String of characters to store in the stack */ public PalindromeFinder(String str) { inputString = str; fillStack(); }

/** Method to fill a stack of characters from an input string. */ private void fillStack() { for (int i = 0; i < inputString.length(); i++) { charStack.push(inputString.charAt(i)); } }

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 9: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

PalindromeFinder (continued) /** Method to build a string containing the characters in a stack. @return The string containing the words in the stack */ private String buildReverse() { StringBuilder result = new StringBuilder(); while (!charStack.empty()) { // Remove top item from stack and append it to result. result.append(charStack.pop()); } return result.toString(); }

public boolean isPalindrome() { return inputString.equalsIgnoreCase(buildReverse()); }}

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 10: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Stack Application: Parentheses Checker When analyzing arithmetic expressions, it

is important to determine whether an expression is balanced with respect to parentheses(a+b*(c/(d-e)))+(d/e)

Problem is further complicated if braces or brackets are used in conjunction with parentheses

Solution is to use stacks!Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using

Java Version 5.0. New York: John Wiley & Sons.

Page 11: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

ParenChecker

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 12: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

ParenChecker (continued)

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 13: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Vector Implementation of Stack The Java API includes a Stack class as

part of the package java.util The vector class implements a growable

array of objects Elements of a vector can be accessed

using an integer index Size of a vector can grow or shrink to

accommodate adding and removing elements

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 14: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Vector Implementation of Stack (continued)

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 15: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

List Implementation of a Stack Can use either the ArrayList, Vector, or the

LinkedList classes as all implement the List interface

Name of class illustrated in text is ListStack<E>ListStack is an adapter class as it adapts the

methods available in another class to the interface its clients expect by giving different names to essentially the same operations

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 16: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Array Implementation of a Stack

Need to allocate storage for an array with an initial default capacity when creating a new stack object

Need to keep track of the top of the stack No size method

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 17: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Array Implementation of a Stack (continued)

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 18: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

LinkedList Implementation of a Stack We can implement a stack using a linked

list of nodes

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 19: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Comparing Stack Implementations

Extending a Vector (as is done by Java) is a poor choice for stack implementation as all Vector methods are accessible

Easiest implementation would be to use a List component for storing the dataCan use any List, but ArrayList is simplest

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 20: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Comparing Stack Implementations (continued)

Using an array implementation is slightly harderThe index to the top of the stack would have

to be kept up-to-dateNew array would have to be created when

stack filled the allocated size

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 21: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Comparing Stack Implementations (continued)

Using a linked data structure to implement the stack is fairly easyStorage allocation/reallocation is not a

problemFlexibility provided by linked structure is not

used

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 22: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Comparing Efficiency List Component Implementation

All insertions and deletions are done at one end, so stack operations are O(1)

Array ImplementationAgain, all insertion and deletions are done at

one end, so stack operations are O(1) Linked Data Structure Implementation

Again, all insertion and deletions are done at one end, so stack operations are O(1)

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 23: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Additional Stack Applications Consider two case studies that relate to

evaluating arithmetic expressionsPostfix and infix notation

Expressions normally written in infix formBinary operators inserted between their

operands A computer normally scans an expression

string in the order that it is input; easier to evaluate an expression in postfix form

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 24: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Additional Stack Applications (continued)

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 25: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Additional Stack Applications (continued) Advantage of postfix form is that there is

no need to group subexpressions in parentheses

No need to consider operator precedence

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 26: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Evaluating Postfix Expressions

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 27: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Evaluating Postfix Expressions (continued)

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 28: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Evaluating Postfix Expressions (continued)

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 29: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Converting Infix to Postfix

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 30: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Converting Infix to Postfix (continued)

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 31: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Converting Infix to Postfix (continued)

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 32: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Converting Infix to Postfix (continued)

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 33: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Converting Infix to Postfix (continued)

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 34: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Summary A stack is a simple, last-in, first-out (LIFO)

data structure Four operations: empty, peek, pop, and

push Stacks are useful to process information in

the reverse of the order that it is encountered

Java.util.Stack is implemented as an extension of the Vector class

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 35: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Summary (continued) Three ways to implement a stack:

Using an object of a class that implements the List interface as a container

Using an array as a containerUsing a linked list as a container

Stacks can be applied in programs for evaluating arithmetic expressions

Koffman, E.B. & Wolfgang, P.A.T. (2003). Objects, Abstraction, Data Structures, and Design Using Java Version 5.0. New York: John Wiley & Sons.

Page 36: COMP 121 Week 13: Stacks. Objectives Learn about the stack data type and how to use its four methods: push, pop, peek, and empty Understand how Java implements

Any Questions?