Lecture 19
Professor Devin Balkcom
devin@cs.dartmouth.edu
office: Sudikoff 211
Current minimum threshholds for grades:
Data structures:
How does this work? Soon, we'll see graph search.
Python lists and linked lists provide the same operations: insert, delete, append, find, replace.
Lists are our first example of an abstract data type: a set of operations, but no specified implementation. Python lists and linked lists are implementations of the 'list' abstract data type.
Different implementations of lists:
Worst cases:
Naive $\Theta(n)$ approach to appending:
Summary: shed full, build new shed one log larger.
Smarter approach to append: plan for the future.
If out of space:
Each list has two sizes:
Cost of appending many items, with capacity-doubling?
(Amortized run-time analysis.)
Add 32 items to a (full) list of size 32:
| items | capacity | cheap appends | cost |
| 32 | 32 | 0 | 32 |
| 32 | 64 | 32 | 1 |
The cost was 64, if we charge 1 for a copy or write.
Cost per item was 2.
Add 64 items to a (full) list of size 64:
| items | capacity | cheap appends | cost |
| 64 | 64 | 0 | 64 |
| 64 | 128 | 64 | 1 |
The cost was 128, if we charge 1 for a copy or write. Cost per item was 2.
In general, the cost of appending $n$ items is $\Theta(n)$. Amortized, the cost of appending $1$ item is $\Theta(1)$.
Every farmer and every farmer's boy ought to be able to splice a rope, make a rope halter, and tie all the useful knots known to the sailor. To splice a rope is a simple matter, but to teach the art on paper is quite another thing. (J.M.Drew, St. Paul Publishing Company 1918.)
Main idea: objects can contain references to other objects.
A node is a simple object that contains data and a reference to the next node. (Node volunteers?)
A linked list is referred to using the address of the first node, the head node. (node_example.py)
Doubly-linked circular list with sentinel:
(look at init method)
OMG WHAT IS self.sentinel.next = self.sentinel OMG?
Insertion near beginning of Python list is $\Theta(n)$. How about for linked lists? (Volunteers, again.)
Deletion near beginning of Python list is $\Theta(n)$. How about for linked lists?
"find()" method in: sentinel_DLL.py
(Note clever use of sentinel.)