DEV Community

loading...

Doubly Linked List In java || Implement DLL Data Structure

pankajkrravi profile image Pankaj Ravi ・2 min read

package com.ds.algorithms.linkedlist.DLLImplementation;

import java.util.NoSuchElementException;

/**

  • @Author pankaj
  • @create 4/12/21 7:05 PM */

public class DoublyLinkedList {
private Node head;// first node of DLL
private Node tail;//Last Node of DLL
private int length;// length of DLL

private class Node{
    private int data;
    private Node next;
    private Node previous;
    Node(int data) {
        this.data=data;
    }
}
// constructor
DoublyLinkedList()
{
    this.head=null;
    this.tail=null;
    this.length=0;
}
// isEmpty()
// two ways to check , DLL is Empty 1. By checking head ref 2. By checking DLL length is zero(0)
public boolean isEmpty()
{
    if(head==null)
        return true;
    else return false;
   // return length==0;// DLL is empty-> true
}
//length() of DLL
public int length()
{
    return length;
}

//-------------------  print DLL in Forward Direction ---

public void printDLLForward()
{
     if (head==null) {
         return;
     }
    Node traverse=head;
    while (traverse !=null)
    {
        System.out.print(traverse.data+" --> ");
        traverse=traverse.next;
    }
    System.out.println("null");
}
// ------------------------ print DLL in Backward Direction ----------------------
public void printDLLBackward()
{
    if (tail==null) {
        return;
    }
    Node traverse=tail;
    while (traverse !=null)
    {
        System.out.print(traverse.data+" --> ");
        traverse=traverse.previous;
    }
    System.out.println("null");
}
    //------------------- insertFirst -------------------
    public void insertFirst(int data){
    Node newNode=new Node(data);
    if (isEmpty())
    {
        tail=newNode;
    }else {
        head.previous=newNode; 
    }
    newNode.next=head;
    head=newNode;
    length++;
    }

// -----------------insertLast --------------------------

public void insertLast(int data)
{
    Node newNode=new Node(data);
    if (isEmpty())
    {
        head=newNode;// head is constant
    } else {
        tail.next=newNode;//--------
        newNode.previous=tail;
    }
    tail=newNode;
    length++;
}
// -------------------------------delete First ----------
public Node deleteFirst()
{
    if (isEmpty())
    {
        throw new NoSuchElementException("DLL is Already Empty !!!!");
    }
    Node newNode= head;
    if (head == tail)
    {
        tail=null;
    } else {
        head.next.previous=null;
    }
    head=head.next;
    newNode.next=null;
    length--;
    return newNode;
}
// ------------------ delete Last -----------------------
    public Node deleteLast()
    {
        if(isEmpty()) {
            throw new NoSuchElementException();
        }
        Node newNode=tail;
        if (head==tail)
        {
            head=null;
        } else {
            tail.previous.next=null;
        }
        tail= tail.previous;
        newNode.previous=null;
        length--;
        return newNode;
    }

public static void main(String[] args) {
    DoublyLinkedList dll=new DoublyLinkedList();
    /*dll.insertFirst(0);
    dll.insertFirst(1);
    dll.insertFirst(5);
    dll.insertFirst(7);
    dll.insertFirst(9);
    dll.insertFirst(12);
    dll.printDLLForward();*/
    // dll.printDLLBackward();

    dll.insertLast(1);
    dll.insertLast(2);
    dll.insertLast(3);
    dll.insertLast(4);
    dll.insertLast(5);

    dll.printDLLForward();
    dll.printDLLBackward();
    System.out.println("------------------------------");
    // dll.deleteFirst();
    //dll.printDLLForward();
    // dll.deleteFirst();
    // dll.deleteFirst();
    dll.deleteLast();
    dll.printDLLForward();
}
Enter fullscreen mode Exit fullscreen mode

}

Discussion (2)

pic
Editor guide
Collapse
michelemauro profile image
michelemauro

A few suggestions:

  • why not trying a smoother API, getting rid of the exceptions? if I delete the first or last node, I could just get null if the list is already empty. Getting a undeclared exception makes the user code fail in unexpected and unpredictable ways.
  • are you sure that this class works? why not writing a little unit test to prove that it works like it should?

And, two ways to extend this code:

  • add a type parameter T for the contained data
  • replace null checking with java.util.Optional, to have a more explicit management of empty values.

And, last but not least, check the formatting of the article so it appears correctly.

Good job!

Collapse
pankajkrravi profile image
Pankaj Ravi Author

Thanks for Your suggestion, Actually this code works fine.
Next time will take care of Exception and and formatting of article.