Beruflich Dokumente
Kultur Dokumente
Data types.
Set of values.
Set of operations on those values.
Some are built in to Java: int, double, char, . . .
Most are not: Complex, Picture, Stack, Queue, Graph,
this lecture
Data structures.
Represent data or relationships among data.
Some are built into Java: arrays, String,
Most are not: linked list, circular list, tree, sparse array, graph, . . .
this lecture
Copyright 20022010
TSP assignment
next lecture
6/26/10 8:10 AM
Collections
Stacks
this lecture
Symbol table.
Remove the item with a given key.
Ex: Phone book.
next lecture
Stack API
push
pop
a[]
% more test.txt
to be or not to be - - that - - - is
to
be
or
not
}
7
push
Problem.
API does not call for capacity (bad to change API).
Client might use multiple stacks.
Client might not know what capacity to use.
pop
10
Linked Lists
addr
value
"Alice"
C0
"Carol"
C1
"Bob"
C1
null
C2
"Carol"
C2
C3
C3
C4
C4
"Alice"
C5
C5
CA
C6
C6
C7
C7
C8
C8
C9
C9
CA
CA
"Bob"
CB
CB
C0
C0
Key distinctions.
Array: random access, fixed size.
Linked list: sequential access, variable size.
get ith element
array
11
linked list
12
Linked Lists
Linked list.
A recursive data structure.
An item plus a pointer to another linked list (or empty list).
Unwind recursion: linked list is a sequence of items.
Node third
third.item
third.next
Node first
first.item
first.next
= new Node();
= "Carol";
= null;
= new Node();
= "Alice";
= second;
Alice
item
Bob
Carol
null
second
Alice
null
C2
C3
"Alice"
second
CA
C5
CA
third
C0
C6
C7
C8
null
Carol
item
C1
C4
third
Bob
"Carol"
C4
next
Value
C0
first
first
first
addr
C9
CA
"Bob"
CB
C0
CC
CD
CE
CF
next
main memory
15
16
first
best
the
was
of
first
best
the
was
it
best
the
was
it
first = first.next;
best
the
was
it
return item;
second
best
the
was
it
first
of
first
"of"
first
it
second
garbage-collected
best
the
was
it
first
second
of
best
the
was
it
first.item = "of";
first.next = second;
17
18
push
pop
20
Array.
Every push/pop operation take constant time.
But must fix maximum capacity of stack ahead of time.
Linked list.
Every push/pop operation takes constant time.
But uses extra space and time to deal with references.
21
22
last
first
first
Alice
item
Bob
Carol
null
Alice
item
next
Bob
Carol
null
next
23
24
We implemented: StackOfStrings.
We also want: StackOfURLs, StackOfInts,
25
26
Generics
stack of apples
parameterized type
sample client
cant push an orange onto
a stack of apples
28
Autoboxing
Stack Applications
Wrapper type.
Each primitive type has a wrapper reference type.
Ex: Integer is wrapper type for int.
29
30
Function Calls
value stack
operator stack
operand
operator
Context. An interpreter!
31
32
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 ) ) )
33
34
Queues
Jan Lukasiewicz
35
36
Queue API
it
Iterator<Key>
iterator()
was
the
best
oldlast
first
it
enqueue
last
was
the
last
best
dequeue
first
it
was
the
first
it
37
was
the
oldlast
last
best
of
oldlast
last
best
of
oldlast.next = last;
38
first
it
last
was
the
best
first
it
of
last
was
the
best
of
first = first.next;
garbage-collected
first
last
was
the
best
of
return item;
}
39
40
Queue Applications
Some applications.
iTunes playlist.
Data buffers (iPod, TiVo).
Asynchronous data transfer (file IO, pipes, sockets).
Dispensing requests on a shared resource (printer, processor).
M/D/1 queue.
Customers are serviced at fixed rate of per minute.
Customers arrive according to Poisson process at rate of per minute.
Pr[X x] = 1 e x
Arrival rate
Departure rate
Infinite queue
Server
41
42
Event-Based Simulation
public class MD1Queue {
public static void main(String[] args) {
double lambda = Double.parseDouble(args[0]);
double mu
= Double.parseDouble(args[1]);
Queue<Double> q = new Queue<Double>();
double nextArrival = StdRandom.exp(lambda);
double nextService = nextArrival + 1/mu;
while(true) {
if (nextArrival < nextService) {
q.enqueue(nextArrival);
nextArrival += StdRandom.exp(lambda);
}
arrival
else {
service
double wait = nextService - q.dequeue();
// add waiting time to histogram
if (q.isEmpty()) nextService = nextArrival + 1/mu;
else
nextService = nextService + 1/mu;
}
43
44
Summary
.21
= .2, = .25
Many applications.
Queueing theory.
W =
1
+
,
2 ( )
L = W
Little's law
45
46