import java.util.*; // A LinkedIntList object represents an ordered list of linked nodes. // Like ArrayIntList, LinkedIntList is an implementation of the List ADT. public class LinkedIntList implements IntList { private ListNode front; public Iterator iterator() { return null; } // Constructs an empty list. public LinkedIntList() { front = null; } // Builds a new LinkedIntList with the specified number of nodes. // Node values start at n and decrease in value. // pre: n >= 0 public LinkedIntList(int n) { for (int i = 0; i <= n; i++) { front = new ListNode(i, front); } } // Add the given value to the end of the list. public void add(int value) { if (front == null) { front = new ListNode(value); } else { // list wasn't empty; find the back ListNode current = front; while (current.next != null) { current = current.next; } current.next = new ListNode(value); } } // Returns the value at a given index // pre: 0 <= index < number of nodes; front != null // throws 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; } // Adds a value at a given index. // pre: 0 <= index <= size // Throws a NullPointerException if index > size. public void add(int index, int value) { if (index == 0) { front = new ListNode(value, front); } else { ListNode current = front; for (int i = 0; i < index - 1 ; i++) { current = current.next; } ListNode temp = new ListNode(value, current.next); current.next = temp; // also ok: current.next = new ListNode(value, current.next); } } public void remove(int index) { if (index == 0) { // removing the first element must be handled specially front = front.next; } else { // removing some element further down in the list; // traverse to the node before the one we want to remove ListNode current = front; for (int i = 0; i < index - 1; i++) { current = current.next; } // change its next pointer to skip past the offending node current.next = current.next.next; } } // pre : 0 <= index < size() // post: replaces the integer at the given index with the given value public void set(int index, int value) { ListNode current = front; for (int i = 0; i < index; i++) { current = current.next; } current.data = value; } // post: returns the current number of elements in the list public int size() { int size = 0; ListNode current = front; while (current != null) { current = current.next; size++; } return size; } // post : returns the position of the first occurrence of the given // value (-1 if not found) public int indexOf(int value) { int index = 0; ListNode current = front; while (current != null) { if (current.data == value) { return index; } current = current.next; index++; } return -1; } // post: returns true if list is empty, false otherwise public boolean isEmpty() { return front == null; } // post: returns true if the given value is contained in the list, // false otherwise public boolean contains(int value) { return indexOf(value) >= 0; } // post: list is empty public void clear() { front = null; } // Returns a comma-separated String representation of this 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; } } private static class ListNode { public int data; // data stored in this node public ListNode next; // link to next node in the list // post: constructs a node with data 0 and null link public ListNode() { this(0, null); } // post: constructs a node with given data and null link public ListNode(int data) { this(data, null); } // post: constructs a node with given data and given link public ListNode(int data, ListNode next) { this.data = data; this.next = next; } } }