Click presentation screen icon to the right to see the slide show.
Using references to instances of a Node class to store information.
Java references
Resizable arrays
Reference based linked lists
class Node { String item; Node next; Node (String newItem) { item = newItem; next = null; } Node(String newItem, Node nextNode) { item = newItem; next = nextNode; } } |
|
How would you use class Node { String item; Node next; Node (String newItem) { item = newItem; next = null; } Node(String newItem, Node nextNode) { item = newItem; next = nextNode; } } |
|
How would you use class Node { String item; Node next; Node (String newItem) { item = newItem; next = null; } Node(String newItem, Node nextNode) { item = newItem; next = nextNode; } } |
Node head = null; // empty list head = new Node(new String("Lou")); |
|
How would you use class Node { String item; Node next; Node (String newItem) { item = newItem; next = null; } Node(String newItem, Node nextNode) { item = newItem; next = nextNode; } } |
Node head = null; // empty list head = new Node(new String("Lou")); head = new Node(new String("Mary", head)); head = new Node(new String("Julie", head)); head = new Node(new String("Chuck", head)); |
How would you delete “Julie”?
Node previous = head; // "Chuck" Node current = previous.next; // "Julie", the one to delete current = current.next; // "Mary" ... Now what? ...
How would you delete “Julie”?
Node previous = head; // "Chuck" Node current = previous.next; // "Julie", the one to delete current = current.next; // "Mary" previous.next = current; // "Chuck" next -> "Mary"
// Using while loop Node current = head; while (current != null) { System.out.println(current.item); current = current.next; } // Using for loop for (Node current = head; current != null; current = current.next) { System.out.println(current.item); }
|
class Node { String item; Node next; Node (String newItem) { item = newItem; next = null; } Node(String newItem, Node nextNode) { item = newItem; next = nextNode; } } Node node = new Node(new String("Chuck")); |
|
|
class Node { String item; Node next; Node (String newItem) { item = newItem; next = null; } Node(String newItem, Node nextNode) { item = newItem; next = nextNode; } } Node node = new Node(new String("Chuck")); |
class Node { Object item; Node next; Node (Object newItem) { item = newItem; next = null; } Node(Object newItem, Node nextNode) { item = newItem; next = nextNode; } } Node node = new Node(new MyClass("stuff", 42)); |
|
class Node { Object item; Node next; Node (Object newItem) { item = newItem; next = null; } Node(Object newItem, Node nextNode) { item = newItem; next = nextNode; } } Node node = new Node(new MyClass("stuff", 42)); |
Any particular type |
|
class Node { Object item; Node next; Node (Object newItem) { item = newItem; next = null; } Node(Object newItem, Node nextNode) { item = newItem; next = nextNode; } } Node node = new Node(new MyClass("stuff", 42)); |
Any particular type class Node<Type> { Type item; Node next; Node (Type newItem) { item = newItem; next = null; } Node(Type newItem, Node nextNode) { item = newItem; next = nextNode; } } Node<MyClass> node = new Node<MyClass>(new MyClass("Chuck",42)); |
// Using while loop Node current = head; while (current != null) { System.out.println(current.item); current = current.next; } // Using for loop for (Node current = head; current != null; current = current.next) { System.out.println(current.item); }
How would you change this code?
// Using while loop Node current = head; while (current != null) { System.out.println(current.item); current = current.next; } // Using for loop for (Node current = head; current != null; current = current.next) { System.out.println(current.item); } // Using while loop Node<MyClass> current = head; while (current != null) { System.out.println(current.item); current = current.next; } // Using for loop for (Node<MyClass> current = head; current != null; current = current.next) { System.out.println(current.item); }
| Array | Reference | |
|---|---|---|
| create | Must allocate initial size. Might be too much or too few | Nothing to allocate |
| storage for each element | Just the element. Location of next one is implicit | Element plus reference |
| access element at position | Constant time. Does not depend on . | Time is linear in |
| insert element at position | Must shift elements first. | Must find element , and assign next. No shifting needed. |
| remove element at position | Same as above | Same as above. |
Inserting. There are two cases:
newNode.next = head; head = newNode;
nodenewNode.next = node.next; node.next = newNode;
Inserting. There are two cases:
newNode.next = head; head = newNode;
nodenewNode.next = node.next; node.next = newNode;
Removing. There are two cases:
head = head.next;
current node, with previous assignedprevious.next = current.next;
Inserting:
previous will be dummy node if inserting at front.newNode.next = previous.next; // 1 previous.next = newNode; // 2
Removing:
current node, with previous assigned. previous will be dummy node if removing first node.previous.next = current.next;
previous and current.current.current.previous.next = current.next; // 1 current.next.previous = current.previous; // 2
Rewrite the multiple references.
(current.previous).next = current.next; // 1 ("Chuck").next = "Mary" (current.next).previous = current.previous; // 2 ("Mary").previous = "Chuck";
newNode node after current.newNode.next = current.next; // 1 newNode.previous = current; // 2 current.next = newNode; // 3 newNode.next.previous = newNode; // 4
or, think of it as inserting b between a and c.
c = a.next; a.next = b; b.next = c; b.previous = a; c.previous = b;
Try deleting last element, “Mary”.
Try deleting first element, “Chuck”.
Try inserting a new first element.
Try inserting a new last element.