// Program to test solutions to problem #3 on the cse143 midterm, fall 2011. // Fill in your solution to increasingTriples, then compile and run the program import java.util.*; class LinkedIntList { public boolean increasingTriples() { // fill in your solution here } private ListNode front; // first value in the list // this is the sample solution public boolean increasingTriples2() { ListNode current = front; while (current != null && current.next != null && current.next.next != null) { if (current.data >= current.next.data || current.next.data >= current.next.next.data) return false; current = current.next.next.next; } if (current!= null && current.next != null && current.data >= current.next.data) return false; return true; } // 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 Test3 { public static void main(String[] args) { test(new int[] {}); test(new int[] {42}); test(new int[] {2, 8}); test(new int[] {8, 2}); test(new int[] {4, 5, 7}); test(new int[] {4, 7, 5}); test(new int[] {5, 4, 7}); test(new int[] {5, 4, 7}); test(new int[] {7, 4, 5}); test(new int[] {7, 5, 4}); test(new int[] {10, 14, 25, 4, 5, 7}); test(new int[] {10, 14, 25, 4, 7, 5}); test(new int[] {10, 14, 25, 5, 4, 7}); test(new int[] {10, 14, 25, 5, 4, 7}); test(new int[] {10, 14, 25,7 , 4, 5}); test(new int[] {10, 14, 25, 7, 5, 4}); test(new int[] {1, 2, 2}); test(new int[] {1, 1, 2}); test(new int[] {0, 10, 15, 23, 45}); test(new int[] {1, 2, 2, 3, 3, 3}); test(new int[] {-8, -5, -5, 0, 17}); test(new int[] {42, 42, 42, 42, 42}); test(new int[] {1, 3, 6, 7, 12}); test(new int[] {3, 5, 11, 4, 8}); test(new int[] {1, 2, 3, 0, 42, 309, -8, 47, 2094}); test(new int[] {1, 2, 3, 0, 42, 309, -8, 47, 2094, 14}); test(new int[] {1, 2, 3, 0, 42, 309, -8, 47, 2094, 14, 20}); test(new int[] {1, 2, 3, 0, 42, 309, -8, 47, 2094, 20, 14}); test(new int[] {1, 2, 3, 0, 42, 309, -8, 2094, 47}); test(new int[] {1, 2, 3, 0, 42, 309, -8, 2094, 47, 14}); test(new int[] {1, 2, 3, 0, 42, 309, -8, 2094, 47, 14, 20}); test(new int[] {1, 2, 3, 0, 42, 309, -8, 2094, 47, 20, 14}); test(new int[] {1, 2, 3, 0, 42, 309, 47, -8, 2094}); test(new int[] {1, 2, 3, 0, 42, 309, 47, -8, 2094, 14}); test(new int[] {1, 2, 3, 0, 42, 309, 47, -8, 2094, 14, 20}); test(new int[] {1, 2, 3, 0, 42, 309, 47, -8, 2094, 20, 14}); } public static void test(int[] data) { LinkedIntList list = new LinkedIntList(); for (int n : data) { list.add(n); } System.out.println("list = " + list); boolean test1 = list.increasingTriples2(); System.out.println("increasingTriples = " + test1); boolean fail = false; try { boolean test2 = list.increasingTriples(); if (test1 != test2) { fail = true; System.out.println("yours = " + test2); } } catch (RuntimeException e) { System.out.println(" threw " + e); 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; } }