Friday, April 27, 2012

16. DOUBLE LINKED LIST IN JAVA



import java.util.Scanner;

public class Node
{
    int info;
    Node next,prev;
    Node left,right;
}

public class DoubleLL
{
    Node start=new Node();
    Node end=new Node();
    Node curr=new Node();
    int num;
  
    DoubleLL()
    {
        start=null;
        end=null;
        num=0;
    }
    void AddBeg(int val)
    {
        Node t=new Node();
        t.info=val;
        if(num++==0)
        {
            t.next=null;
            t.prev=null;
            end=t;
        }
        else
        {
            start.prev=t;
            t.next=start;
        }
        start=t;
    }
    void AddEnd(int val)
    {
        Node t=new Node();
        t.info=val;
        if(num++==0)
        {
            t.next=null;
            t.prev=null;
            start=t;
        }
        else
        {
            end.next=t;
            t.prev=end;
        }
        end=t;
    }
    void DelBeg()
    {
        if(num==0)
            System.out.println("Linked List Empty");
        else
        {
            if(start==end)
            {
                start=null;
                end=null;
                num=0;
            }
            else
            {
                start=start.next;
                start.prev=null;
            }
        }
    }
    void DelEnd()
    {
        if(num==0)
            System.out.println("Linked List Empty");
        else
        {
            if(start==end)
            {
                start=null;
                end=null;
                num=0;
            }
            else
            {
                end=end.prev;
                end.next=null;
            }
        }
    }
    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");
        }
    }
    void Display()
    {
        curr=start;
        System.out.print("Start -> ");
        while(curr!=null)
        {
            System.out.print(curr.info + " -> ");
            curr=curr.next;
        }
        System.out.println("End");
    }
    public static void main(String[] args)
    {
        int ch,val;
        DoubleLL dl=new DoubleLL();
        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 : Display 6 : Search 7 : Exit");
            ch=in.nextInt();
            switch(ch)
            {

                case 1:
                {
                    System.out.print("Enter a Number = ");
                    val=in.nextInt();
                    dl.AddBeg(val);
                    break;
                }
                case 2:
                {
                    System.out.print("Enter a Number = ");
                    val=in.nextInt();
                    dl.AddEnd(val);
                    break;
                }
                case 3:
                {
                    dl.DelBeg();
                    break;
                }
                case 4:
                {
                    dl.DelEnd();
                    break;
                }
                case 5:
                {
                    dl.Display();
                    break;
                }
                case 6:
                {
                    System.out.println("Enter Element to Search");
                    val=in.nextInt();
                    dl.Search(val);
                    break;
                }
                case 7:
                    System.exit(0);
                    break;
                default : continue;
            }
        }
    }
}