package class4_1_LinkedList_tests_start; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.ListIterator; public class LinkedList implements List { private Node head = null; private Node tail = null; private int size = 0; private class Node { private E value; private Node next; public Node(E value, Node next) { this.value = value; this.next = next; } } public boolean add(E element) { return add2(element); } // Original version using the approach we used in class public boolean add1(E element) { Node newNode = new Node(element, null); if(size == 0){ head = newNode; } else { Node current = head; while(current.next != null) { current = current.next; } current.next = newNode; } tail = newNode; size++; return true; } // New version to take advantage of the tail public boolean add2(E element) { Node newNode = new Node(element, null); if(size == 0){ head = newNode; } else { tail.next = newNode; } tail = newNode; size++; return true; } // based on version from Section 051 public void add(int index, E element) { if(index < 0 || index > size) { // FIXED BUG: Must allow index == size for elements // inserted at the end. throw new IndexOutOfBoundsException("index cannot be negative. It is: " + index); } if(index == 0) { head = new Node(element, head); if(tail == null) { tail = head; } } else { Node current = head; for(int i = 0; i iterator() { return new Iter(); } private class Iter implements Iterator { private Node current = head; @Override public boolean hasNext() { return current != null; } @Override public E next() { E tmp = current.value; current = current.next; return tmp; } } @Override public int lastIndexOf(Object o) { return 0; } @Override public ListIterator listIterator() { return null; } @Override public ListIterator listIterator(int index) { return null; } @Override public List subList(int fromIndex, int toIndex) { return null; } @Override public boolean containsAll(Collection c) { return false; } @Override public boolean addAll(Collection c) { return false; } @Override public boolean addAll(int index, Collection c) { return false; } @Override public boolean removeAll(Collection c) { return false; } @Override public boolean retainAll(Collection c) { return false; } @Override public void clear() { } @Override public E get(int index) { return null; } @Override public T[] toArray(T[] a) { return null; } }