You are on page 1of 41

Implementing and Using Stacks

many slides taken from Mike Scott, UT Austin

1
Stacks
Stacks are a straight forward and simple
data structure
Access is allowed only at one point of the
structure, normally termed the top of the
stack, so the operations are limited:
push (add item to stack)
pop (remove top item from stack)
top (get top item without removing it)
clear
isEmpty
size
2
Stack Operations
Assume a simple stack for integers.
Stack s = new Stack();
s.push(12);
s.push(4);
s.push( s.top() + 2 );
s.pop()
s.push( s.top() );
//what are contents of stack?

3
Stack Operations
Write a method to print out contents of stack
in reverse order.

4
Common Stack Error
Stack s = new Stack();
// put stuff in stack
for(int i = 0; i < 7; i++)
s.push( i );
// print out contents of stack
// while emptying it
for(int i = 0; i < s.size(); i++)
System.out.println( s.pop() );
// Output? Why?
// Crossover error

5
Corrected Version
Stack s = new Stack();
// put stuff in stack
for(int i = 0; i < 7; i++)
s.push( i );
// print out contents of stack
// while emptying it
int limit = s.size();
for(int i = 0; i < limit; i++)
System.out.println( s.pop() );
//or
// while( !s.isEmpty() )
// System.out.println( s.pop() );
6
Balanced Symbol Checking
In processing programs and working with
computer languages there are many
instances when symbols must be balanced
{},[],()

A stack is useful for checking symbol balance.


When a closing symbol is found it must match
the most recent opening symbol of the same
type.
Algorithm?

