// Program to test solutions to problem #9 on the cse143 final, spring 2018. // Fill in your solution to shiftLastOf3, then compile and run the program import java.util.*; class LinkedIntList { public int shiftLastOf3() { // fill in your solution here } private ListNode front; // first value in the list // this is the sample solution public int shiftLastOf32() { int count = 0; if (front != null && front.next != null && front.next.next != null) { ListNode curr1 = front; ListNode oldFront = curr1; front = front.next.next; ListNode curr2 = front; curr1.next.next = curr2.next; curr1 = curr1.next.next; count++; while (curr1 != null && curr1.next != null && curr1.next.next != null) { curr2.next = curr1.next.next; curr2 = curr2.next; curr1.next.next = curr2.next; curr1 = curr1.next.next; count++; } curr2.next = oldFront; } return count; } // post: constructs an empty list public LinkedIntList() { front = null; } public boolean equals(Object o) { LinkedIntList other; try { other = (LinkedIntList) o; } catch (Exception e) { return false; } if (other == null) { return false; } ListNode current1 = front; ListNode current2 = other.front; while (current1 != null && current2 != null) { if (current1.data != current2.data) return false; current1 = current1.next; current2 = current2.next; } return current1 == null && current2 == 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: creates a comma-separated, bracketed version of the list public String toString(int number) { if (front == null) return "[]"; else { String result = "[" + front.data; int count = 1; ListNode current = front.next; while (current != null && count < number) { result += ", " + current.data; current = current.next; count++; } if (current != null) { result += "..."; } 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 { private static int totalCount = 0; private static int totalFail = 0; private static int returnFailCount = 0; public static void main(String[] args) { for (int i = 0; i <= 20; i++) { test(i); } if (totalFail > 0) { System.out.println("failed " + totalFail + " of " + totalCount + " tests"); System.out.println(); } else if (returnFailCount > 0) { System.out.println("passed structural tests but return was " + "incorrect in " + returnFailCount + " cases"); } else { System.out.println("passed all " + totalCount + " tests"); } } public static void test(int len) { totalCount++; // increment test counter // list1 for testing sample, list2 for other code LinkedIntList list1 = new LinkedIntList(); LinkedIntList list2 = new LinkedIntList(); for (int i = 0; i < len; i++) { int value = 100 + i * 2; list1.add(value); list2.add(value); } boolean fail = false; System.out.println("initial list = " + list1); int return1 = list1.shiftLastOf32(); System.out.println("expected list = " + list1); int return2 = 0; try { return2 = list2.shiftLastOf3(); } catch (RuntimeException e) { if(e.getStackTrace().length > 0) { int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" threw " + e + " at line #" + line); } fail = true; } if (!fail) { System.out.println("your list = " + list2.toString(len)); if (!list1.equals(list2)) { fail = true; } } if (!fail && return1 != return2) { System.out.println("should have returned " + return1); System.out.println("actually returned " + return2); returnFailCount++; } if (fail) { System.out.println("failed"); totalFail++; } else { System.out.println("passed"); } System.out.println(); } } class ListNode { public final 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; } }