As discussed in Monday’s lecture, an IntList is our CS61B implementation for a naked recursive linked list of integers. Each IntList has a first and rest variable. The first is the int element contained by the node, and the rest is the next chain in the list (another IntList!).
In the IntList directory for this lab, we’ve provided a much larger IntList.java than the one we created in class. It has five important new static methods, two of which you’ll fill in:
1 2 3 4 5 6 7 8 9
/** * Returns a list equal to L with all elements squared. Destructive.Iteratively */ publicstaticvoiddSquareList(IntList L) { while (L != null) { L.first = L.first * L.first; L = L.rest; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** * Returns a list equal to L with all elements squared. Non-destructive.iterative */ publicstatic IntList squareListIterative(IntList L) { if (L == null) { returnnull; } IntListres=newIntList(L.first * L.first, null); IntListptr= res; IntListB= L.rest; while (L != null) { ptr.rest = newIntList(L.first * L.first, null); B = B.rest; ptr = ptr.rest; } return res; }
1 2 3 4 5 6 7 8 9
/** * Returns a list equal to L with all elements squared. Non-destructive.recursive */ publicstatic IntList squareListRecursive(IntList L) { if (L == null) { returnnull; } returnnewIntList(L.first * L.first, squareListRecursive(L.rest)); }
1
dcatenate(IntList A, IntList B): returns a list consisting of all elements of A, followed by all elements of B. May modify A. To be completed by you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/** * Returns a list consisting of the elements of A followed by the * * elements of B. May modify items of A. Don't use 'new'. */ // Iterative concate A, B publicstatic IntList dcatenate(IntList A, IntList B) { if (A == null) { return B; } IntListptr= A; while (ptr.rest != null) { ptr = ptr.rest; } ptr.rest = B; return A; }
1 2 3 4 5 6 7 8 9 10 11
// recursive destruct??? publicstatic IntList dcatenate(IntList A, IntList B) { if (A == null) { return B; } elseif (A.rest == null) { A.rest = B; returnnull; } dcatenate(A.rest, B); return A; }
1
catenate(IntList A, IntList B): returns a list consisting of all elements of A, followed by all elements of B. May not modify A. To be completed by you.
1 2 3 4 5 6 7 8 9 10 11
/** * Returns a list consisting of the elements of A followed by the * * elements of B. May NOT modify items of A. Use 'new'. */ // recursive non-destruct publicstatic IntList catenate(IntList A, IntList B) { if(A == null) { return B; } returnnewIntList(A.first, catenate(A.rest, B)); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// iterative non-destruct publicstatic IntList catenate(IntList A, IntList B) { if (A == null) { return B; } IntListptr= A; IntListres=newIntList(ptr.first, null); IntListptr1= res; while (ptr.rest != null) { ptr1.rest = newIntList(ptr.rest.first, null); ptr = ptr.rest; ptr1 = ptr1.rest; } ptr1.rest = B; return res; }
publicclassLists1Exercises { /** Returns an IntList identical to L, but with * each element incremented by x. L is not allowed * to change. */ publicstatic IntList incrList(IntList L, int x) { /* Your code here. */ if (L == null) { return L; } else { returnnewIntList(L.first + x, incrList(L.rest, x)); } }
/** Returns an IntList identical to L, but with * each element incremented by x. Not allowed to use * the 'new' keyword. */ publicstatic IntList dincrList(IntList L, int x) { /* Your code here. */ IntListB= L; while (B != null) { B.first += x; B = B.rest; } return L; }
// Test your answers by uncommenting. Or copy and paste the // code for incrList and dincrList into IntList.java and // run it in the visualizer. // System.out.println(L.get(1)); // System.out.println(incrList(L, 3)); // System.out.println(dincrList(L, 3)); } }