Lecture 2: List Case Study
CSE 373: Data Structures and Algorithms
1
CSE 373 23SU
CSE 373 23SU
1
Agenda
Quick ADT Review
List Case Study
Questions
CSE 373 23SU
2
Announcements
Things are live!
Project 0 Released – Due Friday June 30th at 11:59PM
Website troubleshooting… please be patient with us :(
CSE 373 23SU
3
Quick ADT Review
List Case Study
Generics
Questions
CSE 373 23SU
4
Review: Full Definitions
CSE 373 23SU
5
ADTs we’ll discuss this quarter
CSE 373 23SU
6
Questions?
CSE 373 23SU
7
Quick ADT Review
List Case Study
Questions
CSE 373 23SU
8
Case Study: The List ADT
list: a collection storing an ordered sequence of elements
Expected Behavior:
List<String> names = new ArrayList<>();
names.append("Anish");
names.append("Amanda");
names.insert(0, "Brian");
CSE 373 23SU
9
Case Study: List Implementations
List ADT
get(index) return item at index
set(item, index) replace item at index
append(item) add item to end of list
insert(item, index) add item at index
delete(index) delete item at index
size() count of items
state
behavior
Set of ordered items
Count of items
ArrayList<E>
get return data[index]
set data[index] = value
append data[size] = value, if out of space grow data
insert shift values to make hole at index, data[index] = value, if out of space grow data
delete shift following values forward
size return size
state
behavior
data[]
size
LinkedList<E>
get loop until index, return node’s value
set loop until index, update node’s value
append create new node, update next of last node
insert create new node, loop until index, update next fields
delete loop until index, skip node
size return size
state
behavior
Node front
size
ArrayList
uses an Array as underlying storage
LinkedList
uses nodes as underlying storage
0 | 1 | 2 | 3 | 4 |
88.6 | 26.1 | 94.4 | 0 | 0 |
88.6
26.1
94.4
list
free space
CSE 373 23SU
10
Implementing Insert
0 | 1 | 2 | 3 |
| | | |
insert(10, 0)
3
4
5
numberOfItems =
3
insert(element, index) with shifting
5
4
3
10
4
insert(10, 0)
numberOfItems =
3
insert(element, index) with shifting
4
3
4
5
10
ArrayList<E>
LinkedList<E>
CSE 373 23SU
11
Implementing Delete
0 | 1 | 2 | 3 |
| | | |
3
4
5
numberOfItems =
3
delete(index) with shifting
delete(0)
10
3
4
5
4
numberOfItems =
3
delete(index) with shifting
delete(0)
4
3
4
5
10
ArrayList<E>
LinkedList<E>
CSE 373 23SU
12
Implementing Append
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| | | | | | | |
0 | 1 | 2 | 3 |
| | | |
append(2)
3
5
numberOfItems =
append(element) with growth
4
10
4
2
5
10
3
4
5
ArrayList<E>
append(2)
numberOfItems =
append(element) with growth
4
5
3
4
5
10
2
LinkedList<E>
CSE 373 23SU
13
Review: Complexity Class
complexity class: A category of algorithm efficiency based on the algorithm's relationship to the input size N.
Complexity Class | Big-O | Runtime if you double N | Example Algorithm |
constant | O(1) | unchanged | Accessing an index of an array |
logarithmic | O(log2 N) | increases slightly | Binary search |
linear | O(N) | doubles | Looping over an array |
log-linear | O(N log2 N) | slightly more than doubles | Merge sort algorithm |
quadratic | O(N2) | quadruples | Nested loops! |
... | ... | ... | ... |
exponential | O(2N) | multiplies drastically | Fibonacci with recursion |
Note: You don’t have to understand all of this right now – we’ll dive into it soon.
CSE 373 23SU
14
List ADT tradeoffs
0 | 1 | 2 | 3 | 4 |
‘h’ | ‘e’ | ‘l’ | ‘l’ | ‘o’ |
‘h’ | |
‘o’ | / |
‘e’ | |
‘l’ | |
‘l’ | |
ArrayList<Character> myArr
front
LinkedList<Character> myLl
Last time: we used “slow” and “fast” to describe running times.
Let’s be a little more precise.
Recall these basic Big-O ideas from 12X: Suppose our list has N elements
For ArrayLists and LinkedLists, what is the O() for each of these operations?
What are the memory tradeoffs for our two implementations?
CSE 373 23SU
15
List ADT tradeoffs
Time needed to access Nth element:
Time needed to insert at Nth element (if the array is full!)
Amount of space used overall/across all elements
Amount of space used per element
O(1) constant time
O(N) linear time
O(N) linear time
O(N) linear time
sometimes wasted space at end of array
compact, one node for each entry
minimal, one element of array
tiny bit extra, object with two fields
0 | 1 | 2 | 3 | 4 |
‘h’ | ‘e’ | ‘l’ | ‘l’ | ‘o’ |
‘h’ | |
‘o’ | / |
‘e’ | |
‘l’ | |
‘l’ | |
ArrayList<Character> myArr
front
LinkedList<Character> myLl
CSE 373 23SU
16
Design Decisions
For every ADT there are lots of different ways to implement them
Based on your situation you should consider:
This class is all about implementing ADTs based on making the right design tradeoffs!
A common topic in interview questions!
CSE 373 23SU
17
A quick aside: Types of memory
int[] array = new int[3];
array[0] = 3;
array[1] = 7;
array[2] = 3;
Node front = new Node(3);
front.next = new Node(7);
front.next.next = new Node(3);
Arrays - contiguous memory: when the “new” keyword is used on an array the operating system sets aside a single, right-sized block of computer memory
0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 1001 |
| d672 | 8baf | 020a | | | | 713f | 04e3 | 2e6e |
3
7
3
Nodes- non-contiguous memory: when the “new” keyword is used on a single node the operating system sets aside enough space for that object at the next available memory location
array
0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 1001 | 1010 |
| 4b44 | | | 052f | d3cd | | | 23d4 | | |
front
3
7
3
More on how memory impacts runtime later in this course…
CSE 373 23SU
18
Design Decisions
Situation #1: Write a data structure that implements the List ADT that will be used to store a list of songs in a playlist.
Features to consider:
Why ArrayList?
Why LinkedList?
CSE 373 23SU
19
Design Decisions
Situation #2: Write a data structure that implements the List ADT that will be used to store the history of a bank customer’s transactions.
Features to consider:
Why ArrayList?
Why LinkedList?
CSE 373 23SU
20
Design Decisions
Situation #3: Write a data structure that implements the List ADT that will be used to store the order of students waiting to speak to a TA at a tutoring center
ArrayList – optimize for addition to back
LinkedList - optimize for removal from front
CSE 373 23SU
21
Real-World Scenarios: Lists
LinkedList
ArrayList
CSE 373 23SU
22
Quick ADT Review
List Case Study
Generics
Questions
CSE 373 23SU
23
Quick ADT Review
List Case Study
Questions?
CSE 373 23SU
24
That’s all!
CSE 373 23SU
25