// Program to test solutions to problem #9 on the cse143 final, winter 2017. // Fill in your solution to switchEvens, then compile and run the program import java.util.*; class LinkedIntList { public void switchEvens(LinkedIntList other) { // fill in your solution here // you can also rename the parameter above } private ListNode front; // first value in the list // this is the sample solution public void switchEvens2(LinkedIntList other) { // enter solution here if (front != null && other.front != null) { ListNode temp = other.front; other.front = front; front = temp; temp = front.next; front.next = other.front.next; other.front.next = temp; ListNode curr1 = front.next; ListNode curr2 = other.front.next; while (curr1 != null && curr2 != null && curr1.next != null && curr2.next != null) { temp = curr2.next; curr2.next = curr1.next; curr1.next = temp; temp = curr2.next.next; curr2.next.next = curr1.next.next; curr1.next.next = temp; curr1 = curr1.next.next; curr2 = curr2.next.next; } } } // 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 count = 0; // test counter for arrays private static int totalFail = 0; public static void main(String[] args) { for (int i = 0; i <= 7; i++) { test(i, i); } for (int i = 0; i <= 6; i++) { for (int j = i + 1; j <= 7; j++) { test(i, j); } } test(8, 12); test(10, 15); test(9, 13); test(11, 14); if (totalFail > 0) { System.out.println("failed " + totalFail + " of " + count + " tests"); } else { System.out.println("passed all " + count + " tests"); } } public static void test(int len1, int len2) { testOne(len1, len2); if (len1 != len2) { testOne(len2, len1); } } public static void testOne(int len1, int len2) { count++; // list1/list2 for testing sample, list3/list4 for other code LinkedIntList list1 = new LinkedIntList(); LinkedIntList list2 = new LinkedIntList(); LinkedIntList list3 = new LinkedIntList(); LinkedIntList list4 = new LinkedIntList(); for (int i = 0; i < len1; i++) { list1.add(i + 11); list3.add(i + 11); } for (int i = 0; i < len2; i++) { list2.add(i + 21); list4.add(i + 21); } boolean fail = false; System.out.println("initial list1 = " + list1); System.out.println("initial list2 = " + list2); list1.switchEvens2(list2); System.out.println("expected list1 = " + list1); System.out.println("expected list2 = " + list2); try { list3.switchEvens(list4); } 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) { if (list1.equals(list3)) { System.out.println("other list1 matches"); } else { System.out.println("other list1 = " + list3); fail = true; } if (list2.equals(list4)) { System.out.println("other list2 matches"); } else { System.out.println("other list2 = " + list4); fail = true; } } 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; } }