// CSE 143, Summer 2012 // A LinkedIntList object is a list of integers represented as linked nodes. // LinkedIntList implements the same methods as ArrayIntList but with // different runtime efficiencies. import java.util.*; public class LinkedIntList { private ListNode front; private int size; // Constructs an empty list public LinkedIntList () { front = null; size = 0; } // The value will be added at the end of the list, // the size of the list will be one greater public void add (int value) { if (front == null) { front = new ListNode (value); } else { // list wasn't empty; find the back // (note: this is inefficient. We could keep a reference to the last node) ListNode current = front; while (current.next != null) { current = current.next; } current.next = new ListNode (value); } size++; } // Returns the value at a given index. // pre: 0 <= index < size // Throws a NullPointerException if index > size or index < 0. public int get (int index) { if (index < 0 || index >= size) { throw new IllegalArgumentException("index was out of bounds"); } ListNode current = front; for (int i = 0; i < index; i++) { current = current.next; } return current.data; } // Adds a value at a given index. // Pre: 0 <= index <= size // Throws a NullPointerException if index > size or index < 0 public void add (int index, int value) { if (index < 0 || index > size) { throw new IllegalArgumentException("index was out of bounds"); } else if (index == 0) { front = new ListNode (value, front); } else { ListNode current = front; for (int i = 0; i < index - 1; i++) { current = current.next; } current.next = new ListNode (value, current.next); } size++; } // Removes the value at the given index. public int remove() { if (front == null) { throw new NoSuchElementException(); } size--; int temp = front.data; front = front.next; return temp; } // Removes the value at the given index. // Pre: 0 <= index < size public void remove (int index) { if (index < 0 || index >= size) { throw new NoSuchElementException(); } else if (index == 0) { remove(); } else { ListNode current = front; for (int i = 0; i < index - 1; i++) { current = current.next; } current.next = current.next.next; size--; } } // Adds the given element value to the list in sorted order. // Pre: the list is already sorted // Post: the new element is added and the list is still 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); } size++; } // returns the number of elements in the list public int size() { return size; } // returns a string representation of the data in the list // surrounded by square brackets and separated by commas public String toString () { String str = "["; if (front != null) { str += front.data; ListNode current = front.next; while (current != null) { str += ", " + current.data; current = current.next; } } str += "]"; return str; } }