package class4_3_LinkedLists; 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. * * **************************************************** */ public abstract 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 { head.next = new Node(); head.next.next = null; head.next.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 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; } } } }