We need to modify the Java node class to support the doubly linked list structure.
A doubly linked list is a linked list in which each node contains a relation to the following and previous nodes. Modify the preceding code in Snippet 2.13 to support this.
The following code shows the solution to this:
public class DblLinkedListNode<V> {
private V value;
private DblLinkedListNode<V> next;
private DblLinkedListNode<V> previous;
public DblLinkedListNode(V value,
DblLinkedListNode<V> next,
DblLinkedListNode<V> previous) {
this.value = value;
this.next = next;
this.previous = previous;
}
}
Snippet 2.14: Doubly linked list node class, with getters and setters omitted for brevity. Source class name: Dbllinkedlistnode
Go to https://goo.gl/oJDQ8g to access the code.
In a doubly linked list, the head node will have a null previous pointer while the tail node will have a null next pointer.
In a doubly linked list, the head node will have a null previous pointer while the tail node will have a null next pointer.
In this section, we saw how to model a linked list node using classes, generics, and optional references. In the next section, we shall see how to implement some of the linked list operations.