Question: public class ConsCell { private int head; private ConsCell tail; public ConsCell(int h, ConsCell t) { head = h; tail = t; } public int

 public class ConsCell { private int head; private ConsCell tail; public

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("]"); } //exercise 5 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); } }

Exercise 6 Add a reverseMe instance method to the IntList class, so that x.reverseMe () returns no value but has the side effect of reversing the contents of x. (This is easier if you start by making a backwards copy of x. It is harder, and more interesting, to do the reversal in place, without creating any new ConsCell objects.)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!