Linked List I
Two data structure to store a list of things
- an array
- a linked list
Array
- Advantage: fast access of each item
- Disadvantages
- Insert an item at the beginning or middle of an array - the complexity proportional to the length of the array
- Fixed length - need to allocate a whole new array and move all items
Linked List
public class ListNode { // ListNode is a recursive type
public int item;
public ListNode next; // Here we're using ListNode before
} // we've finished declaring it.
Let's make some ListNodes.
ListNode l1 = new ListNode(), l2 = new ListNode(), l3 = new ListNode();
l1.item = 7;
l2.item = 0;
l3.item = 6;
------------- ------------- -------------
| ----- | | ----- | | ----- |
| item| 7 | | | item| 0 | | | item| 6 | |
l1-->| ----- | l2-->| ----- | l3-->| ----- |
| ----- | | ----- | | ----- |
| next| ? | | | next| ? | | | next| ? | |
| ----- | | ----- | | ----- |
------------- ------------- -------------
Link them together.
l3.next = null; // Good habit: always initialized the memory!
//(although Java will set it to null, C/C++ won't!)
------------- ------------- -------------
| ----- | | ----- | | ----- |
| item| 7 | | | item| 0 | | | item| 6 | |
l1-->| ----- | l2-->| ----- | l3-->| ----- |
| ----- | | ----- | | ----- |
| next| .-+-+-------->| next| .-+-+-------->| next| X | |
| ----- | | ----- | | ----- |
------------- ------------- -------------
Add constructors to the ListNode class.
public ListNode(int i, ListNode n) {
item = i;
next = n;
}
public ListNode(int i) {
this(i, null); // for the last node
}
These constructors allow us to emulate Scheme's "cons" operation.
ListNode l1 = new ListNode(7, new ListNode(0, new ListNode(6)));
Linked lists vs. array lists
Advantages of linked lists
- Inserting item into middle of linked list takes constant time if you have a ref to previous node.
- List can keep growing until memory runs out.
Insert a new item:
public void insertAfter(int item){
next = new ListNode(item, next);
// second `next`: old value of next
// first `next`: new value of next
}
l2.insertAfter(3);
/*
* this -> l2 (the box of l2 copied to this)
* create a new node, copy the next from l2 to the new
* l2.next links to the new node
*/
------------- ------------- ------------- -------------
| ----- | | ----- | | ----- | | ----- |
| item| 7 | | | item| 0 | | | item| 3 | | | item| 6 | |
l1-->| ----- | l2-->| ----- | | ----- | l3-->| ----- |
| ----- | | ----- | | ----- | | ----- |
| next| .-+-+------>| next| .-+-+-->| next| .-+-+------>| next| X | |
| ----- | | ----- | | ----- | | ----- |
------------- ------------- ------------- -------------
Disadvantage: Finding the nth item takes time proportional to n.
Lists of Objects
- Reference any object.
public class SListNode {
// S: singly linked list
public Object item;
public SListNode next;
}
A List class
Problems with SListNodes
- The other pointers won't be updated when there is a new node inserted at the beginning of the list.
- How do you represent an empty list?
Solution: Separate SList class to maintain the head of the list.
public class SList {
private SListNode head;
private int size;
public SList() {
head = null;
size = 0;
}
public void insertFront(Object item) {
head = new SListNode(item, head);
size++;
}
}
SList object SListNode object
------------- ------------- String object
----- | ----- | | ----- | ----------
x | .-+----->| size| 1 | | | item| .-+-+---->| milk |
----- | ----- | | ----- | ----------
----- | ----- | | ----- |
y | .-+----->| head| .-+-+-------------------->| next| X | |
----- | ----- | | ----- |
------------- -------------
After x.insertFront("fish")
SList SListNode SListNode
------------- ------------- -------------
----- | ----- | | ----- | -------- | ----- | --------
x | .-+-->| size| 2 | | | item| .-+-+->| fish | | item| .-+-+->| milk |
----- | ----- | | ----- | -------- | ----- | --------
----- | ----- | | ----- | | ----- |
y | .-+-->| head| .-+-+-->| next| .-+-+----------->| next| X | |
----- | ----- | | ----- | | ----- |
------------- ------------- -------------
Another advantage: size
can be determined quickly.