Professional Documents
Culture Documents
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
{},[],()
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
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
Q
0 1 2 f r
Q
0 1 2 r f
34
Queue Operations (cont.)
Q
0 1 2 f r
Q
0 1 2 r f
35
Queue Operations (cont.)
Q
0 1 2 f r
Q
0 1 2 r f
36
Queue Interface in Java
37
Application: Round Robin
Schedulers
The Queue
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