// Represents a list of integers. public class LinkedIntList { private ListNode front; // post: Constructs an empty list public LinkedIntList() { front = null; } // pre : max >= 0 // post: Constructs a list with the numbers [max, max - 1, ..., 2, 1, 0] public LinkedIntList(int max) { front = null; for (int i = 0; i <= max; i++) { front = new ListNode(i, front); } } // pre: the list is in sorted order (non-decreasing) // post: add the value to the list such that the list is still sorted public void addSorted(int value) { // empty case front case if (front == null || value <= front.data) { front = new ListNode(value, front); } else { ListNode current = front; // end case middle case while (current.next != null && current.next.data < value) { current = current.next; } current.next = new ListNode(value, current.next); } } // post: Adds the given value to the end of this 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); } } // post: Returns a comma-separated, bracketed version of the list public String toString() { if (front == null) { return "[]"; } else { ListNode current = front; String result = "["; while (current.next != null) { result += current.data + ", "; current = current.next; } result += current.data + "]"; return result; } } }