Elements are stored in contiguous memory (issue of allocation)
| Front of List | Middle of List | End of List | |
|---|---|---|---|
| get | O(1) | O(1) | O(1) |
| set | O(1) | O(1) | O(1) |
| insert | O(n) | O(n) | O(1) |
| remove | O(n) | O(n) | O(1) |
A linked list is a chain of nodes, where each node contains:
No need for contiguous memory allocation (nodes can be scattered anywhere in memory)
To create a linked list:
NodeWrite a class in Java called Node that has two instance variables: data (any type), and a reference to another node.
Write the constructor method, and a set and get method for each instance variable.
Object?
Write:
data (type T)getData and getNextsetData and setNextSubmit your Node.java file to gradescope
public class Node<T> {
private T data;
private Node<T> next;
// constructor
public Node(T data) {
this.data = data;
next = null;
}
// setters
public void setNext(Node<T> next) {
this.next = next;
}
public void setData(T data) {
this.data = data;
}
// getters
public Node<T> getNext() {
return next;
}
public T getData() {
return data;
}
}LinkedList classLinkedList classA linked list has a head (reference to a Node)
LinkedList set up and constructorA toString() method to test our code
addNode(Node<T> node) methodWrite a addNode(Node<T> node)
How can we improve on this?
toString() methodBefore we move on to other types of insert, lets create a toString() method to test our list insertion.
Write a toString() method that traverses the entire list
toString() solutionSubmit your Node.java (previously implemented) and LinkedList.java files to gradescope
Methods in LinkedList.java the autograder will test:
addNode(Node<T> node) that adds a node at the end of the listtoString()isEmpty() returns true if list is empty (no nodes), false otherwiselength() returns an integer representing number of nodes in the list (should be O(1))Our insert method has to traverse the entire list to insert a new node.
How can we improve on insertion time?
search(T value) methodWrite a search(T value) method that traverses the entire list trying to find a node that matches the argument value – if found, returns the node, if not, returns null
toString()Submit your Node.java and LinkedList.java with search(T value) method to gradescope
search() solutionWhat we have implemented is an unsorted linked list – how many comparisons does it required to find a target element?
In the worst case, each node must be examined once, resulting in O(n) complexity – n is the number of elements in the list.
Removing a node takes a few more steps:
public Node<T> remove(T data) {
Node<T> currNode = head;
Node<T> previous = null;
while (currNode != null) {
if (currNode.getData().equals(data)) {
if (previous != null) {
previous.setNext(currNode.getNext());
} else {
head = head.getNext();
}
return currNode;
} else {
previous = currNode;
currNode = currNode.getNext();
}
}
return null;
}public Node<T> pop(int n) {
if (n >= 0 && n < length) {
Node<T> current = head;
Node<T> previous = null;
for (int i = 0; i < n; i++) {
previous = current;
current = current.getNext();
}
if (previous == null) return pop();
else previous.setNext(current.getNext());
length--;
return current;
}
return null;
}| Front of List | Middle of List | End of List | |
|---|---|---|---|
| get | O(1) | O(n) | O(1) |
| set | O(1) | O(n) | O(1) |
| insert | O(1) | O(n) | O(1) |
| remove | O(1) | O(n) | O(1) |
We just implemented a singly linked list
Doubly linked list:
Circular linked list:
Many linked list functions can be implemented with recursion.
public Node<T> search(Node<T> node, T value) {
if (node == null) { // base case
return null;
}
if (node.getData().equals(value)) {
return node; // found node with matching value
}
// search next
return search(node.getNext(), value);
}
public Node<T> search(T value) {
// traverse all nodes, return the first
// node that data equals value argument
return search(head, value);
}java.util.LinkedList ClassSome Pros:
Some Cons: