package class7_2_TestsFixed; import com.sun.istack.internal.NotNull; import java.util.Collection; // CHANGE: Added to complete interface import java.util.ListIterator; // CHANGE: Added to complete interface import java.util.Iterator; import java.util.List; /****************************************************** * * WARNING * * This code is the code we went over in class, but * it does contain errors, one of which prevents it from * compiling. * * TODO: Test code * All methods * Iterator * **************************************************** */ public class LinkedList implements List { private Node head; public boolean add(T value) { Node current = head; Node last = null; while(current != null) { last = current; current = current.next; } if(null != last) { last.next = new Node(); last.next.next = null; last.next.value = value; } else { // ERROR: Using head.next instead of head when setting head. head = new Node(); head.next = null; head.value = value; } printList(); return true; } private class Node { private Node next; private T value; private Node() { // do nothing; } private Node(Node next, T value) { this.next = next; this.value = value; } } private void printList() { Node current = head; while(current != null) { System.out.print(current.value+" "); current = current.next; } System.out.println(); } @Override public int size() { Node current = head; int i; for(i=0; current != null; i++) { current = current.next; } return i; } @Override public boolean isEmpty() { return head == null; } @Override public boolean contains(Object o) { return locateNode(o) != null; } @Override public Object[] toArray() { int size = size(); Object[] objects = new Object[size]; Node current = head; for(int i=0; i 0 Node current = head; for(int i=1; i subList(int fromIndex, int toIndex) { // TODO: Potential HW return null; //To change body of implemented methods use File | Settings | File Templates. } @Override @NotNull public Iterator iterator() { return new MyIterator(); } private class MyIterator implements Iterator { Node next; Node current; Node prev; private MyIterator() { next = head; current = null; prev = null; } @Override public boolean hasNext() { return next != null; } @Override public T next() { prev = current; current = next; next = next.next; return current.value; } @Override public void remove() { if(prev != null) { prev.next = next; } else { head = next; } } } // CHANGE: Added to complete the interface @Override public T1[] toArray(T1[] a) { throw new UnsupportedOperationException("This operation not supported"); } @Override public boolean containsAll(Collection c) { throw new UnsupportedOperationException("This method not implemented"); } @Override public boolean addAll(Collection c) { throw new UnsupportedOperationException("This method not implemented"); } @Override public boolean addAll(int index, Collection c) { throw new UnsupportedOperationException("This method not implemented"); } @Override public boolean removeAll(Collection c) { throw new UnsupportedOperationException("This method not implemented"); } @Override public boolean retainAll(Collection c) { throw new UnsupportedOperationException("This method not implemented"); } @Override public int lastIndexOf(Object o) { throw new UnsupportedOperationException("This method not implemented"); } @Override public ListIterator listIterator() { throw new UnsupportedOperationException("This method not implemented"); } @Override public ListIterator listIterator(int index) { throw new UnsupportedOperationException("This method not implemented"); } }