Beruflich Dokumente
Kultur Dokumente
CSE 2011
Winter 2007
Stacks: LIFO
z Insertions and deletions follow the Last-In First-Out rule
z Applications, examples:
Undo operation in a text editor
History of visited web pages
Sequence of method calls in Java
Stack ADT
z Data stored: arbitrary objects
z Operations:
push(object): inserts an element
object pop(): removes and returns the last
inserted element
z Other useful operations:
object top(): returns the last inserted element
without removing it
3
Error Conditions
z
z
z
z
push(object)
object pop()
object top()
Exceptions are thrown when an operation cannot be
executed
z Execution of pop() or top() on an empty stack
throws StackEmptyException
z Another useful operation:
boolean isEmpty(): returns true if the stack is empty;
false otherwise.
4
Stack Operations
z
z
z
z
push(object)
object pop()
object top()
boolean isEmpty()
Stack Interface
public interface Stack {
public int size();
public boolean isEmpty();
public Object top()
throws EmptyStackException;
public void push(Object o);
public Object pop()
throws EmptyStackException;
}
6
Array-based Implementation
z An array S of maximum size N
z A variable t that keeps track of the top element in array S
z Top element: S[t]
z Stack is empty: ?
z Number of elements in the stack: ?
Pseudo-code
Algorithm size():
return (t + 1);
Algorithm isEmpty():
return (t < 0);
Algorithm top():
if (isEmpty())
throw StackEmptyException;
return S[t];
Algorithm pop():
if (isEmpty())
throw StackEmptyException;
temp = S[t];
t = t 1;
return temp;
Optimization: set S[t] to null
before decrementing t
Homework: implement pop()
without any temp variable
8
Method push()
Algorithm push(object):
t = t + 1;
S[t] = object;
Algorithm push(object):
if (size() == N)
throw StackFullException;
t = t + 1;
S[t] = object;
11
12
13
14
Queues
CSE 2011
Winter 2007
15
Queues: FIFO
z Insertions and removals follow the Fist-In First-Out rule:
Insertions: at the rear of the queue
Removals: at the front of the queue
z Applications, examples:
Waiting lists
Access to shared resources (e.g., printer)
Multiprogramming (UNIX)
16
Queue ADT
z Data stored: arbitrary objects
z Operations:
enqueue(object): inserts an element at the end of the
queue
object dequeue(): removes and returns the element at
the front of the queue
object front(): returns the element at the front
without removing it
z Execution of dequeue() or front() on an empty queue
throws QueueEmptyException
z Another useful operation:
boolean isEmpty(): returns true if the queue is empty;
false otherwise.
17
Queue Operations
z
z
z
z
z
enqueue(object)
object dequeue()
object front()
boolean isEmpty()
int size(): returns the
number of elements in
the queue
Array-based Implementation
z An array Q of maximum size N
z Need to keep track the front and rear of the queue:
f: index of the front object
r: index immediately past the rear element
z Note: Q[r] is empty (does not store any object)
19
Array-based Implementation
z
z
z
z
20
10
temp = Q[f];
f = f + 1;
return temp;
Algorithm enqueue(object):
if (r == N)
throw QueueFullException;
Q[r] = object;
r = r + 1;
21
z Analogy:
A snake chases its tail
z Front element: Q[f]
Rear element: Q[r 1]
z Incrementing f, r
f = (f + 1) mod N
r = (r + 1) mod N
mod: Java operator %
22
11
z Queue size =
(N f + r) mod N
verify this
z Queue is empty: f = r
z When r reaches and
overlaps with f, the queue
is full: r = f
z To distinguish between
empty and full states, we
impose a constraint: Q
can hold at most N 1
objects (one cell is
wasted). So r never
overlaps with f, except
when the queue is empty.
23
Pseudo-code
Algorithm enqueue(object):
if (size() == N 1)
throw QueueFullException;
Q[r] = object;
r = (r + 1) mod N;
Algorithm dequeue():
if (isEmpty())
throw QueueEmptyException;
temp = Q[f];
f = (f + 1) mod N;
return temp;
24
12
Pseudo-code
Algorithm front():
if (isEmpty())
throw QueueEmptyException;
return Q[f];
Algorithm isEmpty():
return (f = r);
Algorithm size():
return ((N f + r) mod N);
13
27
14
29
30
15
31
32
16
Upcoming Topics
z The adapter design pattern
z Applications of stacks and queues
z Extendable arrays
z Iterators, listIterators
z Lower bound for sorting (after Trees)
z When to use which sorting algorithm?
z Quick sort with duplicate elements
34
17