Site icon DataFlair

How to Display Linked List in Reverse Order in DSA Java

Program 1

import java.util.*;
class SingleLinkedList
{
      static class Node
      {
            int data;
            Node add;    
      }
      Node start=null,new1,temp,next1,prev1;
      void create()
      {
            start=new Node();
            int n;
            String choice;
            Scanner scan=new Scanner(System.in);
            System.out.println("Enter an element");
            n=scan.nextInt();
            start.data=n;
            start.add=null;
            temp=start;
           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.data=n;
                new1.add=null;
                temp.add=new1;
                temp=temp.add;
                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.add;
                }
             }

      }
       void reverseDisplay()
      {
             next1=start;
             ArrayList<Node>mylist=new ArrayList<Node>();
             while(next1!=null)
             {
                   mylist.add(next1);
                   next1=next1.add;
             }
               for(int i=mylist.size()-1;i>=0;i--)
               {
                  System.out.print(mylist.get(i).data + "   ");    

               }
            // System.out.println(mylist.size());

      }

      void insertFirst()
      {
        int n;
        Scanner scan=new Scanner(System.in);
        if(start==null)
             System.out.println("List not found"); 
        else
        {
            new1=new Node();
             System.out.println("Enter an element for insert");
             n=scan.nextInt();
             new1.data=n;
             new1.add=null;
             new1.add=start;
             start=new1;
        }     
      }

      void insertMiddle()
      {

            int n,pos,i=1;
           Scanner scan=new Scanner(System.in);
       if(start==null)
             System.out.println("List not found"); 
        else
        {
            System.out.println("Enter an element for insert");
             n=scan.nextInt();
             new1=new Node();
              new1.data=n;
              new1.add=null;
             System.out.println("Enter an position for insert");
             pos=scan.nextInt();
             next1=start;
             while(i<pos)
             {
                   prev1=next1;
                   next1=next1.add;
                   i++;
             }
             prev1.add=new1;
             new1.add=next1;
        }

      }

      void insertLast()
      {

            int n;
           Scanner scan=new Scanner(System.in);
       if(start==null)
             System.out.println("List not found"); 
        else
        {
            System.out.println("Enter an element for insert");
             n=scan.nextInt();

            new1=new Node();
              new1.data=n;
              new1.add=null;
             temp=start;
             while(temp.add!=null)
             {
                temp=temp.add;
             }
             temp.add=new1;
        }    
      }
     void deleteFirst()
      {
          if(start==null)
            System.out.println("List Not found..........");
            else
            {
                  temp=start;
                  start=start.add;
                  System.out.println("Deleted node is " + temp.data);
                  temp=null;
            }
      }
      void deleteLast()
      {

          if(start==null)
            System.out.println("List Not found..........");
            else
            {
                      temp=start;
                      while(temp.add!=null)
                      {
                           prev1=temp;
                           temp=temp.add;
                      }
                      prev1.add=null;
                      System.out.println("Deleted node is "+temp.data);
                      temp.add=null;
            }

      }
     
      void deleteMiddle()
      {
            Scanner scan=new Scanner(System.in);
          if(start==null)
            System.out.println("List Not found..........");
            else
            { 
                     int pos,i=1;
                     System.out.println("Enter your choice");
                     pos=scan.nextInt();
                     next1=start;
                     while(i<pos)
                     {
                         prev1=next1;
                         next1=next1.add;
                         i++;
                     }
                     temp=next1;
                     next1=next1.add;
                     prev1.add=next1;
                     System.out.println("Deleted node is : " + temp.data);
                     temp=null;

            }

      }
       boolean searchData(int n)
       {
               if(start==null)
            System.out.println("List Not found..........");
            else
            { 
                 temp=start;
                 while(temp!=null)    
                 {
                     if(n==temp.data)
                        return true;

                     temp=temp.add;   
                 }
            }
           return false; 
       }

       void countNode()
       {

             if(start==null)
            System.out.println("List Not found..........");
            else
            { 
                   int count=0;
                   temp=start;
                   while(temp!=null)
                   {
                        count++;
                        temp=temp.add;
                   }
                System.out.println("Total Node in List "+count);  
            }
       }
       void sortData()
       {
            prev1=start;
            int t;
            while(prev1!=null)
            {
                  next1=prev1.add;
                  while(next1!=null)
                  {
                        if(next1.data<prev1.data)
                        {
                               t=prev1.data;
                               prev1.data=next1.data;
                               next1.data=t;
                        }
                       next1=next1.add; 
                  }
                 prev1=prev1.add; 
            }
       }
}
class TestList
{
    public static void main(String args[])
    {
        SingleLinkedList S=new SingleLinkedList();
        Scanner scan=new Scanner(System.in);
        
        int ch,n;
      do
     {   
        System.out.println("\n-------------------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.Searching");
        System.out.println("10.Sorting");
        System.out.println("11.Reverse Display");
        System.out.println("12.Count Node");
        System.out.println("13.Exit");
        System.out.println("\n--------------------------------------------------\n");
         System.out.println("Enter your choice");
          ch=scan.nextInt();
          switch(ch)
          {
            case 1:S.create();break;
            case 2:S.display();break;
            case 3:S.insertFirst();break;
            case 4:S.insertMiddle();break;
            case 5:S.insertLast();break;
            case 6:S.deleteFirst();break;
            case 7:S.deleteMiddle();break;
            case 8:S.deleteLast();break;
            case 9:
                  {
                      System.out.println("Enter an element for search in list");
                      n=scan.nextInt();  
                       if(S.searchData(n))
                          System.out.println("Searching success");
                        else
                            System.out.println("Searching not success");
                     break;
                  }
            case 10:S.sortData();break;
            case 11:S.reverseDisplay();break;
            case 12:S.countNode();break;
            case 13:break;
            default: System.out.println("Invalid choice");
          }
     }while(ch!=13); 
    }
}

 

Exit mobile version