package class2_3_LinkedList_051; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.ListIterator; public class LinkedList implements List { private class Node { private Node next; // private Node prev; // Not implemented. private E value; private Node(Node next, E value) { this.next = next; this.value = value; } } private Node head = null; /** * Returns the number of elements in this list. If this list contains * more than Integer.MAX_VALUE elements, returns * Integer.MAX_VALUE. * * @return the number of elements in this list */ @Override public int size() { int count = 0; Node current = head; while(current != null) { count++; current = current.next; } return count; } /** * Appends the specified element to the end of this list (optional * operation). *

*

Lists that support this operation may place limitations on what * elements may be added to this list. In particular, some * lists will refuse to add null elements, and others will impose * restrictions on the type of elements that may be added. List * classes should clearly specify in their documentation any restrictions * on what elements may be added. * * @param e element to be appended to this list * @return true (as specified by {@link java.util.Collection#add}) * @throws ClassCastException if the class of the specified element * prevents it from being added to this list * @throws NullPointerException if the specified element is null and this * list does not permit null elements * @throws IllegalArgumentException if some property of this element * prevents it from being added to this list */ @Override public boolean add(E e) { Node current = head; // Why didn't this work? Because head == null meant current == null, // even if we don't go through the loop. So we fixed it like this: if(current != null) { int sz = size(); for(int i = 0; i < sz-1; i++) { current = current.next; } current.next = new Node(null, e); } else { head = new Node(null, e); } // Second approach: We tested this one too, and it worked. // if(current != null) { // while (current.next != null) { // current = current.next; // } // current.next = new Node(null, e); // } else { // head = new Node(null, e); // } return true; } public void debug() { Node current = head; while(current != null) { System.out.println("Current: "+current.value); current = current.next; } } // ----- not implemented ------ @Override public boolean isEmpty() { return false; } @Override public boolean contains(Object o) { return false; } @Override public Iterator iterator() { return null; } @Override public Object[] toArray() { return new Object[0]; } @Override public T[] toArray(T[] a) { return null; } @Override public boolean remove(Object o) { return false; } @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 E set(int index, E element) { return null; } @Override public void add(int index, E element) { } @Override public E remove(int index) { return null; } @Override public int indexOf(Object o) { return 0; } @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; } }