7
Algorithm for Balanced
Symbol Checking
Make an empty stack
read symbols until end of file
if the symbol is an opening symbol push it onto
the stack
if it is a closing symbol do the following
if the stack is empty report an error
otherwise pop the stack. If the symbol popped does
not match the closing symbol report an error
At the end of the file if the stack is not empty
report an error
8
Algorithm in practice
list[i] = 3 * ( 44 - method( foo( list[ 2 * (i + 1) +
foo( list[i - 1] ) ) / 2 *) - list[ method(list[0])];

Processing a file
Tokenization: the process of scanning an input
stream. Each independent chunk is a token.
Tokens may be made up of 1 or more
characters

9
Mathematical Calculations
What is 3 + 2 * 4? 2 * 4 + 3? 3 * 2 + 4?
The precedence of operators affects the
order of operations. A mathematical
expression cannot simply be evaluated left to
right.
A challenge when evaluating a program.
Lexical analysis is the process of interpreting
a program.
Involves Tokenization

What about 1 - 2 - 4 ^ 5 * 3 * 6 / 7 ^ 2 ^ 2
10
Infix and Postfix Expressions
The way we are used to writing expressions
is known as infix notation
Postfix expression does not require any
precedence rules
3 2 * 1 + is postfix of 3 * 2 + 1
evaluate the following postfix expressions
and write out a corresponding infix
expression:
2324*+* 1234^*+
12-32^3*6/+ 25^1-
11
Evaluation of Postfix Expressions
Easy to do with a stack
given a proper postfix expression:
get the next token
if it is an operand push it onto the stack
else if it is an operator
pop the stack for the right hand operand
pop the stack for the left hand operand
apply the operator to the two operands
push the result onto the stack
when the expression has been exhausted the
result is the top (and only element) of the stack
12
Infix to Postfix
Convert the following equations from infix to
postfix:
2^3^3+5*1
233^^51*+
11 + 2 - 1 * 3 / 3 + 2 ^ 2 / 3
11 2 + 1 3 * 3 / - 2 2 ^ 3 / +
Problems:
parentheses in expression

13
Infix to Postfix Conversion
Requires operator precedence parsing algorithm
parse v. To determine the syntactic structure of a
sentence or other utterance
Operands: add to expression
Close parenthesis: pop stack symbols until an open
parenthesis appears
Operators:
Pop all stack symbols until a symbol of lower
precedence appears. Then push the operator
End of input: Pop all remaining stack symbols and
add to the expression
14
Simple Example
Infix Expression: 3+2*4
PostFix Expression:
Operator Stack:

15
Simple Example
Infix Expression: +2*4
PostFix Expression: 3
Operator Stack:

16
Simple Example
Infix Expression: 2*4
PostFix Expression: 3
Operator Stack: +

17
Simple Example
Infix Expression: *4
PostFix Expression: 32
Operator Stack: +

18
Simple Example
Infix Expression: 4
PostFix Expression: 32
Operator Stack: +*

19
Simple Example
Infix Expression:
PostFix Expression: 324
Operator Stack: +*

20
Simple Example
Infix Expression:
PostFix Expression: 324*
Operator Stack: +

21
Simple Example
Infix Expression:
PostFix Expression: 324*+
Operator Stack:

22
Example
1-2^3^3-(4+5*6)*7
Show algorithm in action on above equation

23
Applications of Stacks
Direct applications
Page-visited history in a Web browser
Undo sequence in a text editor
Chain of method calls in the Java Virtual
Machine
Validate XML
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures

24
Method Stack in the JVM

The Java Virtual Machine (JVM) main() {


keeps track of the chain of int i = 5; bar
active methods with a stack foo(i); PC = 1
When a method is called, the } m=6
JVM pushes on the stack a
frame containing foo(int j) {
foo
Local variables and return value int k; PC = 3
Program counter, keeping track of k = j+1; j=5
the statement being executed bar(k); k=6
When a method ends, its frame }
is popped from the stack and main
control is passed to the method bar(int m) { PC = 2
on top of the stack i=5
Allows for recursion }
25
Implementing a stack
need an underlying collection to hold the elements
of the stack
2 basic choices
array (native or ArrayList)
linked list
array implementation

linked list implementation

Some of the uses for a stack are much more


interesting than the implementation of a stack
26
Array-based Stack

A simple way of Algorithm size()


implementing the return t + 1
Stack ADT uses an
array Algorithm pop()
We add elements if isEmpty() then
from left to right throw EmptyStackException
A variable keeps else
track of the index of tt1
the top element return S[t + 1]


S
0 1 2 t
27
Queues

28
Queues
Closely related to Stacks
Like a line
In Britain people dont get in line they queue
up.
Queues are a first in first out data structure
FIFO (or LILO, but that sounds a bit silly)
Add items to the end of the queue
Access and remove from the front
Used extensively in operating systems
Queues of processes, I/O requests, and much
more
29
Queue operations
add(Object item)
a.k.a. enqueue(Object item)
Object get()
a.k.a. Object front()
Object remove()
a.k.a. Object dequeue()
boolean isEmpty()
Specify in an interface, allow varied
implementations

30
Queue Example

Operation Output Q
enqueue(5) (5)
enqueue(3) (5, 3)
dequeue() 5 (3)
enqueue(7) (3, 7)
dequeue() 3 (7)
front() 7 (7)
dequeue() 7 ()
dequeue() error ()
isEmpty() true ()
enqueue(9) (9)
enqueue(7) (9, 7)
size() 2 (9, 7)
enqueue(3) (9, 7, 3)
enqueue(5) (9, 7, 3, 5)
dequeue() 9 (7, 3, 5)

31
Applications of Queues
Direct applications
Waiting lists, bureaucracy
Access to shared resources (e.g., printer)
Multiprogramming
Indirect applications
Auxiliary data structure for algorithms
Component of other data structures

32
Implementation
Array-based Queue
Use an array of size N in a circular fashion
Two variables keep track of the front and rear
f index of the front element
r index immediately past the rear element
Array location r is kept empty

normal configuration
Q
0 1 2 f r

wrapped-around configuration
Q
0 1 2 r f
33
Queue Operations

We use the Algorithm size()


modulo operator return (N f + r) mod N
(remainder of
Algorithm isEmpty()
division) return (f r)

Q
0 1 2 f r
Q
0 1 2 r f

34
Queue Operations (cont.)

Operation enqueue Algorithm enqueue(o)


throws an exception if if size() = N 1 then
the array is full throw FullQueueException
This exception is else
implementation- Q[r] o
dependent r (r + 1) mod N

Q
0 1 2 f r
Q
0 1 2 r f

35
Queue Operations (cont.)

Operation dequeue Algorithm dequeue()


throws an exception if isEmpty() then
if the queue is empty throw EmptyQueueException
This exception is else
specified in the o Q[f]
queue ADT f (f + 1) mod N
return o

Q
0 1 2 f r
Q
0 1 2 r f
36
Queue Interface in Java

public interface Queue {


Java interface
corresponding to public int size();
our Queue ADT
public boolean isEmpty();
Requires the
definition of class public Object front()
EmptyQueueException throws EmptyQueueException;
No corresponding
public void enqueue(Object o);
built-in Java class,
however there is public Object dequeue()
PriorityQueue throws EmptyQueueException;
}

37
Application: Round Robin
Schedulers

We can implement a round robin scheduler using a


queue, Q, by repeatedly performing the following
steps:
1. e = Q.dequeue()
2. Service element e
3. Q.enqueue(e)

The Queue

1. Deque the 2. Service the 3. Enqueue the


next element next element serviced element

Shared
Service

38
Implementing a Queue
Given the internal storage container and
choice for front and back of queue what are
the Big O of the queue operations?
ArrayList LinkedList LinkedList
(Singly Linked) (Doubly Linked)
enqueue
front
dequeue
isEmpty

39
Priority Queue Implementation
Sequence-based Priority Queue
Implementation with an Implementation with a
unsorted list sorted list
4 5 2 3 1 1 2 3 4 5

Performance: Performance:
insert takes O(1) time insert takes O(n) time
since we can insert the since we have to find the
item at the beginning or place where to insert the
end of the sequence item
removeMin and min take removeMin and min take
O(n) time since we have
to traverse the entire O(1) time, since the
sequence to find the smallest key is at the
smallest key beginning

40
Priority Queue Applications
Priority-based OS process scheduler

41

You might also like