// Hunter Schafer, CSE 143 // This class represents a list of integers public class LinkedIntList { private ListNode front; // Constructs an empty list public LinkedIntList() { front = null; } // Constructs a new list with the values // [n, n-1, ..., 1, 0] public LinkedIntList(int n) { // front = new ListNode(0, front); for (int i = 0; i <= n; i++) { // ListNode temp = new ListNode(i); // temp.next = front; // front = temp; front = new ListNode(i, front); // x = x + 1; } // temp = new ListNode(2); // temp.next = front; // front = temp; // temp = new ListNode(3); // temp.next = front; // front = temp; } // Precondition: The list is sorted // Adds the given n to the list such that the list is still sorted public void addSorted(int n) { // Order of tests is important! if (front == null || front.data > n) { front = new ListNode(n, front); } else { ListNode current = front; // robust sensitive while (current.next != null && current.next.data < n) { current = current.next; } // ListNode temp = new ListNode(n); // temp.next = current.next; // current.next = temp; current.next = new ListNode(n, current.next); } } /** The methods below are either from previous lectures or added to complete the LinkedIntList **/ // Adds the given value to the end of this list. public void add(int value) { // Would do this if we had a size field and size() wasn't "slow". // 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); } } // 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 int remove(int index) { if (index == 0) { int value = front.data; front = front.next; return value; } else { ListNode current = goTo(index - 1); int value = current.next.data; current.next = current.next.next; // deletes the node return value; } } // Returns true if the given value is contained in this list, // or false otherwise. public boolean contains(int value) { return indexOf(value) >= 0; } // 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 void 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. // Precondition: 0 <= index < size private ListNode goTo(int index) { ListNode current = front; for (int i = 0; i < index; i++) { current = current.next; } return current; } }