|
1 |
| -Deques have four fundamental operations (using Array terminology): |
| 1 | +Like an array, a linked list is a simple linear data structure. Several |
| 2 | +common data types can be implemented using linked lists, like queues, |
| 3 | +stacks, and associative arrays. |
2 | 4 |
|
3 |
| -* push (insert value at back) |
4 |
| -* unshift (insert value at front) |
5 |
| -* pop (remove value at back) |
6 |
| -* shift (remove value at front) |
| 5 | +A linked list is a collection of data elements called *nodes*. In a |
| 6 | +*singly linked list* each node holds a value and a link to the next node. |
| 7 | +In a *doubly linked list* each node also holds a link to the previous |
| 8 | +node. |
7 | 9 |
|
8 |
| -The difference between deque and Array performance is that a deque |
9 |
| -implements all of these operations in constant time. Even clever |
10 |
| -implementations of Array will often have to copy the entire Array in |
11 |
| -order to unshift. |
| 10 | +You will write an implementation of a doubly linked list. Implement a |
| 11 | +Node to hold a value and pointers to the next and previous nodes. Then |
| 12 | +implement a List which holds references to the first and last node and |
| 13 | +offers an array-like interface for adding and removing items: |
12 | 14 |
|
13 |
| -Under the hood we'll use an Element class with two fields, `next` and |
14 |
| -`prev`, that are both writable. |
| 15 | +* `push` (*insert value at back*); |
| 16 | +* `pop` (*remove value at back*); |
| 17 | +* `shift` (*remove value at front*). |
| 18 | +* `unshift` (*insert value at front*); |
15 | 19 |
|
16 |
| -To make the API usable, you'll use a Deque class as a proxy for this |
17 |
| -list. There two good ways to implement Deque: maintain separate |
18 |
| -references to the first and last Element, or maintain a reference to one |
19 |
| -of them and ensure that the list is circular. |
| 20 | +To keep your implementation simple, the tests will not cover error |
| 21 | +conditions. Specifically: `pop` or `shift` will never be called on an |
| 22 | +empty list. |
20 | 23 |
|
21 |
| -To keep your implementation simple, the tests will not cover error |
22 |
| -conditions. Specifically: pop or shift will never be called on an empty |
23 |
| -Deque. |
| 24 | +If you want to know more about linked lists, check [Wikipedia](https://en.wikipedia.org/wiki/Linked_list). |
| 25 | + |
| 26 | +--- |
24 | 27 |
|
25 | 28 | In languages that do not have good support for mutability (such as
|
26 |
| -Elixir or Erlang), you may choose to implement a functional Deque where |
| 29 | +Elixir or Erlang), you may choose to implement a functional list where |
27 | 30 | shift and pop have amortized O(1) time. The simplest data structure for
|
28 | 31 | this will have a pair of linked lists for the front and back, where
|
29 | 32 | iteration order is front ++ reverse back.
|
| 33 | + |
| 34 | + |
0 commit comments