Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
pop
queue
enqueue dequeue
Stack. Examine the item most recently added. LIFO = "last in first out"
Queue. Examine the item least recently added. FIFO = "first in first out"
2
Client, implementation, interface
Benefits.
3
1.3 B AGS , Q UEUES, AND S TACKS
‣ stacks
‣ resizing arrays
‣ queues
Algorithms
‣ generics
push pop
5
How to implement a stack with a linked list?
top of stack
top of stack
6
Stack: linked-list implementation
top of stack
first
7
Stack pop: linked-list implementation
first or
be
to
null
first.item = "not";
first.next = oldfirst;
first not
or
be
to
null
10
Stack: linked-list implementation performance
11
How to implement a fixed-capacity stack with an array?
top of stack
0 1 2 3 4 5 6 7 8 9
top of stack
12
Fixed-capacity stack: array implementation
0 1 2 3 4 5 6 7 8 9
N capacity = 10
13
Fixed-capacity stack: array implementation
decrement N;
then use to index into array
14
Stack considerations
15
1.3 B AGS , Q UEUES, AND S TACKS
‣ stacks
‣ resizing arrays
‣ queues
Algorithms
‣ generics
First try.
・push(): increase size of array s[] by 1.
・pop(): decrease size of array s[] by 1.
Too expensive. infeasible for large N
"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
19
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.
construct 1 1 1
push 1 N 1
pop 1 N 1
doubling and
halving operations
size 1 1 1
21
Stack resizing-array implementation: memory usage
Tradeoffs. Can implement a stack with either resizing array or linked list;
save a link to the list
client can use
Nodeinterchangeably.
oldfirst = first; Which one is better?
oldfirst
Linked-list implementation.
first or
be
・ to
Every operation takes constant time
null
in the worst case.
・Usescreate
extra time and space to deal with the links.
a new node for the beginning
first.next = oldfirst;
first not
or
be
to
null
enqueue
dequeue
25
How to implement a queue with a linked list?
B.
times of best the was it null
26
Queue: linked-list implementation
first last
27
Queue dequeue: linked-list implementation
first = first.next;
inner class last
first to
private class Node be
or
{ null
oldlast
last
first to
be
or
null
oldlast.next = last;
oldlast
last
first to
be
or
not
null
0 1 2 3 4 5 6 7 8 9
31
Queue: resizing-array implementation
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?
32
1.3 B AGS , Q UEUES, AND S TACKS
‣ stacks
‣ resizing arrays
‣ queues
Algorithms
‣ generics
We implemented: StackOfStrings.
We also want: StackOfURLs, StackOfInts, StackOfVans, ….
34
Parameterized stack
We implemented: StackOfStrings.
We also want: StackOfURLs, StackOfInts, StackOfVans, ….
run-time error
35
Parameterized stack
We implemented: StackOfStrings.
We also want: StackOfURLs, StackOfInts, StackOfVans, ….
37
Generic stack: array implementation
38
Generic stack: array implementation
the way it is
39
Unchecked cast
% javac FixedCapacityStack.java
Note: FixedCapacityStack.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
40
Generic data types: autoboxing
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.
41
1.3 B AGS , Q UEUES, AND S TACKS
‣ stacks
‣ resizing arrays
‣ queues
Algorithms
‣ generics
i N
0 1 2 3 4 5 6 7 8 9
first current
43
Iterators
java.lang.Iterable interface
Q. What is an Iterable ?
public interface Iterable<Item>
A. Has a method that returns an Iterator. {
Iterator<Item> iterator();
}
Q. What is an Iterator ?
java.util.Iterator interface
A. Has methods hasNext() and next().
public interface Iterator<Item>
{
boolean hasNext();
Item next();
optional; use
Q. Why make data structures Iterable ? void remove(); at your own risk
}
A. Java supports elegant client code.
import java.util.Iterator;
first current
import java.util.Iterator;
i N
0 1 2 3 4 5 6 7 8 9
46
Iteration: concurrent modification
concurrent modification
Q. How to detect?
A.
・Count total number of push() and pop() operations in Stack.
・Save counts in *Iterator subclass upon creation.
・If, when calling next() and hasNext(), the current counts do not equal
the saved counts, throw exception.
47
1.3 B AGS , Q UEUES, AND S TACKS
‣ stacks
‣ resizing arrays
‣ queues
Algorithms
‣ generics
boolean contains(Item item) does the list contain the given item?
...
java.util.Stack.
50
Java collections library
java.util.Stack.
Kenny
・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.
・...
53
Function calls
54
it is easy to convince yourself that it computes the proper value: any time the algo-
Arithmetic expressionerator,
evaluation
rithm encounters a subexpression consisting of two operands separated by an op-
all surrounded by parentheses, it leaves the result of performing that opera-
tion on those operands on the operand stack. The result is the same as if that value
had appeared in the input instead of the sub-
Goal. Evaluate infix expressions.
expression, so we can think of replacing valuethe
stack
subexpression by the value to get an expression
operator stack (1+((2+3)*(4*5)))
that would yield the same result. We can apply 1
this argument again and again until we get a +((2+3)*(4*5)))
( 1 + ( ( 2 + 3 ) * ( single
4 * 5value. ) )For) example, the algorithm com-
1
putes the same value of all of these expres- + ((2+3)*(4*5)))
sions:
operand operator 1 2
+3)*(4*5)))
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) ) +
( 1 + ( 5 * ( 4 * 5 ) ) ) 1 2
( 1 + ( 5 * 20 ) ) + + 3)*(4*5)))
( 1 + 100 )
101 1 2 3
Two-stack algorithm. [E. W. Dijkstra] + + )*(4*5)))
(PROGRAM 4.3.5) is an implemen-
Evaluate
・ Value: push onto the
tationvalue stack.This code is a simple
of this method.
example of an interpreter : a program that in-
1 5
+ *(4*5)))
(fully parenthesized)
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
operand operator
56
Arithmetic expression evaluation
57
Correctness
Q. Why correct?
A. When algorithm encounters an operator surrounded by two values
within parentheses, it leaves the result on the value stack.
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
( 1 + ( 5 * ( 4 * 5 ) ) )
( 1 + ( 5 * 20 ) )
( 1 + 100 )
101
( 1 ( ( 2 3 + ) ( 4 5 * ) * ) + )
1 2 3 + 4 5 * * + Jan Lukasiewicz