Stack Queue
Stack Queue
Stack.
Remove the item most recently added.
Analogy: cafeteria trays, Web surfing.
Queue.
Remove the item least recently added.
Analogy: Registrar's line.
push
pop
enqueue
dequeue
stacks
dynamic resizing
queues
generics
applications
Stacks
Stack operations.
Insert a new item onto stack.
push()
Remove and return the item most recently added.
pop()
isEmpty() Is the stack empty?
push
pop
first
of
best
the
was
it
item = first.item;
best
the
was
it
first = first.next;
best
the
was
it
return item;
first
first
first
best
first
was
it
the
was
it
second = first;
the
was
it
the
was
it
first.item = item;
first.next = second;
second
best
first
the
second
best
first
second
of
best
"inner class"
Error conditions?
Example: pop() an empty stack
COS 217: bulletproof the code
COS 226: first find the code we want to use
10
s[]
it
was
the
best
11
avoid loitering
(garbage collector only reclaims memory
if no outstanding references)
12
stacks
dynamic resizing
queues
generics
applications
13
Too expensive
Need to copy all of the elements to a new array.
Inserting N elements: time proportional to 1 + 2 + + N N2/2.
public StackOfStrings()
{ this(8); }
public void push(String item)
{
if (N >= s.length) resize();
s[N++] = item;
}
15
When (solution): array is 1/4 full (then new array is half full).
public String pop(String item)
{
String item = s[--N];
sa[N] = null;
if (N == s.length/4)
resize(s.length/2);
return item;
}
Consequences.
any sequence of N ops takes time proportional to N
array is always between 25% and 100% full
Not a.length/2
to avoid thrashing
16
Linked list.
Grows and shrinks gracefully.
Every operation takes constant time.
Every operation uses extra space and time to deal with references.
array?
linked list?
18
stacks
dynamic resizing
queues
generics
applications
19
Queues
Queue operations.
enqueue() Insert a new item onto queue.
dequeue() Delete and return the item least recently added.
isEmpty() Is the queue empty?
20
last
first
it
was
the
best
of
item = first.item;
last
first
was
the
best
of
first = first.next;
last
first
was
the
best
of
return item;
Aside:
dequeue (pronounced DQ) means remove from a queue
deque (pronounced deck) is a data structure (see PA 1)
21
last
first
it
was
the
last
first
it
was
the
first
it
was
the
first
it
best
was
the
best
of
last
best
of
last
best
of
x = new Node();
x.item = item;
x.next = null;
last.next = x;
last = x;
22
23
q[]
0
the
best
of
times
head
tail
capacity = 10
stacks
dynamic resizing
queues
generics
applications
25
@#$*! most reasonable approach until Java 1.5 [hence, used in AlgsJava]
26
Stack of Objects
We implemented: StackOfStrings, QueueOfStrings.
We also want: StackOfURLs, QueueOfCustomers, etc?
Attempt 2. Implement a stack with items of type Object.
Casting is required in client.
Casting is error-prone: run-time error if types mismatch.
run-time error
27
Generics
Generics. Parameterize stack by a single type.
Avoid casting in both client and implementation.
Discover type mismatch errors at compile-time instead of run-time.
parameter
Guiding principles.
Welcome compile-time errors
Avoid run-time errors
Why?
28
31
Bottom line: Client code can use generic stack for any type of data
32
stacks
dynamic resizing
queues
generics
applications
33
Stack Applications
Real world applications.
Parsing in a compiler.
Java virtual machine.
Undo in a word processor.
Back button in a Web browser.
PostScript language for printers.
Implementing function calls in a compiler.
34
Function Calls
How a compiler implements functions.
Function call: push local environment and return address.
Return: pop return address and local environment.
operand
operator
Context. An interpreter!
36
37
Correctness
Why correct?
When algorithm encounters an operator surrounded by two values
within parentheses, it leaves the result on the value stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
Observation 2.
All of the parentheses are redundant!
1 2 3 + 4 5 * * +
Jan Lukasiewicz
Basics
%!: I am a PostScript program
literal: push me on the stack
function calls take args from stack
turtle graphics built in
a PostScript program
%!
72 72 moveto
0 72 rlineto
72 0 rlineto
0 -72 rlineto
-72 0 rlineto
2 setlinewidth
stroke
40
Square root of 2:
1.4142
like toString()
%!
/Helvetica-Bold findfont 16 scalefont setfont
72 168 moveto
(Square root of 2:) show
72 144 moveto
2 sqrt 10 string cvs show
41
function definition
function calls
%!
/box
{
/sz exch def
0 sz rlineto
sz 0 rlineto
0 sz neg rlineto
sz neg 0 rlineto
} def
72 144 moveto
72 box
288 288 moveto
144 box
2 setlinewidth
stroke
42
1 1 20
{ 19 mul dup 2 add moveto 72 box }
for
if-else
boolean on stack
alternatives in braces
if operator
%!
72 72 translate
/kochR
{
2 copy ge { dup 0 rlineto }
{
3 div
2 copy kochR 60 rotate
2 copy kochR -120 rotate
2 copy kochR 60 rotate
2 copy kochR
} ifelse
pop pop
} def
0
0 moveto
0 81 moveto
0 162 moveto
0 243 moveto
stroke
81
27
9
1
243
243
243
243
kochR
kochR
kochR
kochR
44
Queue applications
Familiar applications.
iTunes playlist.
Data buffers (iPod, TiVo).
Asynchronous data transfer (file IO, pipes, sockets).
Dispensing requests on a shared resource (printer, processor).
45
Pr[X x] = 1 e x
Arrival rate
Departure rate
Infinite queue
Server
46
47
Littles Law
W =
1
+
,
2 ( )
L = W
wait time W and queue length L approach infinity as service rate approaches arrival rate
48