// A LinkedIntList object represents an ordered list of linked nodes. // Like ArrayIntList, LinkedIntList is an implementation of the List ADT. public class LinkedIntList { private ListNode front; public LinkedIntList() { front = null; } // post: adds num to the end of the list public void add(int value) { // get to the end (almost) // add to the end if (front == null) { front = new ListNode(num); } else { ListNode current = front; while (current.next != null) { current = current.next; } // current = ... will NEVER change the list // two ways to change: // current.next // front current.next = new ListNode(num); } } // pre : 0 <= index < size(), throws IndexOutOfBoundsException otherwise // post: returns the element at index public int get(int index) { if (index < 0) { throw new IndexOutOfBoundsException(); } for (int i = 0; i < index; i++) { if (current == null) { throw new IndexOutOfBoundsException(); } current = current.next; } return current.data; } public int size() { int size = 0; ListNode current = front; while (current != null) { size++; current = current.next; } return size; } // post: returns a string representation of the list public String toString() { String listString = "["; if (front != null) { ListNode current = front; listString += current.data; current = current.next; while (current != null) { listString += ", " + current.data; current = current.next; } } listString += "]"; return listString; } }