A doubly-linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes. The beginning and ending nodes previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list. If there is only one sentinel node, then the list is circularly linked via the sentinel node. It can be conceptualized as two singly linked lists formed from the same data items, but in opposite sequential orders.
Here is the pictorial view of doubly linked list:
The two node links allow traversal of the list in either direction. While adding or removing a node in a doubly-linked list requires changing more links than the same operations on a singly linked list, the operations are simpler and potentially more efficient, because there is no need to keep track of the previous node during traversal or no need to traverse the list to find the previous node, so that its link can be modified.
Here is the pictorial view of inserting an element in the middle of a doubly linked list:
Here is the pictorial view of deleting an element in the middle of a doubly linked list:
Below shows the java implementation of doubly linked list:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
package com.java2novice.ds.linkedlist; import java.util.NoSuchElementException; public class DoublyLinkedListImpl<E> { private Node head; private Node tail; private int size; public DoublyLinkedListImpl() { size = 0; } /** * this class keeps track of each element information * @author java2novice * */ private class Node { E element; Node next; Node prev; public Node(E element, Node next, Node prev) { this.element = element; this.next = next; this.prev = prev; } } /** * returns the size of the linked list * @return */ public int size() { return size; } /** * return whether the list is empty or not * @return */ public boolean isEmpty() { return size == 0; } /** * adds element at the starting of the linked list * @param element */ public void addFirst(E element) { Node tmp = new Node(element, head, null); if(head != null ) {head.prev = tmp;} head = tmp; if(tail == null) { tail = tmp;} size++; System.out.println("adding: "+element); } /** * adds element at the end of the linked list * @param element */ public void addLast(E element) { Node tmp = new Node(element, null, tail); if(tail != null) {tail.next = tmp;} tail = tmp; if(head == null) { head = tmp;} size++; System.out.println("adding: "+element); } /** * this method walks forward through the linked list */ public void iterateForward(){ System.out.println("iterating forward.."); Node tmp = head; while(tmp != null){ System.out.println(tmp.element); tmp = tmp.next; } } /** * this method walks backward through the linked list */ public void iterateBackward(){ System.out.println("iterating backword.."); Node tmp = tail; while(tmp != null){ System.out.println(tmp.element); tmp = tmp.prev; } } /** * this method removes element from the start of the linked list * @return */ public E removeFirst() { if (size == 0) throw new NoSuchElementException(); Node tmp = head; head = head.next; head.prev = null; size--; System.out.println("deleted: "+tmp.element); return tmp.element; } /** * this method removes element from the end of the linked list * @return */ public E removeLast() { if (size == 0) throw new NoSuchElementException(); Node tmp = tail; tail = tail.prev; tail.next = null; size--; System.out.println("deleted: "+tmp.element); return tmp.element; } public static void main(String a[]){ DoublyLinkedListImpl<Integer> dll = new DoublyLinkedListImpl<Integer>(); dll.addFirst(10); dll.addFirst(34); dll.addLast(56); dll.addLast(364); dll.iterateForward(); dll.removeFirst(); dll.removeLast(); dll.iterateBackward(); } } |
Output:
adding: 10
adding: 34
adding: 56
adding: 364
iterating forward..
34
10
56
364
deleted: 34
deleted: 364
iterating backword..
56
10