## OverviewA linked list is a data structure that can store a collection of items. Like arrays, linked lists are used to store several objects of the same type. However, linked lists differ from arrays in the following ways: - linked lists are dynamically sized; they grow and shrink as nodes are added and removed and they do not require more memory than there are objects currently in the collection.
- the nodes of a linked list need not be stored contiguously in memory.
- linked lists are not random access data structures; items are accessed sequentially, starting from the beginning of the list.
Linked lists are typically depicted as follows: Each element of the list is referred to as a The first node in the list is referred to by a reference
## Implementation DetailsTo implement a Linked List in Java you must implement 2 classes -- one to represent a Node and one to represent the entire list. Node
The Node class will have two data members, the Object and the next reference. It should also provide appropriate constructor(s) and get/set methods.
LinkedList
The LinkedList class will maintain appropriate references to the data stored in the list and will provide methods to add data, remove data, and access data.
## Insertion
- Set the next reference of the new node to the node that head refers to
`new_node.setNext(head)`
- Set the head to refer to the new node
`head = new_node`
If you move the head reference before you've set another reference to refer
to the first node in the list you lose the entire list.
Not if you keep a tail reference. If you keep a tail reference, you need to
test whether head == null. If so, set
`head=tail=new_node` .
The algorithm is O(1). It requires the same number of operations regardless
of the size of the current list.
`new_node.setNext(null)` `tail.setNext(new_node)` `tail = new_node`
**What about inserting into an empty list?****What is the running time of the algorithm?****Does the running time remain the same if we do NOT maintain a tail reference?**
## FindThe general algorithm for searching a linked lists is as follows: current = head
## Deletion//a constant time operation //if the list is empty, do nothing Notice that the delete tail operation iterates through the entire list.
This requires visiting ## Arbitrary Insertion/DeletionTo insert in an arbitrary position in a linked list, for example in order
to maintain a sorted list, you must find the node that comes You can then execute the following algorithm: new_node.setNext(previous.getNext()) This is an O(n) operation. As always, make sure to consider special cases! Arbitrary deletion is a similar operation. ## Doubly Linked ListsDoubly linked lists, as you might imagine, are linked lists wherein each node maintains a reference to the next node in the list and a reference to the previous node in the list. They have the advantage that you can move forward and backward in the list. So, removing the last node of the list is a constant time operation, not a linear time operation as with singly linked lists. However, maintaining two pointers for each node adds overhead with respect to memory used and maintenance of the references. Often, doubly linked lists are implemented by using You should be able to implement a doubly linked list and provide running time analysis for each of your list operations. |