Linked List

PHOTO EMBED

Sat Oct 26 2024 13:20:44 GMT+0000 (Coordinated Universal Time)

Saved by @sukumar #java

import java.util.*;

import javax.swing.plaf.synth.SynthOptionPaneUI;
public class LinkedList {
    int size;
    public class Node{
        int data;
        Node next;

        Node(int data){
            this.data=data;
            this.next=null;
            size++;
        }
    }
    LinkedList(){
        size=0;
    }
    Node head;
    public void addFirst(int data){
        Node newNode=new Node(data);
        newNode.next=head;
        head=newNode;
    }
    public void addLast(int data){
        Node newNode=new Node(data);
        if(head==null){
            newNode.next=head;
            head=newNode;
        }else{
            Node curNode=head;
            while(curNode.next!=null){
                curNode=curNode.next;
            }
            curNode.next=newNode;
        }
    }
    public void removeFirst(){
        int val=0;
        if(head==null){
            System.out.println("Linked List Empty, can't perform Deletion");
        }else if(head.next==null){
            val=head.data;
            head=null;
            System.out.println(val+" Deleted...");
            size--;
        }else{
            val=head.data;
            head=head.next;
            System.out.println(val+" Deleted...");
            size--;
        }
    }
    public void removeLast(){
        int val=0;
        if(head==null){
            System.out.println("Linked List Empty, can't perform Deletion");
        }else if(head.next==null){
            val=head.data;
            head=null;
            System.out.println(val+" Deleted...");
            size--;
        }else{
            Node curNode=head;
            Node lastNode=head.next;
            while(lastNode.next!=null){
                lastNode=lastNode.next;
                curNode=curNode.next;
            }
            val=lastNode.data;
            System.out.println(val+" Deleted...");
            curNode.next=null;
            size--;
        }
    }
    public void reverseIterate(){
        if(head==null || head.next==null){
            return;
        }else{
            Node prevNode=head;
            Node curNode=head.next;
            while(curNode!=null){
                Node nextNode=curNode.next;
                curNode.next=prevNode;
                prevNode=curNode;
                curNode=nextNode;
            }
            head.next=null;
            head=prevNode;
            System.out.println("Linked List Reversed...");
        }

    }
    public void sizeOfList(){
        System.out.println("Size of List: "+size);
    }
    public void printList(){
        Node curNode=head;
        while(curNode!=null){
            System.out.print(curNode.data+"-> ");
            curNode=curNode.next;
        }System.out.println("NULL");
    }
    public static void main(String args[]){
        LinkedList list=new LinkedList();
        Scanner sc=new Scanner(System.in);
        int val=0;
        while(true){
            System.out.println("1:addFirst  2:addLast  3:removeFirst  4:removeLast  5:reverseIterate  6:sizeOfList  7:printList  8:End");
            System.out.print("Select any Option: ");
            int option=sc.nextInt();
            if(option==8){
                System.out.println("Thank You!!! ");
                break;
            }
            if(option==1 || option==2){
                System.out.print("Enter Value: ");
                val=sc.nextInt();
            }
            switch(option){
                case 1: 
                    list.addFirst(val);
                    break;
                case 2:
                    list.addLast(val);
                    break;
                case 3:
                    list.removeFirst();
                    break;
                case 4:
                    list.removeLast();
                    break;
                case 5:
                    list.reverseIterate();
                    break;
                case 6:
                    list.sizeOfList();
                    break;
                case 7:
                    list.printList();
                    break;
                default:
                    System.out.println("INVALID INPUT");
            }
        }
        
        
    }
}
content_copyCOPY