Doubly Linked List in DSA Java Part – 2

Program 1

import java.util.*;
class DoubleLinkedList
{
       static class Node
       {
             Node ladd;
             int data;
             Node radd;
       }
       Node start=null,new1,prev1,next1,temp;
       void create()
       {
             Scanner scan=new Scanner(System.in);
             int n;
             System.out.println("Enter first element");
             n=scan.nextInt();
             start=new Node();
             start.ladd=null;
             start.data=n;
             start.radd=null;
             temp=start;
             String choice;
             System.out.println("Want to continue");
             choice=scan.next();
             while(choice.toUpperCase().equals("Y"))
             {
                System.out.println("Enter next element");
                n=scan.nextInt();
                new1=new Node();
                new1.ladd=null;
                new1.data=n;
                new1.radd=null;
                temp.radd=new1;
                new1.ladd=temp;
                temp=temp.radd;
                System.out.println("Want to continue");
                choice=scan.next();
             }

       }
       void display()
       {
                 if(start==null)
                 System.out.println("***********List not Found*************");
                 else
                 {
                      temp=start;
                      while(temp!=null)
                      {
                        System.out.print(temp.data + "    ");
                           temp=temp.radd; 
                      }
                 }

       }
        void reversedisplay()
       {
                 if(start==null)
                 System.out.println("***********List not Found*************");
                 else
                 {
                     temp=start;
                     while(temp.radd!=null)
                     {
                        temp=temp.radd;
                     }
                     while(temp!=null)
                     {
                        System.out.print(temp.data + "   ");
                        temp=temp.ladd;
                     }
                 }
       }
       void insertFirst()
       {
               if(start==null)
                  System.out.println("***********List not Found*************");
               else
               {
                     Scanner scan=new Scanner(System.in);
                     int n;
                     System.out.println("Enter an element for insert");
                     n=scan.nextInt();
                     new1=new Node();
                     new1.data=n;
                    // new1.add=null;
                     new1.ladd=null;
                     new1.radd=null;

                     new1.radd=start;
                     start.ladd=new1;
                     start=new1;
               }

       }

       void insertLast()
       {
               if(start==null)
                  System.out.println("***********List not Found*************");
               else
               {
                     Scanner scan=new Scanner(System.in);
                     int n;
                     System.out.println("Enter an element for insert");
                     n=scan.nextInt();
                     new1=new Node();
                     new1.data=n;
                     new1.ladd=null;
                     new1.radd=null;
                    temp=start;
                    while(temp.radd!=null)
                    {
                       temp=temp.radd;
                    }
                    temp.radd=new1;
                    new1.ladd=temp;
               }

       }

       void insertMiddle()
       {
               if(start==null)
                  System.out.println("***********List not Found*************");
               else
               {
                     Scanner scan=new Scanner(System.in);
                     int n,pos,i;
                     System.out.println("Enter an element for insert");
                     n=scan.nextInt();
                     new1=new Node();
                     new1.data=n;
                     new1.ladd=null;
                     new1.radd=null;
                     System.out.println("Enter poistion for insert");
                     pos=scan.nextInt();
                     i=1;
                     next1=start;
                     while(i<pos)  
                     {
                           prev1=next1;
                           next1=next1.radd;
                           i++;
                     }
                     prev1.radd=new1;
                     new1.ladd=prev1;
                     new1.radd=next1;
                     next1.ladd=new1;
               }          
       }
       void deleteFirst()
       {
                if(start==null)
                  System.out.println("***********List not Found*************");
               else
               {
                       temp=start;
                       start=start.radd;
                       System.out.println("Deleted node " + temp.data);
                       temp=null;
               }
       }

       void deleteLast()
       {
                if(start==null)
                  System.out.println("***********List not Found*************");
               else
               {
                       temp=start;
                       while(temp.radd!=null)
                       {
                           temp=temp.radd;
                       }
                       prev1=temp.ladd;
                         prev1.radd=null;
                         System.out.println("Deleted node " + temp.data);
                         temp=null;
               }
       }

          void deleteMiddle()
          {
               int i,pos;
             if(start==null)
                  System.out.println("***********List not Found*************");
               else
               {
                     Scanner scan=new Scanner(System.in);   
                     System.out.println("Enter poisition of node for delete");
                     pos=scan.nextInt();   
                        i=1;
                        temp=start;
                        while(i<pos)
                        {
                              temp=temp.radd;
                              i++;
                        }
                        prev1=temp.ladd;
                        next1=temp.radd;
                        prev1.radd=next1;
                        next1.ladd=prev1;
                         System.out.println("Deleted node " + temp.data);
                         temp=null;
               }
          }

}
class TestDoubleList
{
        public static void main(String args[])
    {
        DoubleLinkedList D=new DoubleLinkedList();
        Scanner scan=new Scanner(System.in);
        int ch;
      do
     {   
        System.out.println("\n-------------------Doubly Linked List-------------------");
        System.out.println("1.Create");
        System.out.println("2.Display");
        System.out.println("3.Insert First");
        System.out.println("4.Insert Middle");
        System.out.println("5.Insert Last");
        System.out.println("6.Delete First");
        System.out.println("7.Delete Middle");
        System.out.println("8.Delete Last");
        System.out.println("9.Reverse Display");
        System.out.println("10.Exit");
        System.out.println("\n--------------------------------------------------\n");
         System.out.println("Enter your choice");
          ch=scan.nextInt();
          switch(ch)
          {
            case 1:D.create();break;
            case 2:D.display();break;
            case 3:D.insertFirst();break;
            case 4:D.insertMiddle();break;
            case 5:D.insertLast();break;
            case 6:D.deleteFirst();break;
            case 7:D.deleteMiddle();break;
            case 8:D.deleteLast();break;
            case 9:D.reversedisplay();
            case 10:break;
          }
     }while(ch!=10); 
    }
  }

 

courses

Leave a Reply

Your email address will not be published. Required fields are marked *