// CSE 143, Winter 2011, Marty Stepp // A LinkedIntList object stores a list of integers using a series of linked // node objects. // Today's version of the class implements an IntList interface // so that both kinds of lists can be treated the same by client code. import java.util.*; // for NoSuchElementException public class LinkedIntList implements IntList { private ListNode front; // refers to first node in list (null if empty) // Constructs a new empty list. public LinkedIntList() { front = null; // null front means empty } // Adds the given value to the end of this list. public void add(int value) { if (front == null) { // empty list front = new ListNode(value); } else { // non-empty list // example: change from: // front 10 -> 20 -> 30 / // to: front 10 -> 20 -> 30 -> 40 / // walk to last node ListNode current = front; while (current.next != null) { current = current.next; } // add new node at end of list current.next = new ListNode(value); } } // Inserts the given value into this list at the specified index. // Precondition: 0 <= index <= size // Throws a NullPointerException if index > size. public void add(int index, int value) { if (index == 0) { // insert at front of list front = new ListNode(value, front); } else { // walk to node before the one to insert ListNode current = front; for (int i = 0; i < index - 1; i++) { current = current.next; } ListNode newNode = new ListNode(value, current.next); current.next = newNode; // shorter version of the above code // current.next = new ListNode(value, current.next); } } // Adds the given element value to the list at the appropriate index // so that the list will remain in sorted order. // Precondition: The existing elements of the list are already sorted. // Postcondition: The new element value will be added, and the elements // of the list will still be sorted. // This method may add the value in an improper location if the list's // existing elements are not sorted when this method is called. public void addSorted(int value) { // two special cases: adding to empty list, and adding smallest value if (front == null || front.data > value) { front = new ListNode(value, front); } else { // two other cases: adding to middle and end of list // (end of list case is handled by the current.next != null test) // advance to node before the place to add ListNode current = front; while (current.next != null && current.next.data < value) { current = current.next; } // insert the new node at this location current.next = new ListNode(value, current.next); } } // Returns the element at the specified index from the list. // Precondition: 0 <= index < size // Throws a 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 the index of the first occurrence of the given value in the list, // or -1 if the value is not found in the list. public int indexOf(int value) { ListNode current = front; int index = 0; while (current != null) { if (current.data == value) { return index; } current = current.next; index++; } return -1; } // Removes and returns the front element from the list. // Precondition: size > 0 // Throws a NoSuchElementException if the list is empty. public int remove() { if (front == null) { throw new NoSuchElementException("cannot remove from empty list"); } // retrieve/return first element's data, then move to next int result = front.data; front = front.next; return result; } // Removes the value at the specified index from this list. // Precondition: 0 <= index < size // Throws a NullPointerException if index > size. public void remove(int index) { if (index == 0) { // special case: remove front node front = front.next; } else { // walk to node before the one to remove ListNode current = front; for (int i = 0; i < index - 1; i++) { current = current.next; } // remove it (point the previous node's next link past it) current.next = current.next.next; } } // Sets the element at the specified index in the list to the given value. // Precondition: 0 <= index < size // Throws a NullPointerException if index >= size. public void set(int index, int value) { ListNode current = front; for (int i = 0; i < index; i++) { current = current.next; } current.data = value; } public int size() { return 0; } public boolean isEmpty() { return true; } public void clear() { } 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; } } }