// Program to test solutions to problem #5 on the cse143x midterm, fall 2024. // The methods code1/code2/code3/code4 have the sample solution to the four // coding problems. Replace the code with your own code to test it. public class Test5 { public static void code1() { q.next.next = p; p = null; } public static void code2() { q = p; p = p.next; q.next = null; } public static void code3() { q.next.next = q; q = q.next; q.next.next = p; p = p.next; q.next.next.next = null; } public static void code4() { q.next.next.next = p; ListNode temp = q; q = q.next.next; temp.next.next = q.next.next; p.next.next = temp; p = temp.next; p.next.next.next = null; q.next.next = null; } public static void main(String[] args) { passCount = 0; problem1(); problem2(); problem3(); problem4(); System.out.println("Passed " + passCount + " of 4 tests"); } private static ListNode p, q; private static int passCount; public static void problem1() { p = new ListNode(1); q = new ListNode(2, new ListNode(3)); doProblem(1, "p", "q->[2]->[3]->[1]"); } public static void problem2() { p = new ListNode(1, new ListNode(2, new ListNode(3))); q = null; doProblem(2, "p->[2]->[3]", "q->[1]"); } public static void problem3() { p = new ListNode(1, new ListNode(2)); q = new ListNode(3, new ListNode(4)); doProblem(3, "p->[2]", "q->[4]->[3]->[1]"); } public static void problem4() { p = new ListNode(1, new ListNode(2)); q = new ListNode(3, new ListNode(4, new ListNode(5))); doProblem(4, "p->[4]->[2]->[3]", "q->[5]->[1]"); } public static void doProblem(int n, String answer1, String answer2) { System.out.println("Problem #" + n + ":"); System.out.println("before:"); System.out.println(" " + text(p, "p")); System.out.println(" " + text(q, "q")); boolean threwException = false; try { if (n == 1) { code1(); } else if (n == 2) { code2(); } else if (n == 3) { code3(); } else if (n == 4) { code4(); } else { System.out.println("unknown problem number"); } } catch (Exception e) { int line = e.getStackTrace()[0].getLineNumber(); System.out.println("threw " + e + " at line #" + line); threwException = true; } String text1 = text(p, "p"); String text2 = text(q, "q"); int correct = 0; int nulls = 0; System.out.println("after:"); if (text1.equals(answer1)) { correct++; System.out.println(" " + text1); } else { System.out.println(" " + text1 + " (should be " + answer1 + ")"); if (text1.startsWith(answer1)) { nulls++; } } if (text2.equals(answer2)) { correct++; System.out.println(" " + text2); } else { System.out.println(" " + text2 + " (should be " + answer2 + ")"); if (text2.startsWith(answer2)) { nulls++; } } if (correct == 2 && !threwException) { System.out.println("PASS"); passCount++; } else if (correct + nulls == 2) { System.out.print("PARTIAL PASS"); if (nulls > 0) { System.out.print(" with " + nulls + " null assignment(s)"); } if (threwException) { System.out.print(" eliminating exception code"); } System.out.println(); } else { System.out.println("FAILED"); } System.out.println(); } public static String text(ListNode p, String text) { String result = text; for (int i = 0; i < 5; i++) { if (p != null) { result += "->[" + p.data + "]"; p = p.next; } } if (p != null) { result += "..."; } return result; } } 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; } }