// Tyler Mi // Represents a list of integers that we can add to and print the contents of public class LinkedIntList { private ListNode front; // Constructs an empty LinkedIntList public LinkedIntList() { front = null; } // Constructs a list with values, 1,...,n public LinkedIntList(int n) { // for(int i = 1; i <= n; i++) { // add(i); // } // ListNode temp = new ListNode(5); // front = temp; // temp = new ListNode(4, front); // front = temp; // temp = new ListNode(3, front); // front = temp; // temp = new ListNode(2, front); // front = temp; // temp = new ListNode(1, front); // front = temp; for (int i = n; i >= 1; i--) { front = new ListNode(i, front); } } // Assumes that the list is currently sorted // Add the value into the list, maintaining sorted order public void addSorted(int value) { if (front == null || front.data >= value) { // ^-empty list-^ ^----front case----^ front = new ListNode(value, front); } else { ListNode current = front; while(current.next != null && current.next.data < value) { // ^-----end case----^ ^------middle case------^ current = current.next; } current.next = new ListNode(value, current.next); } } // Adds 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); } } // Prints all the values in the list with each value // on its own line public void print() { ListNode current = front; while (current != null) { System.out.println(current.data); current = current.next; } } }