Download COMP 121 Week 13

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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 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

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.
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 element
 Retrieve the top element
 Push a new element on the stack
 Test 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.
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.
Stack Application: Palindrome
Finder

Palindrome: A string that reads the same
in either direction
 Example:
“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.
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.
PalindromeFinder
public 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 data

 Can
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.
Comparing Stack Implementations
(continued)

Using an array implementation is slightly
harder
 The
index to the top of the stack would have
to be kept up-to-date
 New 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.
Comparing Stack Implementations
(continued)

Using a linked data structure to implement
the stack is fairly easy
 Storage
allocation/reallocation is not a
problem
 Flexibility 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.
Comparing Efficiency

List Component Implementation
 All
insertions and deletions are done at one
end, so stack operations are O(1)

Array Implementation
 Again,
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.
Additional Stack Applications

Consider two case studies that relate to
evaluating arithmetic expressions
 Postfix

and infix notation
Expressions normally written in infix form
 Binary
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 container
 Using 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.
Any Questions?