// Program to test solutions to problem #9 on the cse143 final, spring 2011. // Fill in your solution to shift, then compile and run the program import java.util.*; class LinkedIntList { public void shift() { // fill in your solution here } private ListNode front; // first value in the list // this is the sample solution public void shift2() { if (front != null && front.next != null) { ListNode otherFront = front; ListNode otherBack = front; front = front.next; ListNode current = front; while (current.next != null && current.next.next != null) { otherBack.next = current.next; otherBack = current.next; current.next = current.next.next; current = current.next; } if (current.next != null) { otherBack.next = current.next; otherBack = current.next; } current.next = otherFront; otherBack.next = null; } } // post: constructs an empty list public LinkedIntList() { front = null; } // post: creates a comma-separated, bracketed version of the list public String toString() { if (front == null) return "[]"; else { String result = "[" + front.data; ListNode current = front.next; while (current != null) { result += ", " + current.data; current = current.next; } result += "]"; return result; } } // post: appends the given value to the end of the list public void add(int value) { if (front == null) front = new ListNode(value); else { ListNode current = front; while (current.next != null) current = current.next; current.next = new ListNode(value); } } } public class FinalTest9 { public static void main(String[] args) { test(new int[] {}); test(new int[] {0}); test(new int[] {0, 1}); test(new int[] {0, 1, 2}); test(new int[] {0, 1, 2, 3}); test(new int[] {0, 1, 2, 3, 4}); test(new int[] {0, 1, 2, 3, 4, 5}); test(new int[] {0, 1, 2, 3, 4, 5, 6, 7}); test(new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8}); test(new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); test(new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}); test(new int[] {42}); test(new int[] {42, 19, 6, 4, 2}); test(new int[] {37}); test(new int[] {37, 9, 3, 4}); test(new int[] {4, 17, 29, 3, 8, 2, 28, 5, 7}); } public static void test(int[] data) { LinkedIntList list1 = new LinkedIntList(); LinkedIntList list2 = new LinkedIntList(); for (int n : data) { list1.add(n); list2.add(n); } boolean fail = false; System.out.println("list = " + list1); list1.shift2(); System.out.println("expected = " + list1); try { list2.shift(); } catch (RuntimeException e) { System.out.println(" threw " + e + " with list = " + list2); fail = true; } if (!fail && !list1.toString().equals(list2.toString())) { System.out.println("actual = " + list2); fail = true; } if (fail) System.out.println("failed"); else System.out.println("passed"); System.out.println(); } } class ListNode { public int data; // data stored in this node public ListNode next; // link to next node in the list // post: constructs a node with data 0 and null link public ListNode() { this(0, null); } // post: constructs a node with given data and null link public ListNode(int data) { this(data, null); } // post: constructs a node with given data and given link public ListNode(int data, ListNode next) { this.data = data; this.next = next; } }