// Allison Obourn // CSE 143 - Lecture 26 // A LinkedList object stores a list of values using // a linked list as its underlying data structure. // This version uses "generics" so that it can store any type of value, not // just integers. It also implements the same interface as ArrayList, // so that the two can be used interchangeably in client code. public class LinkedList implements List { private ListNode front; // Initializes a new empty list public LinkedList() { front = null; } // Adds the given value to the end of the list. public void add(E value) { if(front == null) { front = new ListNode(value); } else { ListNode current = front; while(current.next != null) { current = current.next; } current.next = new ListNode(value); } } // Adds a value at a given index. // Pre: 0 <= index <= size, throws a NullPointerException if index > size. public void add(int index, E 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); } } // 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, E value) { ListNode current = goTo(index); current.data = value; } // 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(E value) { int index = 0; ListNode current = front; while (current != null) { if (current.data.equals(value)) { return index; } index++; current = current.next; } return -1; } //TODO: change <> to compareTo - incomplete! // Adds the value in sorted (non-decreasing) order. // Pre: list is sorted // Post: list is sorted public void addSorted(E value) { if(front == null || value < front.data) { 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 value at the given index. // Pre: 0 <= index < size public E get(int index) { ListNode current = front; for(int i = 0; i < index; i++) { current = current.next; } return current.data; } // 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; } } // Returns the size of this list (inefficient -- could use a size field). public int size() { ListNode current = front; int size = 0; while(current != null) { size++; current = current.next; } return size; } // True if the list is empty, false otherwise public boolean isEmpty() { return front == null; } // Removes the value at the given index. // Pre: 0 <= index < size public void remove(int index) { if (index == 0) { front = front.next; } else { ListNode current = front; for (int i = 0; i < index - 1; i++) { current = current.next; } current.next = current.next.next; } } // 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; } // A ListNode represents a single node in a linked list. It stores a // value and a link to the next node. private class ListNode { E data; ListNode next; // Creates a terminal ListNode containing null as data. public ListNode() { this(null, null); } // Creates a terminal ListNode with the specified data. public ListNode(E data) { this(data, null); } // Creates a ListNode with the specified data and next node. public ListNode(E data, ListNode next) { this.data = data; this.next = next; } } }