// Helene Martin, CSE 143 // A LinkedIntList stores an ordered list of integers. // This version adds: // - a constructor that builds a list // - add at an index (not discussed in class) // - addSorted // LinkedIntList and ArrayIntList are different implementations of the // List Abstract Data Type (ADT). public class LinkedIntList { private ListNode front; // Initializes a new empty list. public LinkedIntList() { front = null; } // Build a new list with nodes of values n to 0. // Pre: n >= 0 public LinkedIntList(int n) { if (n < 0) { throw new IllegalArgumentException(); } for (int i = 0; i <= n; i++) { front = new ListNode(i, front); // same as: // ListNode temp = new ListNode(i, front); // front = temp; } // we started by writing out some examples to find pattern // // n = 1 // temp = new ListNode(1, front); // front = temp; // // // n = 2 // temp = new ListNode(2, front); // front = temp; } // 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); } } // Adds a value at a given index. // Pre: 0 <= index <= size, throws a NullPointerException if index > size. public void add(int index, int value) { if (index == 0) { front = new ListNode(value, front); } else { ListNode current = front; for (int i = 0; i < index - 1; i++) { current = current.next; } ListNode temp = new ListNode(value, current.next); current.next = temp; // also ok: current.next = new ListNode(value, current.next); } } // Adds the value in sorted (non-decreasing) order. // Pre: list is sorted // Post: list is sorted public void addSorted(int value) { if (front == null || value < front.data) { front = new ListNode(value, front); } else { ListNode current = front; while (current.next != null && current.next.data < value) { current = current.next; } current.next = new ListNode(value, current.next); } } // Returns the value at the given index. // Pre: 0 <= index < size // Throws NullPointerException if index >= size public int get(int index) { ListNode current = front; for (int i = 0; i < index; i++) { current = current.next; } return current.data; } // Returns a comma-separated String representation of this 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; } } }