diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 3e59c38..f1d2de0 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,91 @@ +import edu.greenriver.sdev333.ArrayList; +import edu.greenriver.sdev333.List; +import edu.greenriver.sdev333.RecursiveLinkedList; +import edu.greenriver.sdev333.SinglyLinkedList; + +import java.util.Iterator; +import java.util.ListIterator; + public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + + List friends = new SinglyLinkedList<>(); + System.out.println("The initial size is: " + friends.size()); + friends.add("1"); + friends.add("2"); + friends.add("3"); + friends.add("4"); + friends.add("5"); + friends.add("6"); + friends.add("7"); + friends.add("8"); + friends.add("9"); + friends.add("10"); + ((SinglyLinkedList) friends).addToBack("Friday"); + //((ArrayList) friends).addToFront("Friday"); + System.out.println("The size is now: " + friends.size()); + + + for (int i = 0; i < friends.size(); i++) { + System.out.println(friends.get(i)); + } + } + + + public void testArrayListClass(ArrayList friends) { + // contains test code + System.out.println(""); + System.out.println("CONTAINS TEST CODE............"); + System.out.println(""); + //String stringToTest = null; + String stringToTest = "10"; + System.out.println("Does our list contain " + stringToTest + "? " + + friends.contains(stringToTest)); + + // add( item ) test code + // add( index, item ) test code + System.out.println(""); + System.out.println("ADD TEST CODE............"); + System.out.println(""); + System.out.println("Size before add: " + friends.size()); + int indexToTest = 10; + String stringToAddWithIndex = " added this string"; + String stringToAddWithoutIndex = "This should be added at the end"; + + friends.add(indexToTest, stringToAddWithIndex); + friends.add(stringToAddWithoutIndex); + System.out.println("Size AFTER adds: " + friends.size()); + for (int i = 0; i < friends.size(); i++) { + System.out.println(friends.get(i)); + } + + + // remove test code + System.out.println(""); + System.out.println("REMOVE TEST CODE............"); + System.out.println(""); + System.out.println("Size BEFORE remove: " + friends.size()); + int indexToRemove = 0; + friends.remove(indexToRemove); + System.out.println("Size AFTER remove: " + friends.size()); + for (int i = 0; i < friends.size(); i++) { + System.out.println(friends.get(i)); + } + + // get test code + // set test code + System.out.println(""); + System.out.println("GET/SET TEST CODE............"); + System.out.println(""); + int indexToGet = 0; + System.out.println("Get item at index " + indexToGet + + " = " + friends.get(indexToGet) ); + int indexToSet = 0; + String newStringToSet = "Set this at index " + indexToSet; + friends.set(indexToSet, newStringToSet); + System.out.println("After set at index " + indexToGet + ": " + + friends.get(indexToSet)); + } + } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java new file mode 100644 index 0000000..e7be023 --- /dev/null +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -0,0 +1,525 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; +import java.util.ListIterator; + +public class ArrayList implements List { + // WE NEED FIELDS + + // one plain old Java array + private ItemType[] data; + + // one int to keep track of size + + // size is number of spots that are not NULL + // size is different than length + private int size; + + + /** + * Constructor initializing private fields + */ + public ArrayList(){ + size = 0; + data = (ItemType[]) new Object[10]; + } + + /** + * Second constructor that allows the client to ask for an initial capacity for + * the underlying data array (instead of using our default of 10) + * @param capacity the capacity + */ + public ArrayList(int capacity){ + size = 0; + data = (ItemType[]) new Object[capacity]; + } + + /** + * Returns the number of items in this collection. + * + * @return the number of items in this collection + */ + @Override + public int size() { + + return size; + } + + /** + * Returns true if this collection contains no items. + * + * @return true if this collection contains no items + */ + @Override + public boolean isEmpty() { + + return size == 0; + } + + /** + * Returns true if this collection contains the specified item. + * + * @param item items whose presence in this collection is to be tested + * @return true if this collection contains the specified item + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public boolean contains(ItemType item) { + if ( item == null ){ + throw new NullPointerException("Item cannot be null"); + } + int i = indexOf(item); + if ( i != -1 ) { + return true; + } + return false; + } + + /** + * Returns an iterator over the elements in this collection. + * + * @return an Iterator over the elements in this collection + */ + @Override + public Iterator iterator() { + + return new OurCustomIterator(); + } + + /** + * Adds the specified item to the collection. + * + * @param item item to be added to the collection + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void add(ItemType item) { + + // HOWEVER: when size becomes length - we run out of room + checkSize(); + + data[size] = item; + size++; + } + + /** + * Removes a single instance of the specified item from this collection, + * if it is present. + * + * @param item item to be removed from this collection, if present + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void remove(ItemType item) { + if ( item == null ){ + throw new NullPointerException("Item cannot be null"); + } + int i = indexOf(item); + if( i != -1 ){ + remove(i); + } + } + + /** + * Removes all items from this collection. + * The collection will be empty after this method returns. + */ + @Override + public void clear() { + // lazy deletion = + size = 0; + } + + /** + * Returns true if this collection contains all the items + * in the specified other collection. + * + * @param otherCollection collection to be checked for containment in this collection + * @return true if this collection contains all the items + * in the specified other collection + */ + @Override + public boolean containsAll(Collection otherCollection) { + + Iterator itr = (Iterator) otherCollection.iterator(); + while (itr.hasNext()){ + ItemType itemToCheck = itr.next(); + if (!contains(itemToCheck)){ + return false; + } + } + + return true; + } + + /** + * Adds all the items in this specified other collection to this collection. + * + * @param otherCollection collection containing items to be added to this collection + */ + @Override + public void addAll(Collection otherCollection) { + + } + + /** + * Removes all of this collection's items that are also contained in the + * specified other collection. After this call returns, this collection will + * contain no elements in common with the specified other collection. + * + * @param otherCollection collection containing elements to be removed + * from this collection + */ + @Override + public void removeAll(Collection otherCollection) { + + } + + /** + * Retains only the items in this collection that are contained in the + * specified other collection. In other words, removes from this collection + * all of its items that are not contained in the specified other collection + * + * @param otherCollection collection containing elements to be retained in + * this collection + */ + @Override + public void retainAll(Collection otherCollection) { + + } + + /** + * Returns the item at the specified position in this list + * + * @param index index of the item to return + * @return the item at the specified position in this list + * @throws IndexOutOfBoundsException if this index is out of range + * (index < 0 || index >= size()) + */ + @Override + public ItemType get(int index) { + if(index >= size){ + throw new IndexOutOfBoundsException("index is beyond size"); + } + return data[index]; + } + + /** + * Replaces the item at the specified position in this list + * with the specified item + * + * @param index index of the item to replace + * @param item item to be stored at the specified position + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void set(int index, ItemType item) { + if(index >= size){ + throw new IndexOutOfBoundsException("index is beyond size"); + } + data[index] = item; + } + + + + public void addToFront(ItemType item) { + // create larger array just in case + ItemType[] temp = (ItemType[]) new Object[size * 2]; + for (int i = 0; i < size; i++) { + temp[i] = data[i]; + } + size++; + + temp[0] = item; + for (int i = 1; i < size; i++) { + temp[i] = data[i - 1]; + } + data = temp; + } + + public void addToBack(ItemType item) { + // create larger array just in case + ItemType[] temp = (ItemType[]) new Object[size * 2]; + for (int i = 0; i < size; i++) { + temp[i] = data[i]; + } + size++; + + temp[size - 1] = item; + data = temp; + } + + /** + * Inserts the specified item at the specified position in this list. + * Shifts the item currently at that position (if any) and any subsequent + * items to the right. + * + * @param index index at which the specified item is to be inserted + * @param item item to be inserted + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void add(int index, ItemType item) { + // focus on trying this one + + if ( index < 0 || index >= size() ){ + throw new IndexOutOfBoundsException("Please enter a valid number"); + } + if ( item == null ){ + throw new NullPointerException("Item cannot be null"); + } + + checkSize(); + + for (int i = size; i >= index + 1; i--) { + data[i] = data[i - 1]; + } + data[index] = item; + size++; + + } + + + @Override + public void remove(int index) { + // shift values left to overwrite the item at index + for (int i = index; i < size - 1; i++) { + data[i] = data[i + 1]; + } + size--; + } + + /** + * Returns the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item. + * + * @param item the item to search for + * @return the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item + * @throws NullPointerException if the specified item is null and this + * list does not permit null items + */ + @Override + public int indexOf(ItemType item) { + if ( item == null ){ + throw new NullPointerException("Item cannot be null"); + } + + for (int i = 0; i < size; i++) { + if ( item.equals(data[i])){ + return i; + } + } + // if we got here, it wasn't in the array + return -1; + } + + /** + * Returns the index of the last occurrence of the specified item + * in this list, or -1 if this list does not contain the item. + * + * @param item the item to search for + * @return the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item + * @throws NullPointerException if the specified item is null and this + * list does not permit null items + */ + @Override + public int lastIndexOf(ItemType item) { + if ( item == null ){ + throw new NullPointerException("Item cannot be null"); + } + return 0; + } + + /** + * Returns a list iterator over the elements in this list + * (in proper sequence). + * + * @return a list iterator over the elements in this list + * (in proper sequence) + */ + @Override + public ListIterator listIterator() { + return null; + } + + /** + * Resizing code ( creates larger array to add new objects to ) + */ + private void checkSize(){ + // HOWEVER: when size becomes length - we run out of room + if (size == data.length){ + // create new array, double the size, copy elements over + + // create larger array twice the size as the current + ItemType[] temp = (ItemType[]) new Object[size * 2]; + + // copy items from data array to temp array + for (int i = 0; i < size; i++) { + temp[i] = data[i]; + } + + // repoint/reference data to point to temp array + data = temp; + + // optional + temp = null; + } + } + + private class OurCustomIterator implements Iterator { + + //fields + private int currentPosition; + + + public OurCustomIterator(){ + currentPosition = 0; + } + + @Override + public boolean hasNext() { + return currentPosition < size(); + } + + + @Override + public ItemType next() { + ItemType result = get(currentPosition); + currentPosition++; + return result; + } + } + + + // listIterator to work on + private class SecondCustomIterator implements ListIterator { + // fancier Iterator - lets us go forwards and backwards + + private int currentPosition; + + public SecondCustomIterator(){ + currentPosition = 0; + } + + + @Override + public boolean hasNext() { + + return currentPosition < size(); + } + + + @Override + public ItemType next() { + ItemType result = get(currentPosition); + currentPosition++; + return result; + } + + + @Override + public boolean hasPrevious() { + // hasNext checked currentPosition with size + // hasPrevious check currentPosition against 0 + return currentPosition > 0; + } + + @Override + public ItemType previous() { + + return null; + } + + @Override + public int nextIndex() { + return 0; + } + + + @Override + public int previousIndex() { + return 0; + } + + /** + * Removes from the list the last element that was returned by {@link + * #next} or {@link #previous} (optional operation). This call can + * only be made once per call to {@code next} or {@code previous}. + * It can be made only if {@link #add} has not been + * called after the last call to {@code next} or {@code previous}. + * + * @throws UnsupportedOperationException if the {@code remove} + * operation is not supported by this list iterator + * @throws IllegalStateException if neither {@code next} nor + * {@code previous} have been called, or {@code remove} or + * {@code add} have been called after the last call to + * {@code next} or {@code previous} + */ + @Override + public void remove() { + + } + + /** + * Replaces the last element returned by {@link #next} or + * {@link #previous} with the specified element (optional operation). + * This call can be made only if neither {@link #remove} nor {@link + * #add} have been called after the last call to {@code next} or + * {@code previous}. + * + * @param itemType the element with which to replace the last element returned by + * {@code next} or {@code previous} + * @throws UnsupportedOperationException if the {@code set} operation + * is not supported by this list iterator + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this list + * @throws IllegalArgumentException if some aspect of the specified + * element prevents it from being added to this list + * @throws IllegalStateException if neither {@code next} nor + * {@code previous} have been called, or {@code remove} or + * {@code add} have been called after the last call to + * {@code next} or {@code previous} + */ + @Override + public void set(ItemType itemType) { + + } + + /** + * Inserts the specified element into the list (optional operation). + * The element is inserted immediately before the element that + * would be returned by {@link #next}, if any, and after the element + * that would be returned by {@link #previous}, if any. (If the + * list contains no elements, the new element becomes the sole element + * on the list.) The new element is inserted before the implicit + * cursor: a subsequent call to {@code next} would be unaffected, and a + * subsequent call to {@code previous} would return the new element. + * (This call increases by one the value that would be returned by a + * call to {@code nextIndex} or {@code previousIndex}.) + * + * @param itemType the element to insert + * @throws UnsupportedOperationException if the {@code add} method is + * not supported by this list iterator + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this list + * @throws IllegalArgumentException if some aspect of this element + * prevents it from being added to this list + */ + @Override + public void add(ItemType itemType) { + + } + } + +} diff --git a/src/edu/greenriver/sdev333/RecursiveLinkedList.java b/src/edu/greenriver/sdev333/RecursiveLinkedList.java new file mode 100644 index 0000000..f488a5c --- /dev/null +++ b/src/edu/greenriver/sdev333/RecursiveLinkedList.java @@ -0,0 +1,305 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; +import java.util.ListIterator; + +public class RecursiveLinkedList implements List { + + // private helper class + private class Node { + ItemType data; + Node next; + } + + + // fields + private Node head; + + public RecursiveLinkedList(){ + head = null; + } + + + + + /** + * Returns the number of items in this collection. + * + * @return the number of items in this collection + */ + @Override + public int size() { + return size(head); + } + + // private helper method that does the actual + //work + private int size (Node theHead) { + if( theHead == null ){ + return 0; + } + else { + // VV "the rest" + return size(theHead.next) + 1; + } + } + + /** + * Returns true if this collection contains no items. + * + * @return true if this collection contains no items + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Returns true if this collection contains the specified item. + * + * @param item items whose presence in this collection is to be tested + * @return true if this collection contains the specified item + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public boolean contains(ItemType item) { + return false; + } + + /** + * Returns an iterator over the elements in this collection. + * + * @return an Iterator over the elements in this collection + */ + @Override + public Iterator iterator() { + return null; + } + + /** + * Adds the specified item to the collection. + * + * @param item item to be added to the collection + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void add(ItemType item) { + // adds an item to back + // loopy way: + // - created a current variable and set it the head value + // - wrote a loop while (current.next != null) + // - once we are at last node, create one and attach it + head = helpAdd(head, item); + + } + + private Node helpAdd(Node theHead, ItemType item){ + // if I'm at the end / hit bottom + if (theHead == null){ + Node theNewNode = new Node(); + theNewNode.data = item; + theNewNode.next = null; + return theNewNode; + } + else { + // not at end/bottom + theHead.next = helpAdd(theHead.next, item); + return theHead; + } + } + + + + /** + * Removes a single instance of the specified item from this collection, + * if it is present. + * + * @param item item to be removed from this collection, if present + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void remove(ItemType item) { + + } + + /** + * Removes all items from this collection. + * The collection will be empty after this method returns. + */ + @Override + public void clear() { + + } + + /** + * Returns true if this collection contains all the items + * in the specified other collection. + * + * @param otherCollection collection to be checked for containment in this collection + * @return true if this collection contains all the items + * in the specified other collection + */ + @Override + public boolean containsAll(Collection otherCollection) { + return false; + } + + /** + * Adds all the items in this specified other collection to this collection. + * + * @param otherCollection collection containing items to be added to this collection + */ + @Override + public void addAll(Collection otherCollection) { + + } + + /** + * Removes all of this collection's items that are also contained in the + * specified other collection. After this call returns, this collection will + * contain no elements in common with the specified other collection. + * + * @param otherCollection collection containing elements to be removed + * from this collection + */ + @Override + public void removeAll(Collection otherCollection) { + + } + + /** + * Retains only the items in this collection that are contained in the + * specified other collection. In other words, removes from this collection + * all of its items that are not contained in the specified other collection + * + * @param otherCollection collection containing elements to be retained in + * this collection + */ + @Override + public void retainAll(Collection otherCollection) { + + } + + /** + * Returns the item at the specified position in this list + * + * @param index index of the item to return + * @return the item at the specified position in this list + * @throws IndexOutOfBoundsException if this index is out of range + * (index < 0 || index >= size()) + */ + @Override + public ItemType get(int index) { + return null; + } + + /** + * Replaces the item at the specified position in this list + * with the specified item + * + * @param index index of the item to replace + * @param item item to be stored at the specified position + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void set(int index, ItemType item) { + + } + + /** + * Inserts the specified item at the specified position in this list. + * Shifts the item currently at that position (if any) and any subsequent + * items to the right. + * + * @param index index at which the specified item is to be inserted + * @param item item to be inserted + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void add(int index, ItemType item) { + // add (0, item) + if ( index == 0 ) { + // add to the front list may or may not be empy + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = head; + head = theNewOne; + } + else { + // anywhere else other than 0 + Node current = head; + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = current.next; + current.next = theNewOne; + } + + } + + /** + * Removes the element at the specified position in this list. + * Shifts any subsequent items to the left. + * + * @param index the index of the item to be removed + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void remove(int index) { + + } + + /** + * Returns the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item. + * + * @param item the item to search for + * @return the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item + * @throws NullPointerException if the specified item is null and this + * list does not permit null items + */ + @Override + public int indexOf(ItemType item) { + return 0; + } + + /** + * Returns the index of the last occurrence of the specified item + * in this list, or -1 if this list does not contain the item. + * + * @param item the item to search for + * @return the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item + * @throws NullPointerException if the specified item is null and this + * list does not permit null items + */ + @Override + public int lastIndexOf(ItemType item) { + return 0; + } + + /** + * Returns a list iterator over the elements in this list + * (in proper sequence). + * + * @return a list iterator over the elements in this list + * (in proper sequence) + */ + @Override + public ListIterator listIterator() { + return null; + } +} diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java new file mode 100644 index 0000000..cd5bd86 --- /dev/null +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -0,0 +1,643 @@ +package edu.greenriver.sdev333; + +import javax.naming.OperationNotSupportedException; +import java.util.Iterator; +import java.util.ListIterator; +import java.util.NoSuchElementException; + +public class SinglyLinkedList implements List { + + // FIELDS - what does a linked list actually have in it?? + private Node head; + private int size; + + // helper/inner classes + private class Node{ + ItemType data; + Node next; + } + public SinglyLinkedList(){ + // an empty list has no nodes, + // which means it has no head, so set head to null + head = null; + } + + + + + /** + * Returns the number of items in this collection. + * + * @return the number of items in this collection + */ + @Override + public int size() { + return size; + } + + /** + * Returns true if this collection contains no items. + * + * @return true if this collection contains no items + */ + @Override + public boolean isEmpty() { + + return size == 0; + } + + /** + * Returns true if this collection contains the specified item. + * + * @param item items whose presence in this collection is to be tested + * @return true if this collection contains the specified item + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public boolean contains(ItemType item) { + // assume indexOf is working... + int position = indexOf(item); + if ( position == -1 ) { + return false; + } + return true; + } + + /** + * Returns an iterator over the elements in this collection. + * + * @return an Iterator over the elements in this collection + */ + @Override + public Iterator iterator() { + + return new OurCustomIterator(); + } + + + public void addToBack(ItemType item) { + + Node current = head; + // stop one before the position I want to insert at + for (int i = 0; i < size - 1; i++) { + current = current.next; + } + + // when I get here, current is pointing to the node + // BEFORE the one at the index + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = current.next; + + current.next = theNewOne; + size++; + } + + + public void addToFront(ItemType item) { + + Node nodeToAdd = new Node(); + nodeToAdd.data = item; + nodeToAdd.next = head; + head = nodeToAdd; + size++; + } + /** + * Adds the specified item to the collection. + * + * @param item item to be added to the collection + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void add(ItemType item) { + if( head == null ){ + Node newNode = new Node(); + newNode.data = item; + newNode.next = null; + head = newNode; + } + else { + Node current = head; + while (current.next != null){ + current = current.next; + } + Node newNode = new Node(); + newNode.data = item; + newNode.next = null; + current.next = newNode; + + size++; + + } + + } + + /** + * Removes a single instance of the specified item from this collection, + * if it is present. + * + * @param item item to be removed from this collection, if present + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void remove(ItemType item) { + if (item == null) { + throw new NullPointerException(); + } + + /* // alternative, easier but less efficient + int position = indexOf(item); + if( position != -1 ) { + remove(position); + }*/ + + if(head.data == item) { + head = head.next; + size--; + } + else { + Node current = head; + Node previous; + while (current.next != null){ + previous = current; + current = current.next; + + if(current.data.equals(item)) { + previous.next = current.next; + size--; + } + } + } + } + + /** + * Removes all items from this collection. + * The collection will be empty after this method returns. + */ + @Override + public void clear() { + head = null; + size = 0; + } + + /** + * Returns true if this collection contains all the items + * in the specified other collection. + * + * @param otherCollection collection to be checked for containment in this collection + * @return true if this collection contains all the items + * in the specified other collection + */ + @Override + public boolean containsAll(Collection otherCollection) { + return false; + } + + /** + * Adds all the items in this specified other collection to this collection. + * + * @param otherCollection collection containing items to be added to this collection + */ + @Override + public void addAll(Collection otherCollection) { + // walk through the other collection + // for-each loop or use Iterator + Iterator itr = (Iterator) otherCollection.iterator(); + while (itr.hasNext()) { + ItemType currentItem = itr.next(); + add(0, currentItem); + } + + } + + /** + * Removes all of this collection's items that are also contained in the + * specified other collection. After this call returns, this collection will + * contain no elements in common with the specified other collection. + * + * @param otherCollection collection containing elements to be removed + * from this collection + */ + @Override + public void removeAll(Collection otherCollection) { + + } + + /** + * Retains only the items in this collection that are contained in the + * specified other collection. In other words, removes from this collection + * all of its items that are not contained in the specified other collection + * + * @param otherCollection collection containing elements to be retained in + * this collection + */ + @Override + public void retainAll(Collection otherCollection) { + + } + + /** + * Returns the item at the specified position in this list + * + * @param index index of the item to return + * @return the item at the specified position in this list + * @throws IndexOutOfBoundsException if this index is out of range + * (index < 0 || index >= size()) + */ + @Override + public ItemType get(int index) { + if ( index < 0 || index >= size ){ + throw new IndexOutOfBoundsException(); + } + + Node current = head; + int counter = 0; + while ( counter != index ){ + current = current.next; + counter++; + } + return current.data; + } + + /** + * Replaces the item at the specified position in this list + * with the specified item + * + * @param index index of the item to replace + * @param item item to be stored at the specified position + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void set(int index, ItemType item) { + if ( index < 0 || index >= size ){ + throw new IndexOutOfBoundsException(); + } + + Node current = head; + Node temp = current; + int counter = 0; + while ( counter != index ){ + temp = current; + current = current.next; + counter++; + } + // need to finish + current.data.equals(item); + current.next = temp; + + } + + /** + * Inserts the specified item at the specified position in this list. + * Shifts the item currently at that position (if any) and any subsequent + * items to the right. + * + * @param index index at which the specified item is to be inserted + * @param item item to be inserted + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void add(int index, ItemType item) { + checkIndex(index); + + if( index == 0 ) { + // if someone wants to add at the beginning, I need + // to change the head + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = head; + + head = theNewOne; + } + else { + Node current = head; + // stop one before the position I want to insert at + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + + // when I get here, current is pointing to the node + // BEFORE the one at the index + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = current.next; + + current.next = theNewOne; + } + size++; + + } + + + private void checkIndex( int index) { + if ( index < 0 || index >= size ) { + throw new IndexOutOfBoundsException(); + } + } + + /** + * Removes the element at the specified position in this list. + * Shifts any subsequent items to the left. + * + * @param index the index of the item to be removed + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void remove(int index) { + checkIndex(index); + + if ( index == 0) { + head = head.next; + } + + else { + Node current = head; + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + + // here, current is pointing to the node BEFORE the one + // at index + current.next = current.next.next; + + size--; + } + + } + + /** + * Returns the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item. + * + * @param item the item to search for + * @return the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item + * @throws NullPointerException if the specified item is null and this + * list does not permit null items + */ + @Override + public int indexOf(ItemType item) { + int counter = 0; + Node current = head; + while ( current != null ) { + if ( current.data.equals(item) ) { + return counter; + } + counter++; + current = current.next; + } + return -1; + } + + /** + * Returns the index of the last occurrence of the specified item + * in this list, or -1 if this list does not contain the item. + * + * @param item the item to search for + * @return the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item + * @throws NullPointerException if the specified item is null and this + * list does not permit null items + */ + @Override + public int lastIndexOf(ItemType item) { + return 0; + } + + /** + * Returns a list iterator over the elements in this list + * (in proper sequence). + * + * @return a list iterator over the elements in this list + * (in proper sequence) + */ + @Override + public ListIterator listIterator() { + return new OurEnhancedIterator(); + } + + private class OurCustomIterator implements Iterator { + + // field + private Node currentPosition; + + public OurCustomIterator() { + currentPosition = head; + } + + + /** + * Returns {@code true} if the iteration has more elements. + * (In other words, returns {@code true} if {@link #next} would + * return an element rather than throwing an exception.) + * + * @return {@code true} if the iteration has more elements + */ + @Override + public boolean hasNext() { + // see if I'm on the last node: if (current.next == null) + // see if I made it past the last node: if (current == null) + if (currentPosition != null){ + return true; + } + + return false; + } + + /** + * Returns the next element in the iteration. + * + * @return the next element in the iteration + * @throws NoSuchElementException if the iteration has no more elements + */ + @Override + public ItemType next() { + ItemType result = currentPosition.data; + currentPosition = currentPosition.next; + return result; + } + } + + + private class OurEnhancedIterator implements ListIterator { + + private Node currentPosition; + private int currentIndex; + + public OurEnhancedIterator(){ + currentPosition = head; + currentIndex = 0; + } + + + + /** + * Returns {@code true} if this list iterator has more elements when + * traversing the list in the forward direction. (In other words, + * returns {@code true} if {@link #next} would return an element rather + * than throwing an exception.) + * + * @return {@code true} if the list iterator has more elements when + * traversing the list in the forward direction + */ + @Override + public boolean hasNext() { + return currentPosition != null; + } + + /** + * Returns the next element in the list and advances the cursor position. + * This method may be called repeatedly to iterate through the list, + * or intermixed with calls to {@link #previous} to go back and forth. + * (Note that alternating calls to {@code next} and {@code previous} + * will return the same element repeatedly.) + * + * @return the next element in the list + * @throws NoSuchElementException if the iteration has no next element + */ + @Override + public ItemType next() { + ItemType result = currentPosition.data; + currentPosition = currentPosition.next; + return result; } + + /** + * Returns {@code true} if this list iterator has more elements when + * traversing the list in the reverse direction. (In other words, + * returns {@code true} if {@link #previous} would return an element + * rather than throwing an exception.) + * + * @return {@code true} if the list iterator has more elements when + * traversing the list in the reverse direction + */ + @Override + public boolean hasPrevious() { + return false; + } + + /** + * Returns the previous element in the list and moves the cursor + * position backwards. This method may be called repeatedly to + * iterate through the list backwards, or intermixed with calls to + * {@link #next} to go back and forth. (Note that alternating calls + * to {@code next} and {@code previous} will return the same + * element repeatedly.) + * + * @return the previous element in the list + * @throws NoSuchElementException if the iteration has no previous + * element + */ + @Override + public ItemType previous() { + return null; + } + + /** + * Returns the index of the element that would be returned by a + * subsequent call to {@link #next}. (Returns list size if the list + * iterator is at the end of the list.) + * + * @return the index of the element that would be returned by a + * subsequent call to {@code next}, or list size if the list + * iterator is at the end of the list + */ + @Override + public int nextIndex() { + return 0; + } + + /** + * Returns the index of the element that would be returned by a + * subsequent call to {@link #previous}. (Returns -1 if the list + * iterator is at the beginning of the list.) + * + * @return the index of the element that would be returned by a + * subsequent call to {@code previous}, or -1 if the list + * iterator is at the beginning of the list + */ + @Override + public int previousIndex() { + return 0; + } + + /** + * Removes from the list the last element that was returned by {@link + * #next} or {@link #previous} (optional operation). This call can + * only be made once per call to {@code next} or {@code previous}. + * It can be made only if {@link #add} has not been + * called after the last call to {@code next} or {@code previous}. + * + * @throws UnsupportedOperationException if the {@code remove} + * operation is not supported by this list iterator + * @throws IllegalStateException if neither {@code next} nor + * {@code previous} have been called, or {@code remove} or + * {@code add} have been called after the last call to + * {@code next} or {@code previous} + */ + @Override + public void remove() { + + } + + /** + * Replaces the last element returned by {@link #next} or + * {@link #previous} with the specified element (optional operation). + * This call can be made only if neither {@link #remove} nor {@link + * #add} have been called after the last call to {@code next} or + * {@code previous}. + * + * @param itemType the element with which to replace the last element returned by + * {@code next} or {@code previous} + * @throws UnsupportedOperationException if the {@code set} operation + * is not supported by this list iterator + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this list + * @throws IllegalArgumentException if some aspect of the specified + * element prevents it from being added to this list + * @throws IllegalStateException if neither {@code next} nor + * {@code previous} have been called, or {@code remove} or + * {@code add} have been called after the last call to + * {@code next} or {@code previous} + */ + @Override + public void set(ItemType itemType) { + + } + + /** + * Inserts the specified element into the list (optional operation). + * The element is inserted immediately before the element that + * would be returned by {@link #next}, if any, and after the element + * that would be returned by {@link #previous}, if any. (If the + * list contains no elements, the new element becomes the sole element + * on the list.) The new element is inserted before the implicit + * cursor: a subsequent call to {@code next} would be unaffected, and a + * subsequent call to {@code previous} would return the new element. + * (This call increases by one the value that would be returned by a + * call to {@code nextIndex} or {@code previousIndex}.) + * + * @param itemType the element to insert + * @throws UnsupportedOperationException if the {@code add} method is + * not supported by this list iterator + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this list + * @throws IllegalArgumentException if some aspect of this element + * prevents it from being added to this list + */ + @Override + public void add(ItemType itemType) { + + } + } + +}