{"id":3898,"date":"2017-09-16T01:24:06","date_gmt":"2017-09-15T19:54:06","guid":{"rendered":"https:\/\/www.java2blog.com\/?p=3898"},"modified":"2021-04-13T20:58:03","modified_gmt":"2021-04-13T15:28:03","slug":"doubly-linked-list-java","status":"publish","type":"post","link":"https:\/\/java2blog.com\/doubly-linked-list-java\/","title":{"rendered":"Doubly Linked List in java"},"content":{"rendered":"<blockquote><p>If you want to practice data structure and algorithm programs, you can go through\u00a0<a href=\"https:\/\/java2blog.com\/java-coding-interview-questions\/\" target=\"_blank\" rel=\"noopener noreferrer\">Java coding interview questions<\/a>.<\/p><\/blockquote>\n<p>In this post, we will see about Doubly LinkedList implementation in java.<br \/>\nWe have already seen the <a href=\"https:\/\/www.java2blog.com\/implement-singly-linked-list-in-java\/\">implementation of singly linked list<\/a>. You can consider this as an extension of Singly linked list.It is quite complex to implement it as compared to singly linked list.<br \/>\nIn doubly linked list, Node has data and pointers to next node and previous node. First node&#8217;s previous points to null and Last node\u2018s next also points to null, so you can iterate over linked list in both direction with these next and previous pointers.<br \/>\nAn example of Doubly Linked List:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3900 size-full\" src=\"https:\/\/www.java2blog.com\/wp-content\/uploads\/2017\/09\/DoublyLinkedList.png\" alt=\"Doubly Linked List in java\" width=\"793\" height=\"202\" srcset=\"https:\/\/java2blog.com\/wp-content\/uploads\/2017\/09\/DoublyLinkedList.png 793w, https:\/\/java2blog.com\/wp-content\/uploads\/2017\/09\/DoublyLinkedList-300x76.png 300w, https:\/\/java2blog.com\/wp-content\/uploads\/2017\/09\/DoublyLinkedList-768x196.png 768w\" sizes=\"(max-width: 793px) 100vw, 793px\" \/><\/p>\n<p>Node for doubly linked list can be presented as below:<\/p>\n<pre class=\"java\" name=\"code\" mark=\"5\">class Node {\n    public int data;\n    public Node next;\n    public Node prev;\n\n    public void displayNodeData() {\n        System.out.println(\"{ \" + data + \" } \");\n    }\n}\n<\/pre>\n<p>As you can see, we have one more extra reference(Node prev) in case of doubly linked list.<br \/>\nLet&#8217;s say, you want to do insert First operation in case of Doubly linked list, it will look like as below:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-3905 size-full\" src=\"https:\/\/www.java2blog.com\/wp-content\/uploads\/2017\/09\/InsertDoublyLinkedList.png\" alt=\"Insert Doubly Linked List in java\" width=\"794\" height=\"296\" srcset=\"https:\/\/java2blog.com\/wp-content\/uploads\/2017\/09\/InsertDoublyLinkedList.png 794w, https:\/\/java2blog.com\/wp-content\/uploads\/2017\/09\/InsertDoublyLinkedList-300x112.png 300w, https:\/\/java2blog.com\/wp-content\/uploads\/2017\/09\/InsertDoublyLinkedList-768x286.png 768w\" sizes=\"(max-width: 794px) 100vw, 794px\" \/><\/p>\n<h2>Doubly linked list in java example<\/h2>\n<p>Let\u2019s implement Doubly Linked List in java.<\/p>\n<p>Create a java file named DoublyLinkedList.java.<\/p>\n<pre class=\"java\" name=\"code\">package org.arpit.java2blog.algo;\n\nclass Node {\n    public int data;\n    public Node next;\n    public Node prev;\n\n    public void displayNodeData() {\n        System.out.println(\"{ \" + data + \" } \");\n    }\n}\n\npublic class DoublyLinkedList {\n\n    private Node head;\n    private Node tail;\n    int size;\n\n    public boolean isEmpty() {\n        return (head == null);\n    }\n\n    \/\/ used to insert a node at the start of linked list\n    public void insertFirst(int data) {\n        Node newNode = new Node();\n        newNode.data = data;\n        newNode.next = head;\n        newNode.prev=null;\n        if(head!=null)\n            head.prev=newNode;\n        head = newNode;\n        if(tail==null)\n            tail=newNode;\n        size++;\n    }\n\n    \/\/ used to insert a node at the start of linked list\n    public void insertLast(int data) {\n        Node newNode = new Node();\n        newNode.data = data;\n        newNode.next = null;\n        newNode.prev=tail;\n        if(tail!=null)\n            tail.next=newNode;\n        tail = newNode;\n        if(head==null)\n            head=newNode;\n        size++;\n    }\n    \/\/ used to delete node from start of Doubly linked list\n    public Node deleteFirst() {\n\n        if (size == 0) \n            throw new RuntimeException(\"Doubly linked list is already empty\");\n        Node temp = head;\n        head = head.next;\n        head.prev = null;\n        size--;\n        return temp;\n    }\n\n    \/\/ used to delete node from last of Doubly linked list\n    public Node deleteLast() {\n\n        Node temp = tail;\n        tail = tail.prev;\n        tail.next=null;\n        size--;\n        return temp;\n    }\n\n    \/\/ Use to delete node after particular node\n    public void deleteAfter(Node after) {\n        Node temp = head;\n        while (temp.next != null &amp;&amp; temp.data != after.data) {\n            temp = temp.next;\n        }\n        if (temp.next != null)\n            temp.next.next.prev=temp;\n        temp.next = temp.next.next;\n\n    }\n\n    \/\/ For printing Doubly Linked List forward\n    public void printLinkedListForward() {\n        System.out.println(\"Printing Doubly LinkedList (head --&gt; tail) \");\n        Node current = head;\n        while (current != null) {\n            current.displayNodeData();\n            current = current.next;\n        }\n        System.out.println();\n    }\n\n    \/\/ For printing Doubly Linked List forward\n    public void printLinkedListBackward() {\n        System.out.println(\"Printing Doubly LinkedList (tail --&gt; head) \");\n        Node current = tail;\n        while (current != null) {\n            current.displayNodeData();\n            current = current.prev;\n        }\n        System.out.println();\n    }\n}\n<\/pre>\n<p>Create another class named DoubleLinkedListMain as below:<\/p>\n<pre class=\"java\" name=\"code\">package org.arpit.java2blog.algo;\n\npublic class DoubleLinkedListMain {\n\n public static void main(String args[])\n {\n   DoublyLinkedList myLinkedlist = new DoublyLinkedList();\n   myLinkedlist.insertFirst(5);\n   myLinkedlist.insertFirst(6);\n   myLinkedlist.insertFirst(7);\n   myLinkedlist.insertFirst(1);\n   myLinkedlist.insertLast(2);\n   myLinkedlist.printLinkedListForward();\n   myLinkedlist.printLinkedListBackward();\n\n   System.out.println(\"================\");\n   \/\/ Doubly Linked list will be \n   \/\/ 1 -&gt;  7 -&gt; 6 -&gt; 5 -&gt; 2\n\n   Node node=new Node();\n   node.data=1;\n   myLinkedlist.deleteAfter(node);\n   myLinkedlist.printLinkedListForward();\n   myLinkedlist.printLinkedListBackward();\n   \/\/ After deleting node after 1,doubly Linked list will be \n   \/\/ 2 -&gt; 1 -&gt; 6 -&gt; 5\n   System.out.println(\"================\");\n   myLinkedlist.deleteFirst();\n   myLinkedlist.deleteLast();\n\n   \/\/ After performing above operation, doubly Linked list will be \n   \/\/  6 -&gt; 5\n   myLinkedlist.printLinkedListForward();\n   myLinkedlist.printLinkedListBackward();\n }\n}\n<\/pre>\n<p>When you run above program, you will get below output:<\/p>\n<div class=\"content-box-green\">Printing Doubly LinkedList (head &#8211;&gt; tail)<br \/>\n{ 1 }<br \/>\n{ 7 }<br \/>\n{ 6 }<br \/>\n{ 5 }<br \/>\n{ 2 }Printing Doubly LinkedList (tail &#8211;&gt; head)<br \/>\n{ 2 }<br \/>\n{ 5 }<br \/>\n{ 6 }<br \/>\n{ 7 }<br \/>\n{ 1 }================<br \/>\nPrinting Doubly LinkedList (head &#8211;&gt; tail)<br \/>\n{ 1 }<br \/>\n{ 6 }<br \/>\n{ 5 }<br \/>\n{ 2 }<\/p>\n<p>Printing Doubly LinkedList (tail &#8211;&gt; head)<br \/>\n{ 2 }<br \/>\n{ 5 }<br \/>\n{ 6 }<br \/>\n{ 1 }<\/p>\n<p>================<br \/>\nPrinting Doubly LinkedList (head &#8211;&gt; tail)<br \/>\n{ 6 }<br \/>\n{ 5 }<\/p>\n<p>Printing Doubly LinkedList (tail &#8211;&gt; head)<br \/>\n{ 5 }<br \/>\n{ 6 }<\/p>\n<\/div>\n<p>That&#8217;s all about Doubly linked list in java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to practice data structure and algorithm programs, you can go through\u00a0Java coding interview questions. In this post, we will see about Doubly LinkedList implementation in java. We have already seen the implementation of singly linked list. You can consider this as an extension of Singly linked list.It is quite complex to implement [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[26,12,13],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/3898"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=3898"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/3898\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=3898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=3898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=3898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}