Write a JAVA LinkedListRec class that has the following meth
Write a JAVA LinkedListRec class that has the following methods: size, empty, insertBefore, insertAfter, addAtHead, addAtEnd, remove, replace, peekFront, peekEnd, removeFront, removeEnd, toString. Use recursion to implement most of these methods. Write a driver to test your implemented LinkedListRec class methods.
Solution
 import java.util.Scanner;
 public class LinkedListRec {
     int data;
     LinkedListRec prev=null,next=null;
   
     int size(LinkedListRec l)//method which returns the length of the list
     {
         if(l==null)return 0;
         return 1+size(l.next);
     }
     boolean empty(LinkedListRec l)//method checks whether the list is empty or not...
     {
         if(l==null)return true;
         else return false;
     }
     LinkedListRec insertBefore(LinkedListRec l,int value,int num_insertbefore)//method which adds a new number before the given number..
     {
         if(l==null)return l;
         if(l.data == num_insertbefore)
         {
             LinkedListRec n = new LinkedListRec();
             n.data = value;
             if(l.prev==null)
             {
                 n.next = l;
                 l=n;
             }
             else
             {
                 n.prev = l.prev;
                 n.next= l;
                 l.prev = n;
             }
             return l;
        }
         return insertBefore(l.next,value,num_insertbefore);
   
     }
   
     LinkedListRec insertAfter(LinkedListRec l,int value,int num_insertafter)//method which adds the new number after the given number
     {
         if(l==null)return l;
         if(l.data == num_insertafter)
         {
             LinkedListRec n = new LinkedListRec();
             n.data = value;
             if(l.next==null)
             {
                 l.next = n;
                 n.prev = l;
             }
             else
             {
                 n.next = l.next;
                 l.next.prev = n;
                 n.prev = l;
             }
             return l;
        }
         return insertAfter(l.next,value,num_insertafter);
   
     }
     LinkedListRec addAtHead(LinkedListRec l,int value)//method which adds a new number at the head position of the list...
     {
         LinkedListRec n = new LinkedListRec();
     n.data=value;
     n.next = l;
     l=n;
     return l;
     }
     LinkedListRec addAtEnd(LinkedListRec l,int value)//method which adds a new number at the end position of the list...
     {
         if(l==null){ l = new LinkedListRec();l.data = value;return l;}
         if(l.next==null)
         {
         LinkedListRec n = new LinkedListRec();
         n.data=value;
         n.prev = l;
         l.next = n;
         return l;
         }
         return addAtEnd(l.next,value);
     }
     LinkedListRec remove(LinkedListRec l,int value)//method which removes given numbet from the list....
     {
         if(l==null)return l;
         if(l.data == value)
         {
             if(l.prev!=null){
             l.prev = l.next;
             }
             else
                 l=l.next;
             return l;
         }
         return remove(l.next,value);
     }
     boolean replace(LinkedListRec l,int value,int newvalue)//method which replaces current number with new number,...
     {
         if(l==null)return false;
         if(l.data == value)
         {
             l.data = newvalue;
             return true;
         }
         return replace(l.next,value,newvalue);
     }
     int peekFront(LinkedListRec l)//method which returns the first value of the list...
     {
     if(l!=null)  
     return l.data;
     else return -1;
     }
     int peekEnd(LinkedListRec l)//method which returns the last value of the list........
     {
         if(l==null)return -1;
         if(l.next == null)return l.data;
         return peekEnd(l.next);
   
     }
     LinkedListRec removeFront(LinkedListRec l)//method which removes the first element of the list..
     {
         if(l==null)return l;
         l=l.next;
         return l;
     }
     boolean removeEnd(LinkedListRec l)//method which removes the last element of the list//
     {
       
         if(l.next==null)
         {
             if(l.prev!=null){l.prev.next=null;return true;}
             l.data = -1;
             return true;
         }
         return removeEnd(l.next);
     }
     String tostring(LinkedListRec l)//displaying list
     {
         if(l==null)return \"\";
         System.out.print(l.data+\"->\");
         return l.data+\"->\"+tostring(l.next);
   
     }
    public static void main(String argv[])
     {
         //driver testing code...
         LinkedListRec ll = new LinkedListRec();
         ll.data = 2;
         Scanner sc = new Scanner(System.in);
         int c=-2;
         while(true)
         {
             System.out.println(\"Select on option(-1 to exit)\ 1: size of list\ 2: is list empty\ 3: insert Before a number\ 4:insert After a number\ 5: add At head\ 6: add at End\ 7: remove from list\  8: Replace a value\ 9: peekfrontvalue\ 10:peekendvalue\ 11:removefrontvalue\ 12:removeendvalue\ 13:display\");
              System.out.print(\"Enter ur choice\");
             c = sc.nextInt();
             if(c==-1)break;
             if(c==1){
                 System.out.println(\"\ Size of List:\"+ll.size(ll));
               
              }
             else if(c==2){
                 if(ll.empty(ll))
                     System.out.println(\"\ List is Empty\ \");
                 else System.out.println(\"\ List is not Empty\ \");
             }
             else if(c==3){
                 int v,p;
                 System.out.println(\"\ Enter number to insert:\");
                 v = sc.nextInt();
                 System.out.println(\"\ Enter number to insertBefore it:\");
                 p = sc.nextInt();
                 ll=ll.insertBefore(ll, v, p);
             }
             else if(c==4){
           
                 int v,p;
                 System.out.println(\"\ Enter number to insert:\");
                 v = sc.nextInt();
                 System.out.println(\"\ Enter number to insertAfter it:\");
                 p = sc.nextInt();
                 ll.insertAfter(ll, v, p);
             }
             else if(c==5){
                 int v;
                 System.out.println(\"\ Enter number to insert:\");
                 v = sc.nextInt();
                ll= ll.addAtHead(ll, v);
             }
             else if(c==6){
                 int v;
                 System.out.println(\"\ Enter number to insert:\");
                 v = sc.nextInt();
                 ll=ll.addAtEnd(ll, v);
             }
             else if(c==7){
                 int v;
                 System.out.println(\"\ Enter number to remove:\");
                 v = sc.nextInt();
                 ll=ll.remove(ll, v);
              }
             else if(c==8){
                 int v,p;
                 System.out.println(\"\ Enter number to replace:\");
                 v = sc.nextInt();
                 System.out.println(\"\ Enter which number to replace:\");
                 p = sc.nextInt();
                 ll.replace(ll, v, p);
             }
             else if(c==9){
           
                 System.out.println(\"\ Front value of the list:\"+ll.peekFront(ll));
             }
             else if(c==10){
                 System.out.println(\"\ End value of the list:\"+ll.peekEnd(ll));
             }
             else if(c==11){
                 ll=ll.removeFront(ll);
               
                      System.out.println(\"\ Front value of the list is removed\");
              
              }
             else if(c==12){
                 if(ll.removeEnd(ll))
                 {
                     System.out.println(\"\ Front value of the list is removed\");
                 }
             }
             else if(c==13){
                 System.out.println(\"\  The list:\");
                 ll.tostring(ll);
                 System.out.println();
             }
      
         }
       
     }
 }
ouput:
run:
 Select on option(-1 to exit)
 1: size of list
 2: is list empty
 3: insert Before a number
 4:insert After a number
 5: add At head
 6: add at End
 7: remove from list
 8: Replace a value
 9: peekfrontvalue
 10:peekendvalue
 11:removefrontvalue
 12:removeendvalue
 13:display
 Enter ur choice8
Enter number to replace:
 2
Enter which number to replace:
 7
 Select on option(-1 to exit)
 1: size of list
 2: is list empty
 3: insert Before a number
 4:insert After a number
 5: add At head
 6: add at End
 7: remove from list
 8: Replace a value
 9: peekfrontvalue
 10:peekendvalue
 11:removefrontvalue
 12:removeendvalue
 13:display
 Enter ur choice13
The list:
 7->
 Select on option(-1 to exit)
 1: size of list
 2: is list empty
 3: insert Before a number
 4:insert After a number
 5: add At head
 6: add at End
 7: remove from list
 8: Replace a value
 9: peekfrontvalue
 10:peekendvalue
 11:removefrontvalue
 12:removeendvalue
 13:display
 Enter ur choice9
Front value of the list:7
 Select on option(-1 to exit)
 1: size of list
 2: is list empty
 3: insert Before a number
 4:insert After a number
 5: add At head
 6: add at End
 7: remove from list
 8: Replace a value
 9: peekfrontvalue
 10:peekendvalue
 11:removefrontvalue
 12:removeendvalue
 13:display
 Enter ur choice6
Enter number to insert:
 9
 Select on option(-1 to exit)
 1: size of list
 2: is list empty
 3: insert Before a number
 4:insert After a number
 5: add At head
 6: add at End
 7: remove from list
 8: Replace a value
 9: peekfrontvalue
 10:peekendvalue
 11:removefrontvalue
 12:removeendvalue
 13:display
 Enter ur choice13
The list:
 7->9->
 Select on option(-1 to exit)
 1: size of list
 2: is list empty
 3: insert Before a number
 4:insert After a number
 5: add At head
 6: add at End
 7: remove from list
 8: Replace a value
 9: peekfrontvalue
 10:peekendvalue
 11:removefrontvalue
 12:removeendvalue
 13:display
 Enter ur choice1
Size of List:2
 Select on option(-1 to exit)
 1: size of list
 2: is list empty
 3: insert Before a number
 4:insert After a number
 5: add At head
 6: add at End
 7: remove from list
 8: Replace a value
 9: peekfrontvalue
 10:peekendvalue
 11:removefrontvalue
 12:removeendvalue
 13:display
 Enter ur choice-1







