package class3_3_LinkedList_021; 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; private int size = 0; /** * Appends the specified element to the end of this list. *

*

This method is equivalent to {@link java.util.LinkedList#addLast}. * * @param e element to be appended to this list * @return {@code true} (as specified by {@link java.util.Collection#add}) */ @Override public boolean add(E e) { if(head == null) { head = new Node(null, e); } else { Node current = head; while (current.next != null) { current = current.next; } current.next = new Node(null, e); } size++; return true; } /** * Inserts the specified element at the specified position in this list. * Shifts the element currently at that position (if any) and any * subsequent elements to the right (adds one to their indices). * * @param index index at which the specified element is to be inserted * @param element element to be inserted * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index > size()) */ @Override public void add(int index, E element) { throw new UnsupportedOperationException("This method isn't written yet."); } /** * Removes all of the elements from this list. * The list will be empty after this call returns. */ @Override public void clear() { size= 0; head = null; } /** * Returns {@code true} if this list contains the specified element. * More formally, returns {@code true} if and only if this list contains * at least one element {@code e} such that * (o==null ? e==null : o.equals(e)). * * @param o element whose presence in this list is to be tested * @return {@code true} if this list contains the specified element */ @Override public boolean contains(Object o) { // TODO: DEBUG COMPILE ERRORS // Node cursor = head; // // for (int x = 0; x < size; x++) { // if (cursor.content.equals(element)) // return true; // // cursor = cursor.next; // } return false; } /** * Returns the element at the specified position in this list. * * @param index index of the element to return * @return the element at the specified position in this list * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= size()) */ @Override public E get(int index) { if(index < 0) { throw new IndexOutOfBoundsException("TODO"); } int currentIndex = 0; Node currentNode = head; // TODO: HANDLE NULL NEXT while(currentNode !=null && currentIndex != index) { currentIndex++; currentNode = currentNode.next; } if(currentNode == null) { throw new IndexOutOfBoundsException("TODO: Better message"); } return currentNode.value; } /** * Returns the index of the first occurrence of the specified element * in this list, or -1 if this list does not contain the element. * More formally, returns the lowest index {@code i} such that * (o==null ? get(i)==null : o.equals(get(i))), * or -1 if there is no such index. * * @param o element to search for * @return the index of the first occurrence of the specified element in * this list, or -1 if this list does not contain the element */ @Override public int indexOf(Object o) { throw new UnsupportedOperationException("Compile errors yet to be fixed."); } // TODO: FIX COMPILE ERRORS // Public int indexOf(object O) // int index = 0; // while(current.next() != null){ // if(current.value.equals(O){ // return index; // } // current = current.next(); // index++; // } // return -1; //} /** * Returns true if this list contains no elements. * * @return true if this list contains no elements */ @Override public boolean isEmpty() { throw new UnsupportedOperationException("This method isn't written yet."); } /** * Removes the element at the specified position in this list. Shifts any * subsequent elements to the left (subtracts one from their indices). * Returns the element that was removed from the list. * * @param index the index of the element to be removed * @return the element previously at the specified position * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= size()) */ @Override public E remove(int index) { Node currentNode = head; int currentIndex = 0; while(currentIndex != index - 1) { currentNode = currentNode.next; currentIndex++; } Node indexO = head; // TODO: FIX COMPILE ERRORS // while(indexO != index) { // indexO = indexO.next; // } // if(index.hasNext) { // currentNode.next = index.next; // } else { // currentNode.next = null; // } // return index; return null; } /** * Removes the first occurrence of the specified element from this list, * if it is present. If this list does not contain the element, it is * unchanged. More formally, removes the element with the lowest index * {@code i} such that * (o==null ? get(i)==null : o.equals(get(i))) * (if such an element exists). Returns {@code true} if this list * contained the specified element (or equivalently, if this list * changed as a result of the call). * * @param o element to be removed from this list, if present * @return {@code true} if this list contained the specified element */ @Override public boolean remove(Object o) { Node n = head; while (n.next != null && n.next.value.equals(o)) { n = n.next; } if (n.next == null) { return false; } n.next = n.next.next; return true; } /** * Replaces the element at the specified position in this list with the * specified element. * * @param index index of the element to replace * @param element element to be stored at the specified position * @return the element previously at the specified position * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= size()) */ @Override public E set(int index, E element) { if(index < 0 || index >= size){ throw new IndexOutOfBoundsException("Index is out of bounds."); } Node temp = head; for(int i = 0; i < index; i++){ temp = temp.next; } E oldValue = temp.value; temp.value = element; return oldValue; } /** * Returns the number of elements in this list. * * @return the number of elements in this list */ @Override public int size() { return size; } /** * Returns an array containing all of the elements in this list * in proper sequence (from first to last element). *

*

The returned array will be "safe" in that no references to it are * maintained by this list. (In other words, this method must allocate * a new array). The caller is thus free to modify the returned array. *

*

This method acts as bridge between array-based and collection-based * APIs. * * @return an array containing all of the elements in this list * in proper sequence */ @Override public Object[] toArray() { throw new UnsupportedOperationException("This method isn't written yet."); } /** * Returns an iterator over the elements in this list (in proper * sequence).

*

* This implementation merely returns a list iterator over the list. * * @return an iterator over the elements in this list (in proper sequence) */ @Override public Iterator iterator() { throw new UnsupportedOperationException("This method isn't written yet."); } public void debug() { Node current = head; while(current != null) { System.out.println("Current: "+current.value); current = current.next; } } // ----- not implemented ------ @Override public T[] toArray(T[] a) { 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 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; } }