Singly Linked List



Program In Java To Implement Singly 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;
    /*
        Constructors
    */
    Node() {
        next = 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;
        else {
            Node tmp;
            tmp = this.next;
            while (tmp.next != null) tmp = tmp.next;
            tmp.next = p;
        }
        System.out.println("\n\tYour Data Has Successfully Been Inserted.");
    }
    void delete() {
        if (this.next != null) {
            Node tmp, prev;
            tmp = this.next;
            prev = null;
            if (tmp.next != null) {
                while (tmp.next != null) {
                    prev = tmp;
                    tmp = tmp.next;
                }
                prev.next = null; //Delinks The Last Node Of A Linked List
            }
            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("LL Is Empty.");
        }
    }
    void display() {
        Node tmp;
        tmp = this.next;
        if (this.next != null) {
            System.out.println("Singly Linked List is ::");
            while (tmp != null) {
                System.out.println(tmp.info);
                tmp = tmp.next;
            }
        } else {
            System.out.println("Linked List Is Empty.");
        }
    }
}
/*
    Main Class
*/
class LList {
    public static void main(String[] arg) {
        Node ll = new Node();
        int ch, e, k;
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("\n\n\tImplementation Of Singly 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.");
                    break;
            }
        }
    }
}

OUTPUT::


Implementation Of Singly 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 Inserted.

Implementation Of Singly 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 Inserted.

Implementation Of Singly 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 Inserted.

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

Enter Your Choice::3

Singly Linked List is::12
23
34

Implementation Of Singly 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 Inserted.

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

Enter Your Choice::3

Singly Linked List is::12
23
34
45

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

Enter Your Choice::2

Deleted Node is 45

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

Enter Your Choice::2

Deleted Node is 34

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

Enter Your Choice::3

Singly Linked List is::12
23

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

Enter Your Choice::4

Exited Successfully.
Previous Post Next Post