// Program to test solutions to problem #9 on the cse143 final, winter 2016. // Fill in your solution to removeFirstOf3, then compile and run the program. import java.util.*; class LinkedIntList { public LinkedIntList removeFirstOf3() { // fill in your solution here } private ListNode front; // first value in the list // this is the sample solution public LinkedIntList removeFirstOf32() { LinkedIntList other = new LinkedIntList(); if (front != null) { other.front = front; ListNode back1 = other.front; front = front.next; ListNode current = front; while (current != null && current.next != null && current.next.next != null) { current = current.next; back1.next = current.next; back1 = back1.next; current.next = current.next.next; current = current.next; } back1.next = null; } return other; } // 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; private static int totalInfi = 0; @SuppressWarnings("deprecation") public static void main(String[] args) { for (int i = 0; i <= 25; i++) { Runner run = new Runner(i); run.start(); long slept = 0; try { while(slept < 500 && !run.isFinished()) { Thread.sleep(100); slept += 100; } } catch (InterruptedException e) { System.out.println("something went wrong with thread sleep"); } if(!run.isFinished()) { run.interrupt(); run.stop(); System.out.println("timeout...failed"); System.out.println(); count++; totalFail++; } } double sum = 0; if (totalFail > 0) { System.out.println("failed " + totalFail + " of " + count + " tests"); } else { System.out.println("passed all " + count + " tests"); } } private static class Runner extends Thread { private int number; private boolean finished; public Runner(int number) { this.number = number; this.finished = false; } public void run() { int length = number; // list1/list2 for testing sample, list3/list4 for their code LinkedIntList list1 = new LinkedIntList(); LinkedIntList list2 = null; LinkedIntList list3 = new LinkedIntList(); LinkedIntList list4 = null; for (int i = 0; i < length; i++) { list1.add(i); list3.add(i); } boolean fail = false; System.out.println("initial list1 = " + list1); list2 = list1.removeFirstOf32(); System.out.println("expected list1 = " + list1); System.out.println("expected list2 = " + list2); try { list4 = list3.removeFirstOf3(); } 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("your list1 matches"); } else { System.out.println("your list1 = " + list3.toString(length)); fail = true; } if (list2.equals(list4)) { System.out.println("your list2 matches"); } else { if (list4 == null) { System.out.println("your list2 = null"); } else { System.out.println("your list2 = " + list4.toString(length)); } fail = true; } } if (fail) { System.out.println("failed"); totalFail++; } else { System.out.println("passed"); } System.out.println(); count++; // increment test counter finished = true; } public boolean isFinished() { return finished; } } } 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; } }