Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Looking for help to Add a reverse method to the IntList class, so x.reverse() re

ID: 3558038 • Letter: L

Question

Looking for help to Add a reverse method to the IntList class, so x.reverse() returns an IntList that is in reverse order.

public class Driver
{
   public static void main (String[] args)
   {
       IntList a = new IntList (null);
       IntList b = a.cons(2);
       IntList c = b.cons(1);
       int x = a.length() + b.length() + c.length();
       a.print();
       b.print();
       c.print();
       System.out.println(x);
   }
}


public class IntList
{
   private ConsCell start;
   public IntList (ConsCell s)
   {
       start = s;
   }
   public IntList cons(int h)
   {
       return new IntList(new ConsCell(h, start));
   }
   public int length()
   {
       int len = 0;
       ConsCell cell = start;
       while (cell != null)
       {
           len++;
           cell = cell.getTail();
       }
       return len;
   }
   public void print()
   {
       System.out.print("[");
       ConsCell a = start;
       while(a != null)
       {
           System.out.print(a.getHead());
           a = a.getTail();
           if(a != null)
               System.out.print(",");  
       }
       System.out.println("]");
   }
  
}


public class ConsCell
{
   private int head;
   private ConsCell tail;
  
   public ConsCell(int h, ConsCell t)
   {
       head = h;
       tail = t;
   }
  
   public int getHead()
   {
       return head;
   }
  
   public ConsCell getTail()
   {
       return tail;
   }
  
   public void setTail(ConsCell t)
   {
       tail = t;
   }
}

Explanation / Answer

public class ConsCell
{
   private int head;
   private ConsCell tail;
   public ConsCell(int h, ConsCell t)
   {
       head = h;
       tail = t;
   }
   public int getHead()
   {
       return head;
   }
   public ConsCell getTail()
   {
       return tail;
   }
   public void setTail(ConsCell t)
   {
       tail = t;
   }
}

public class IntList
{
   private ConsCell start;
   public IntList (ConsCell s)
   {
       start = s;
   }
   public IntList cons(int h)
   {
       return new IntList(new ConsCell(h, start));
   }
   public int length()
   {
       int len = 0;
       ConsCell cell = start;
       while (cell != null)
       {
           len++;
           cell = cell.getTail();
       }
       return len;
   }
   public void print()
   {
       System.out.print("[");
       ConsCell a = start;
       while(a != null)
       {
           System.out.print(a.getHead());
           a = a.getTail();
           if(a != null)
               System.out.print(",");
       }
       System.out.println("]");
   }
   public IntList reverse()
   {
       IntList Reverselist = new IntList(null);
       ConsCell begin = start;
       while (begin != null) {
           Reverselist = Reverselist.cons(begin.getHead());
           begin = begin.getTail();
       }  
       return Reverselist;
   }

}

public class Driver
{
   public static void main (String[] args)
   {
       IntList a = new IntList (null);
       IntList b = a.cons(2);
       IntList c = b.cons(1);
       IntList d = c.reverse();
       int x = a.length() + b.length() + c.length();
       a.print();
       b.print();
       c.print();
      d.print();
       System.out.println(x);
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote