import java.util.*; /** * LinkedIntList is a class that represents a LinkedList with int data. Stores a * reference to the front of the LinkedList. * * @author Adam Blank */ public class LinkedIntList implements IntList, Iterable { /** Stores the front of the LinkedIntList */ private ListNode front; /** Stores the size of the LinkedIntList */ private int size; private static class ListNode { final int data; ListNode next; /** Creates a terminal ListNode with data as its data. */ public ListNode(int data) { this(data, null); } /** * Creates a ListNode with data as its data and next as the node it * points to. */ public ListNode(int data, ListNode next) { this.data = data; this.next = next; } } private class LinkedIntListIterator implements Iterator { private int last; private ListNode node; public LinkedIntListIterator() { this.last = -1; this.node = LinkedIntList.this.front; } public boolean hasNext() { return (this.last + 1) < LinkedIntList.this.size; } public Integer next() { this.last++; int data = this.node.data; this.node = this.node.next; return data; } public void remove() { throw new UnsupportedOperationException(); } } /** Creates a new empty LinkedIntList */ public LinkedIntList() { this.front = null; } /** Returns a new iterator for the list */ public Iterator iterator() { return new LinkedIntListIterator(); } /** * Returns a String representation of the list consisting of the elements * in order, separated by commas and enclosed in square brackets. */ public String toString() { String result = "["; ListNode current = this.front; // Process all but the last element of the LinkedList while (current != null && current.next != null) { result += current.data + ", "; current = current.next; } // Process the last element (if one exists) if (current != null) { result += current.data; } return result + "]"; } /** * Appends value to the end of the list.

* post: value is appended to the ArrayList */ public void add(int value) { if (this.front == null) { this.front = new ListNode(value); } else { ListNode current = this.front; while (current.next != null) { current = current.next; } current.next = new ListNode(value); } this.size++; } /** Returns the number of elements in the list. */ public int size() { return this.size; } /** * Returns the data at index.

* pre: 0 <= index <= size, otherwise throws IndexOutOfBoundsException */ public int get(int index) { checkIndex(index); ListNode current = this.front; for (int i = 0; i < index; i++) { current = current.next; } return current.data; } /** Removes the value at the given index, shifting later values over.

* pre: 0 <= index < size otherwise throws IndexOutOfBoundsException */ public void remove(int index) { checkIndex(index); if (index == 0) { this.front = this.front.next; } else { ListNode current = this.front; for (int i = 0; i < index - 1; i++) { current = current.next; } current.next = current.next.next; } this.size--; } /** * Sets the data at index to value.

* pre: 0 <= index < size, otherwise throws IndexOutOfBoundsException */ public void set(int index, int value) { checkIndex(index); ListNode current = this.front; if (index == 0) { this.front = new ListNode(value, this.front.next); } else { for (int i = 0; i < index - 1; i++) { current = current.next; } current.next = new ListNode(value, current.next.next); } } /** * Checks that index is within valid bounds for the LinkedIntList.

* pre: 0 <= index <= size, otherwise throws IndexOutOfBoundsException */ private void checkIndex(int index) { if (index < 0 || index >= this.size) { throw new IndexOutOfBoundsException(); } } }