import java.io.*;
class Node
{
     Object info;
     Node next,pre;
    Node(Object data)
    {
       info=data;
       next=null;
       pre=null;    
    }
 }
 class DLinkedList
 {
   Node start;
   DLinkedList()
   {
    start=null;
   }
   void first(Node nnode)
   {
      nnode.next=start;
      start.pre=nnode;
      start=nnode;
   }
   void mid(Node nnode,int v)
   {
       Node p=start;
       for(int i=1;i<v-1;i++)
         {
            p=p.next;
         }  
      (p.next).pre=nnode;
      nnode.next=p.next;
      nnode.pre=p;
      p.next=nnode;
     System.out.println("value inserted at "+v+" position.");
  }
   void end(Node nnode)
   {
       Node p=start;
       while(p.next!=null)
       {
         p=p.next;
      }
      p.next=nnode;
      nnode.pre=p;
      System.out.println("value inserted at end");
  }
   void display()
   {
       Node p=start;
       if(p==null)
       {
          System.out.println("\n The LinkedList is empty.");
       }
     else
     {
        System.out.print("your values are : ");
        while(p!=null)
        {
           System.out.print(p.info+"  ");
           p=p.next;
        }
     }
   }
   Node search(Object data)throws Exception
   {
      Node p;
      int  co=1,f=0;
      p=start;
      if(p==null)
      {
          System.out.println(" The LinkedList is empty.");
      }
      else
      { 
        while((p!=null)&&(f==0))
         {
            if((p.info).equals(data)==true)
            {
             f=1;
            }
           else
           {
              p=p.next;
              co++;
           }
        }
        if((p.info).equals(data)==true)
        {
           System.out.println(p.info+" found at "+co+" place " );
           return(p);  
         }
        else
         {
            System.out.println(data+" does not found");
             return(null);
         }
    }
    return(p);
  }
  void del(Object data)throws Exception
  {
      Node p=null;
      int f=0;
      if(start==null)
      {
         System.out.println(" The linked list is empty.");
       }
      try
      {
          p=search(data);
       }
       catch(Exception e){}
       if(p!=null)
       {
           if(p.pre!=null)
           {
               if(p.next==null)
                {
  p.pre.next=p.next;
   p=null;
                }
               else
               {
  p.pre.next=p.next;
  p.next.pre=p.pre;
  p=null;
               }
          }
         else
          {
              if(p.pre!=null && p.next==null)
                  start=null;
              else
               {
  start=start.next;
                  start.pre=null;
               }
           }
     }
  }  
   public static void main(String args[ ])throws Exception
   {
      DLinkedList ob=new DLinkedList();
      int c,a=1,x;
      DataInputStream dis=new DataInputStream(System.in);
      while(a==1)
      {
         System.out.println("\n   Main Menu \n");
         System.out.println("1:Insert value\n2:To Delete value");
         System.out.println("3:Display\n4:Search value");
         System.out.println("5:Exit");
         System.out.print("\nEnter your choice:");
         c=Integer.parseInt(dis.readLine());
         switch(c) 
          {
            case 1: System.out.println("\n....INSERTION.... \n");
                          System.out.print("Enter your value:");
                          x=Integer.parseInt(dis.readLine());
                          Node nnode=new Node(new Integer(x));
                          if(ob.start==null)                         
                              ob.start=nnode;
                          else
                           {
                  System.out.println("\n1:Insert value at first place");
                               System.out.println("2:Midle");
                               System.out.println("3:Insert value at end");
                               System.out.print("Enter your choice:");
                                int pos=Integer.parseInt(dis.readLine());
                                if(pos==1)
                                   ob.first(nnode);
                                if(pos==2)
                                  {    
                                       System.out.print("Enter position:");
                                        int v=Integer.parseInt(dis.readLine());
                                         ob.mid(nnode,v);
                                   }
                 if(pos==3)
                                       ob.end(nnode);
            }
              break;
            
              case 2:  try
            {
                            System.out.print("Enter value to delete:");
                               x=Integer.parseInt(dis.readLine());
              ob.del(new Integer(x));
     System.out.print(" value is to deleted");
             }
              catch(Exception e)
              {}
                        break;
                   
                case 3:
                              System.out.println();
                              ob.display();
                              break;
          case 4: try
        {
                         System.out.print("Enter value for search:");
                            x=Integer.parseInt(dis.readLine());
           Node p=null;
           p=ob.search(new Integer(x));
                  }
         catch(Exception e)
         {}
                            break;
            case 5:     System.out.println("\n....EXIT....");
                             System.exit(1);
                             break;
           default: System.out.println("\n....Wrong Choice....");
        }
     }
   }
}
/*
 OUTPUT:
 C:\java\bin>javac DLinkedList.java
Note: DLinkedList.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
C:\java\bin>java DLinkedList
   Main Menu
1:Insert value
2:To Delete value
3:Display
4:Search value
5:Exit
Enter your choice:1
....INSERTION....
Enter your value:54
   Main Menu
1:Insert value
2:To Delete value
3:Display
4:Search value
5:Exit
Enter your choice:1
....INSERTION....
Enter your value:34
1:Insert value at first place
2:Midle
3:Insert value at end
Enter your choice:3
value inserted at end
   Main Menu
1:Insert value
2:To Delete value
3:Display
4:Search value
5:Exit
Enter your choice:1
....INSERTION....
Enter your value:778
1:Insert value at first place
2:Midle
3:Insert value at end
Enter your choice:1
   Main Menu
1:Insert value
2:To Delete value
3:Display
4:Search value
5:Exit
Enter your choice:1
....INSERTION....
Enter your value:898
1:Insert value at first place
2:Midle
3:Insert value at end
Enter your choice:2
Enter position:2
value inserted at 2 position.
   Main Menu
1:Insert value
2:To Delete value
3:Display
4:Search value
5:Exit
Enter your choice:3
your values are : 778  898  54  34
   Main Menu
1:Insert value
2:To Delete value
3:Display
4:Search value
5:Exit
Enter your choice:2
Enter value to delete:778
778 found at 1 place
 value is to deleted
   Main Menu
1:Insert value
2:To Delete value
3:Display
4:Search value
5:Exit
Enter your choice:5
....EXIT....
 */
 
No comments:
Post a Comment