Professional Documents
Culture Documents
‣ stacks
‣ resizing arrays
‣ queues
‣ generics
h t t p : / / a l g s 4. c s . p r i n c e t o n . e d u
Stack API
push pop
2
Sample client
top of stack
0 1 2 3 4 5 6 7 8 9
5
Fixed-capacity stack: array implementation
decrement N;
then use to index into array
6
Stack considerations
7
STACKS AND QUEUES
‣ stacks
‣ resizing arrays
‣ queues
‣ generics
h t t p : / / a l g s 4. c s . p r i n c e t o n . e d u
Stack: resizing-array implementation
First try.
・push(): increase size of array s[] by 1.
・pop(): decrease size of array s[] by 1.
"repeated doubling"
Q. How to grow array?
A. If array is full, create a new array of twice the size, and copy items.
public ResizingArrayStackOfStrings()
{ s = new String[1]; }
First try.
・push(): double size of array s[] when array is full.
・pop(): halve size of array s[] when array is one-half full.
N = 4 to be or not
N = 4 to be or not
12
Stack: resizing-array implementation
Efficient solution.
・push(): double size of array s[] when array is full.
・pop(): halve size of array s[] when array is one-quarter full.
14
Stack: linked-list implementation
top of stack
first
18
Stack: linked-list representation
push("to")
push("be") first
insert at front
push("or")
of linked list
push("not")
push("to")
pop()
remove from front
of linked list
push("be")
pop()
pop()
19
Stack pop: linked-list implementation
inner class
20
Stack push: linked-list implementation
inner class
21
Stack: linked-list implementation in Java
22
Stack: linked-list implementation performance
23
Stack implementations: resizing array vs. linked list
Tradeoffs. Can implement a stack with either resizing array or linked list;
client can use interchangeably. Which one is better?
Linked-list implementation.
・Every operation takes constant time in the worst case.
・Uses extra time and space to deal with the links.
Resizing-array implementation.
・Every operation takes constant amortized time.
・Less wasted space.
24
1.3 B AGS, Q UEUES, AND
STACKS
‣ stacks
‣ resizing arrays
‣ queues
‣ generics
h t t p : / / a l g s 4. c s . p r i n c e t o n . e d u
Queue API
enqueue
dequeue
26
Queue test client
first last
28
Queue: linked-list representation
enqueue("to")
enqueue("be")
insert at end
enqueue("or")
of linked list
enqueue("not")
enqueue("to")
dequeue()
remove from front
of linked list enqueue("be")
dequeue()
first
dequeue()
last
29
Queue dequeue: linked-list implementation
inner class
inner class
31
Queue: linked-list implementation in Java
q[] null null the best of times null null null null
0 1 2 3 4 5 6 7 8 9
Q. How to resize?
33
1.3 B AGS, Q UEUES, AND
STACKS
‣ stacks
‣ resizing arrays
‣ queues
‣ generics
h t t p : / / a l g s 4. c s . p r i n c e t o n . e d u
Parameterized stack
We implemented: StackOfStrings.
We also want: StackOfURLs, StackOfInts, StackOfVans, ….
35
Parameterized stack
We implemented: StackOfStrings.
We also want: StackOfURLs, StackOfInts, StackOfVans, ….
run-time error
36
Parameterized stack
We implemented: StackOfStrings.
We also want: StackOfURLs, StackOfInts, StackOfVans, ….
38
Generic stack: array implementation
the way it is
Wrapper type.
・Each primitive type has a wrapper object type.
・Ex: Integer is wrapper type for int.
Bottom line. Client code can use generic stack for any type of data.
44