// Program to test solutions to problem #9 on the cse143 final, fall 2014. // Fill in your solution to bubble, then compile and run the program import java.util.*; class LinkedIntList { public boolean bubble() { // fill in your solution here } private ListNode front; // first value in the list // this is the sample solution public boolean bubble2() { boolean swap = false; if (front != null && front.next != null) { if (front.data > front.next.data) { swap = true; ListNode temp = front; front = front.next; temp.next = front.next; front.next = temp; } ListNode current = front; while (current.next != null && current.next.next != null) { if (current.next.data > current.next.next.data) { swap = true; ListNode temp = current.next.next; current.next.next = temp.next; temp.next = current.next; current.next = temp; } current = current.next; } } return swap; } // 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 { private static int testCount; private static int failCount; public static void main(String[] args) { test(new int[] {}); test(new int[] {0}); test(new int[] {2, 1}); test(new int[] {1, 3, 2, 4}); test(new int[] {1, 4, 3, 2}); test(new int[] {3, 2, 1, 5, 4, 6}); test(new int[] {1, 2, 3, 4, 5, 6}); if (failCount == 0) { System.out.println("passed all tests"); } else { System.out.println("failed " + failCount + " of " + testCount + " tests"); } } 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); boolean flag1 = list1.bubble2(); System.out.println("expected = " + list1 + " returning " + flag1); boolean flag2 = false; try { flag2 = list2.bubble(); } catch (RuntimeException e) { int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" threw " + e + " at line #" + line); fail = true; } if (!fail && (!list1.toString().equals(list2.toString()) || flag1 != flag2)) { System.out.println("actual = " + list2 + " returning " + flag2); fail = true; } testCount++; if (fail) { System.out.println("failed"); failCount++; } 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; } }