Doubly Linked List



Program In Java To Implement Doubly Linked List.

The Following Program Reads The Data From User And Displays Menu Containing Linked List Operations And Reads Users Choice And Works According To Input And Displays Message Accordingly. This Menu Is In Infinite Loop Until User Enters Exit.


import java.util.*;
class Node {
    int info;
    Node next, prev;
    /*
        Constructors
    */
    Node() {
        next = prev = null;
    }
    /*
        Functions
    */
    void insert(int d) //Is Also A Create Method
    {
        Node p = new Node();
        p.info = d;
        if (this.next == null) {
            this.next = p;
            p.prev = null;
        } else {
            Node tmp;
            tmp = this.next;
            while (tmp.next != null) tmp = tmp.next;
            tmp.next = p;
            p.prev = tmp;
        }
        System.out.println("\nYour Data Has Successfully Been Entered.");
    }
    void delete() {
        if (this.next != null) {
            Node tmp, p;
            tmp = this.next;
            if (tmp.next != null) {
                while (tmp.next != null) tmp = tmp.next;
                p = tmp.prev;
                p.next = null; //Delinks The Last Node Of A Linked List
            }
            tmp.next = tmp.prev = null;
            int x = tmp.info;
            if (tmp == this.next) tmp = this.next = null;
            else tmp = null;
            System.out.println("\nDeleted Node is " + x);
        } else {
            System.out.println("\nLL Is Empty.");
        }
    }
    void display() {
        Node tmp;
        tmp = this.next;
        if (this.next != null) {
            System.out.println("Doubly Linked List is ::");
            while (tmp != null) {
                System.out.println(tmp.info);
                tmp = tmp.next;
            }
        } else {
            System.out.println("\nLinked List Is Empty.");
        }
    }
}
/*
    Main Class
*/
class DLList {
    public static void main(String[] arg) {
        Node ll = new Node();
        int ch, e;
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("\n\n\tImplementation Of Doubly Linked List");
            System.out.println("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
            System.out.print("\tEnter Your Choice::");
            ch = sc.nextInt();
            switch (ch) {
                case 1: //Inserting Node In Linked List
                    System.out.print("\n\tEnter Data To Be Inserted::");
                    e = sc.nextInt();
                    ll.insert(e);
                    break;
                case 2: //Removing Node From Linked List
                    ll.delete();
                    break;
                case 3: //Displaying The Linked List
                    ll.display();
                    break;
                case 4: //Exiting
                    System.out.println("\n\tExited Successfully.");
                    System.exit(1);
                    break;
                default:
                    System.out.println("\n\tImproper Input.\n\tEnter Proper Input.");
            }
        }
    }
}

OUTPUT::


Implementation Of Doubly Linked List
1. Insert
2. Delete
3. Display
4. Exit
Enter Your Choice::1

Enter Data To Be Inserted::12

Your Data Has Successfully Been Entered.

Implementation Of Doubly Linked List
1. Insert
2. Delete
3. Display
4. Exit

Enter Your Choice::1

Enter Data To Be Inserted::23

Your Data Has Successfully Been Entered.

Implementation Of Doubly Linked List
1. Insert
2. Delete
3. Display
4. Exit

Enter Your Choice::1

Enter Data To Be Inserted::34

Your Data Has Successfully Been Entered.

Implementation Of Doubly Linked List
1. Insert
2. Delete
3. Display
4. Exit

Enter Your Choice::3

Doubly Linked List is::12
23
34

Implementation Of Doubly Linked List
1. Insert
2. Delete
3. Display
4. Exit

Enter Your Choice::1

Enter Data To Be Inserted::45

Your Data Has Successfully Been Entered.

Implementation Of Doubly Linked List
1. Insert
2. Delete
3. Display
4. Exit

Enter Your Choice::3

Doubly Linked List is::12
23
34
45

Implementation Of Doubly Linked List
1. Insert
2. Delete
3. Display
4. Exit

Enter Your Choice::2

Deleted Node is 45

Implementation Of Doubly Linked List
1. Insert
2. Delete
3. Display
4. Exit

Enter Your Choice::2

Deleted Node is 34

Implementation Of Doubly Linked List
1. Insert
2. Delete
3. Display
4. Exit

Enter Your Choice::3

Doubly Linked List is::12
23

Implementation Of Doubly Linked List
1. Insert
2. Delete
3. Display
4. Exit

Enter Your Choice::4

Exited Successfully.
Previous Post Next Post