Doubly Linked List In java || Implement DLL Data Structure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ravipankaj
    New Member
    • Apr 2021
    • 5

    Doubly Linked List In java || Implement DLL Data Structure

    package com.ds.algorith ms.linkedlist.D LLImplementatio n;

    import java.util.NoSuc hElementExcepti on;

    /**
    * @Author pankaj
    * @create 4/12/21 7:05 PM
    */
    public class DoublyLinkedLis t {
    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
    DoublyLinkedLis t()
    {
    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.prin t(traverse.data +" --> ");
    traverse=traver se.next;
    }
    System.out.prin tln("null");
    }
    // ------------------------ print DLL in Backward Direction ----------------------
    public void printDLLBackwar d()
    {
    if (tail==null) {
    return;
    }
    Node traverse=tail;
    while (traverse !=null)
    {
    System.out.prin t(traverse.data +" --> ");
    traverse=traver se.previous;
    }
    System.out.prin tln("null");
    }
    //------------------- insertFirst ---------------------
    public void insertFirst(int data){
    Node newNode=new Node(data);
    if (isEmpty())
    {
    tail=newNode;
    }else {
    head.previous=n ewNode; //========
    }
    newNode.next=he ad;
    head=newNode;
    length++;
    }

    // -----------------insertLast ------------------------------------------
    public void insertLast(int data)
    {
    Node newNode=new Node(data);
    if (isEmpty())
    {
    head=newNode;// head is constant
    } else {
    tail.next=newNo de;//--------
    newNode.previou s=tail;
    }
    tail=newNode;
    length++;
    }
    // -------------------------------delete First ------------------------------
    public Node deleteFirst()
    {
    if (isEmpty())
    {
    throw new NoSuchElementEx ception("DLL is Already Empty !!!!");
    }
    Node newNode= head;
    if (head == tail)
    {
    tail=null;
    } else {
    head.next.previ ous=null;
    }
    head=head.next;
    newNode.next=nu ll;
    length--;
    return newNode;
    }
    // ------------------ delete Last -------------------------
    public Node deleteLast()
    {
    if(isEmpty()) {
    throw new NoSuchElementEx ception();
    }
    Node newNode=tail;
    if (head==tail)
    {
    head=null;
    } else {
    tail.previous.n ext=null;
    }
    tail= tail.previous;
    newNode.previou s=null;
    length--;
    return newNode;
    }
    public static void main(String[] args) {
    DoublyLinkedLis t dll=new DoublyLinkedLis t();
    /*dll.insertFirs t(0);
    dll.insertFirst (1);
    dll.insertFirst (5);
    dll.insertFirst (7);
    dll.insertFirst (9);
    dll.insertFirst (12);
    dll.printDLLFor ward();*/
    // dll.printDLLBac kward();

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

    dll.printDLLFor ward();
    dll.printDLLBac kward();
    System.out.prin tln("----------------------------------------");
    // dll.deleteFirst ();
    //dll.printDLLFor ward();
    // dll.deleteFirst ();
    // dll.deleteFirst ();
    dll.deleteLast( );
    dll.printDLLFor ward();
    }
    }
Working...