Professional Documents
Culture Documents
01 DiapositivasBolsasColasPilas PDF
01 DiapositivasBolsasColasPilas PDF
01 DiapositivasBolsasColasPilas PDF
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 BAGS, QUEUES, AND STACKS
‣ stacks
‣ resizing arrays
‣ queues
‣ generics
‣ iterators
‣ applications
Stack API
push pop
5
Stack test client
% more tobe.txt
to be or not to - be - - that - - - is
6
Stack: linked-list representation
StdIn StdOut
to
to
null
be
be to
first null
insert at front or
or be
of linked list to
null
not
not or
be
to
null
to
to not
or
be
to
null
- to not
or
be
to
remove from front null
of linked list be
be not
or
be
to
null
not
- be or
be
to
null
or
- not be
to
null
that
that or
be 7
to
null
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
0 1 2 3 4 5 6 7 8 9
N capacity = 10
decrement N;
then use to index into array
13
Stack considerations
14
1.3 BAGS, QUEUES, AND STACKS
‣ stacks
‣ resizing arrays
‣ queues
‣ generics
‣ iterators
‣ applications
1.3 BAGS, QUEUES, AND STACKS
‣ stacks
‣ resizing arrays
‣ queues
‣ generics
‣ iterators
‣ applications
Stack: resizing-array implementation
First try.
・push(): increase size of array s[] by 1.
・pop(): decrease size of array s[] by 1.
Too expensive.
・Need to copy all items to a new array.
・Inserting first N items takes time proportional to 1 + 2 + … + N ~ N 2 / 2.
public ResizingArrayStackOfStrings()
{ s = new String[1]; }
128
cost (array accesses)
64
red dots give cumulative average 3
0
0 number of push() operations 128
Amortized cost of adding to a Stack
19
Stack: resizing-array implementation
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
20
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.
a[]
push() pop() N a.length
0 1 2 3 4 5 6 7
0 1 null
to 1 1 to
be 2 2 to be
or 3 4 to be or null
not 4 4 to be or not
to 5 8 to be or not to null null null
- to 4 8 to be or not null null null null
be 5 8 to be or not be null null null
- be 4 8 to be or not null null null null
- not 3 8 to be or null null null null null
that 4 8 to be or that null null null null
- that 3 8 to be or null null null null null
- or 2 4 to be null null
- be 1 2 to null
is 2 2 to is
22
Stack resizing-array implementation: performance
construct 1 1 1
push 1 N 1
pop 1 N 1
doubling and
halving operations
size 1 1 1
23
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
Node interchangeably.
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
28
Queue: linked-list representation
StdIn StdOut
to
to
null
to
be be
first null last
to insert at end
or be
or
null
of linked list
to
not be
or
not
null
to
to be
or
not
to
null
be
- to or
not
to
remove from front null
of linked list be be
or
not
to
be
null
or
- be not
to
be
null
not
- or to
be
29
null
Queue dequeue: linked-list implementation
first = first.next;
last
inner class
first to
be
or
private class Node
null
{
last
String item; first
to
Node next; be
or
} null
oldlast
last
first to
be
or
null
oldlast.next = last;
oldlast
last
first to
be
or
not
null
q[] null null the best of times null null null null
0 1 2 3 4 5 6 7 8 9
33
1.3 BAGS, QUEUES, AND STACKS
‣ stacks
‣ resizing arrays
‣ queues
‣ generics
‣ iterators
‣ applications
1.3 BAGS, QUEUES, AND STACKS
‣ stacks
‣ resizing arrays
‣ queues
‣ generics
‣ iterators
‣ applications
Parameterized stack
We implemented: StackOfStrings.
We also want: StackOfURLs, StackOfInts, StackOfVans, ….
36
Parameterized stack
We implemented: StackOfStrings.
We also want: StackOfURLs, StackOfInts, StackOfVans, ….
run-time error
37
Parameterized stack
We implemented: StackOfStrings.
We also want: StackOfURLs, StackOfInts, StackOfVans, ….
39
Generic stack: array implementation
40
Generic stack: array implementation
the way it is
41
Unchecked cast
% javac FixedCapacityStack
Note: FixedCapacityStack.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
42
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.
43
1.3 BAGS, QUEUES, AND STACKS
‣ stacks
‣ resizing arrays
‣ queues
‣ generics
‣ iterators
‣ applications
1.3 BAGS, QUEUES, AND STACKS
‣ stacks
‣ resizing arrays
‣ queues
‣ generics
‣ iterators
‣ applications
Iteration
i N
0 1 2 3 4 5 6 7 8 9
first current
46
Iterators
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 ?
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
49
Bag API
boolean contains(Item item) does the list contain the given item?
...
java.util.Stack.
Takes ~ c1 N 2 seconds.
・Kenny: create a java.util.ArrayList of N 2 closed sites.
Pick an index at random and delete.
open
site
Takes ~ c2 N 4 seconds. open site connected to top
no open site connected to
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.
・...
56
Function calls
57
it is easy to convince yourself that it computes the proper value: any time the algo-
rithm encounters a subexpression consisting of two operands separated by an op-
Arithmetic expressionerator,
evaluation
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 * 5 ) )For)example, the algorithm com-
value. 1
putes the same value of all of these expres- + ((2+3)*(4*5)))
sions: 1 2
operand operator
+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
(fully parenthesized)
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
operand operator
59
Arithmetic expression evaluation
60
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