Professional Documents
Culture Documents
Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 20022010 6/26/10 8:10 AM
Data Types and Data Structures
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, . . .
2
Collections
3
Stacks
4
Stack API
push
pop
5
Stack Client Example 1: Reverse
6
Stack Client Example 2: Test Client
7
Stack: Array Implementation
a[] to be or not
0 1 2 3 4 5 6 7 8 9
N
8
Array Stack: Test Client Trace
push
pop
9
Array Stack: Performance
Problem.
API does not call for capacity (bad to change API).
Client might use multiple stacks.
Client might not know what capacity to use.
10
Linked Lists
11
Sequential vs. Linked Allocation
C0 "Alice" C0 "Carol"
C1 "Bob" C1 null
C2 "Carol" C2 -
C3 - C3 -
Key distinctions. get ith element
C4 - C4 "Alice"
C9 - C9 -
CA - CA "Bob"
CB - CB C0
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.
first
15
Building a Linked List
addr Value
Node third = new Node();
C0 "Carol"
third.item = "Carol";
third.next = null; C1 null
C7 -
C8 -
C9 -
CA "Bob"
CB C0
CC -
first second third
CD -
CE -
Alice Bob Carol null
CF -
item next
main memory
16
Stack Push: Linked List Implementation
first
first second
first second
first second
17
Stack Pop: Linked List Implementation
first "of"
first
garbage-collected
first
18
Stack: Linked List Implementation
push
pop
20
Linked List Stack: Performance
21
Stack Data Structures: Tradeoffs
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.
22
List Processing Challenge 1
first
23
List Processing Challenge 2
last
first
24
Parameterized Data Types
25
Parameterized Data Types
We implemented: StackOfStrings.
26
Generics
sample client
cant push an orange onto
a stack of apples
27
Generic Stack: Linked List Implementation
Wrapper type.
Each primitive type has a wrapper reference type.
29
Stack Applications
30
Function Calls
31
Arithmetic Expression Evaluation
value stack
Goal. Evaluate infix expressions. operator stack
operand operator
Context. An interpreter!
32
Arithmetic Expression Evaluation
33
Correctness
( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )
( 1 + ( 5 * ( 4 * 5 ) ) )
1 + (2 - 3 - 4) * 5 * sqrt(6*6 + 7*7)
34
Stack-Based Programming Languages
( 1 ( ( 2 3 + ) ( 4 5 * ) * ) + )
1 2 3 + 4 5 * * +
Jan Lukasiewicz
Bottom line. Postfix or "reverse Polish" notation.
35
Queues
36
Queue API
enqueue dequeue
37
Enqueue: Linked List Implementation
first last
38
Dequeue: Linked List Implementation
first last
first last
garbage-collected
first last
39
Queue: Linked List Implementation
40
Queue Applications
Some applications.
iTunes playlist.
41
M/D/1 Queuing Model
M/D/1 queue.
Customers are serviced at fixed rate of per minute.
42
43
Event-Based Simulation
44
M/D/1 Queue Analysis
.21
= .2, = .25
1
Queueing theory. W = + , L = W
2 ( )
Little's law
45
Summary
Many applications.
46