Saturday, April 16, 2011

Single Link-List with Operations - Add in Beginning, Add at End, Del at Beg, Del at End, Del any Element, Display, Search


import java.util.Scanner;
public class SingleLL
{
    Node start=new Node();
    Node end=new Node();
    Node curr=new Node();
    int num;

    SingleLL()
    {
        start=null;
        end=null;
        num=0;
    }
   
    void AddBeg(int val)
    {
        Node t=new Node();
        t.info=val;
        if(num++==0)
            end=t;
        else
            t.next=start;
        start=t;
    }

    void AddEnd(int val)
    {

        Node t=new Node();
        t.info=val;
        t.next=null;
        if(num++==0)
            start=t;
        else
            end.next=t;
        end=t;
    }

    void DelBeg()
    {
        if(num==0)
            System.out.println("Linked List is Empty");
        else
        {
            if(start==end)
            {
                start=null;
                end=null;
                num=0;
            }
            else
                start=start.next;
        }
    }

    void DelEnd()
    {
        if(num==0)
            System.out.println("Linked List is Empty");
        else
        {
            if(start==end)
            {
                start=null;
                end=null;
                num=0;
            }
            else
            {
                curr=start;
                while(curr.next!=end)
                    curr=curr.next;
                end=curr;
                end.next=null;
            }
        }
    }
   
    void DelEle(int val)
    {
        if(num==0)
            System.out.println("Linked List is Empty");
        else
        {
            curr=start;
            Node prev=new Node();
            if(end==start && start.info==val)
            {
                start=end=null;
                num=0;
            }
            else if(start.info==val)
                start=start.next;
            else if(end.info==val)
            {
                System.out.println("HEllo");
                curr=start;
                while(curr.next!=end)
                    curr=curr.next;
                end=curr;
                end.next=null;
            }
            else
            {
                curr=start;
                while(curr.info!=val)
                {
                    prev=curr;
                    curr=curr.next;
                }
                prev.next=curr.next;
            }           
        }
    }

    void Display()
    {
        curr=start;
        System.out.print("Start -> ");
        while(curr!=null)
        {
            System.out.print(curr.info + " -> ");
            curr=curr.next;
        }
        System.out.println("End");
    }

    void Search(int val)
    {
        Loop:
        {
        for(curr=start;curr!=null;curr=curr.next)
        {
            if(curr.info==val)
            {
                System.out.println("Element Found");
                break Loop;
            }
        }
        System.out.println("Element Not Found");
        }
    }

    public static void main(String args[])
    {
        int ch,val;
        SingleLL sl=new SingleLL();
        Scanner in = new Scanner(System.in);
        while(true)
        {
            System.out.println("-----------------------------------");
            System.out.println("Enter your Choice");
            System.out.println("1 : Add Beg  2 : Add End  3 : Del Beg  4 : Del End  5 : Del Ele  6 : Display  7 : Search  8 : Exit");
            ch=in.nextInt();
            switch(ch)
            {

case 1:
                {
                    System.out.print("Enter Number = ");
                    val=in.nextInt();
                    sl.AddBeg(val);
                    break;
                }
                case 2:
                {
                    System.out.print("Enter Number = ");
                    val=in.nextInt();
                    sl.AddEnd(val);
                    break;
                }
                case 3:
                {
                    sl.DelBeg();
                    break;
                }
                case 4:
                {
                    sl.DelEnd();
                    break;
                }
                case 5:
                {
                    System.out.print("Enter Number To Delete = ");
                    val=in.nextInt();
                    sl.DelEle(val);
                    break;
                }
                case 6:
                {
                    sl.Display();
                    break;
                }
                case 7:
                    System.out.print("Enter Number = ");
                    val=in.nextInt();
                    sl.Search(val);
                    break;
                case 8:
                    System.exit(0);
                    break;
                default : continue;
            }
        }
    }   
}

No comments:

Post a Comment