// CSE 143, Winter 2011, Marty Stepp // A LinkedIntList object stores a list of integers using a series of linked // node objects. We will implement the same methods as ArrayIntList. // (I have included several methods from the Section 8 handout to make the list // a more complete class, though we did not write such methods in lecture. // The key methods written in lecture were add, addSorted, get, and remove.) public class LinkedIntList { 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) { // add(size(), value); if (front == null) { // empty list front = new ListNode(value); } else { // non-empty list; 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 the front front = new ListNode(value, front); } else { // insert in middle/end; walk to node before the one to insert ListNode current = goTo(index - 1); 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) { if (front == null || front.data > value) { 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 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 = goTo(index); 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) { int index = 0; ListNode current = front; while (current != null) { if (current.data == value) { return index; } index++; current = current.next; } return -1; } // Returns whether this list does not contain any elements. public boolean isEmpty() { return front == null; } // 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) { // removing from the front front = front.next; } else { ListNode current = goTo(index - 1); current.next = current.next.next; // deletes the node } } // Sets the element at the specified index in the list to have the given value. // Precondition: 0 <= index < size // Throws a NullPointerException if index >= size. public int set(int index, int value) { ListNode current = goTo(index); current.data = value; } // Returns the number of elements in this list. public int size() { int count = 0; ListNode current = front; while (current != null) { current = current.next; count++; } return count; } // Returns a text representation of this list, such as "[42, -3, 17]" // or "[]" for an empty 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; } } // Returns a reference to the node object representing the index'th element // in the list. Used as a helper by many of the public methods. private ListNode goTo(int index) { ListNode current = front; for (int i = 0; i < index; i++) { current = current.next; } return current; } }