diff --git a/.idea/misc.xml b/.idea/misc.xml index 07115cd..05b1176 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file 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..46dd41e 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,252 @@ +import edu.greenriver.sdev333.*; +import java.util.Iterator; +import java.util.ListIterator; + public class Main { - public static void main(String[] args) { - System.out.println("Hello world!"); + public static void main(String[] args) + { +/* System.out.println("Hello world!"); + + List friends = new ArrayList(); + System.out.println("Initial size is " + friends.size()); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + friends.add("Susan"); + friends.add("Tyler"); + friends.add("Usman"); + friends.add("Dee"); + friends.add("Rose"); + friends.add("Blanche"); + friends.add("Dorothy"); + friends.add("Sophia"); + friends.add(2,"Wednesday"); + System.out.println("size is now " + friends.size()); + //for (int i = 0; i < friends.size(); i ++) + // System.out.println(friends.get(i)) + Iterator itr = friends.iterator(); + while(itr.hasNext()) + { + String name = itr.next(); + System.out.println(name); + } + for(String name : friends) + { + System.out.println(name); + }*/ + + //test (Method main (or jUnit tests) need to be present to demonstrate that you have tested what you have wrote) + +// //Constructor: ArrayList() +// List friends = new ArrayList(); +// +// //public void add(ItemType item) +// friends.add("Jack"); +// +// //public int size() Should return 1 +// System.out.println("Size of the Array : " + friends.size()); +// +// //public boolean isEmpty() Should return false +// System.out.println("Is the Array Empty? " + friends.isEmpty()); +// +// //public boolean contains(ItemType item) - hint: this method can use indexOf to do its work +// System.out.println("Does the Array contain Jack? " + friends.contains("Jack")); +// +// //public int indexOf(ItemType item) +// System.out.println("Index of 'Jack' is " + friends.indexOf("Jack")); +// +// //public void add(int index, ItemType item) +// friends.add(0,"Ken"); +// System.out.println("Check if Ken is added to my frendsList, Ken's index is " + friends.indexOf("Ken")); +// System.out.println("Index of 'Jack' is " + friends.indexOf("Jack")); +// +// //public void remove(int index) +// friends.remove(1); +// //test this method +// System.out.println("Is Jack still in the List? " + friends.contains("Jack")); +// +// //public ItemType get(int index) +// System.out.println(friends.get(0)); +// +// //public void set(int index, ItemType item) +// friends.set(0,"Jack"); +// System.out.println("Now Ken changed to " + friends.get(0)); +// +// //public void clear() +// friends.clear(); +// System.out.println("Nothing in the Array List, the size of Friends is " + friends.size()); +// +// //public Iterator iterator() +// friends.add("Jack"); +// friends.add("Ken"); +// friends.add("Brandon"); +// Iterator itr = friends.iterator(); +// while(itr.hasNext()) +// { +// String name = itr.next(); +// System.out.println(name); +// } +// +// //Level 2 Requirements (to earn beyond 75-80% credit, up to 100%, if working properly) +// //Second constructor: ArrayList(int capacity) - allows the client to ask for an initial capacity for the underlying data array (instead of using our default of 10) +// List friend = new ArrayList(5); +// friend.add("JackOne"); +// System.out.println("Is JackOne in the List? " + friend.contains("JackOne")); +// +// //public void remove(ItemType item) - hint: this method can use indexOf, then remove(index) to do its work +// friend.remove("JackOne"); +// System.out.println("Is JackOne in the List? " + friend.contains("JackOne")); +// +// //**public int lastIndexOf(ItemType item) +// friends.add("Jack"); +// System.out.println("the last index of Jack should be 3: which is : " + friends.lastIndexOf("Jack")); +// +// //**public void addAll(Collection otherCollection) +// friend.add("Jack"); +// friend.add("Ken"); +// friend.add("Brandon"); +// friends.addAll(friend); +// +// //**public boolean equals(Object otherObject) +// System.out.println(friends.equals(friend)); +// friend.clear(); +// friends.clear(); +// System.out.println(friends.equals(friend)); +// friends.add("Jack"); +// friend.add("Jack"); +// System.out.println(friends.equals(friend)); +// //public ListIterator listIterator() +// friends.add("Ken"); +// friends.add("Brandon"); +// ListIterator itrL = friends.listIterator(); +// while(itrL.hasNext()) +// { +// String name = itrL.next(); +// System.out.println(name); +// System.out.println(itrL.nextIndex()); +// } +// while(itrL.hasPrevious()) +// { +// String name = itrL.previous(); +// System.out.println(name); +// System.out.println(itrL.previousIndex()); +// } + + //**Move resizing code into its own private method, something like: private void resize(int newSize) + //Move code to validate indexes into its own private method, something like: private boolean isValidIndex(int index) + // those two methods are written in ArrayList. + + + + + + /*SinglyLinkedList + * Level 1 Requirements (to earn 75-80% credit if working properly) + Provide an implementation for the following methods: +* */ + //Constructor: LinkedList() + SinglyLinkedList friendss = new SinglyLinkedList(); + + //public void add(ItemType item) + friendss.add("Jack"); + + //public int size() + System.out.println(friendss.size()); + + //public boolean isEmpty() + System.out.println(friendss.isEmpty()); + + //public boolean contains(ItemType item) - hint: this method can use indexOf to do its work + System.out.println("Does the SinglyLinkedList contain Jack? : " + friendss.contains("Jack")); + + //public int indexOf(ItemType item) + System.out.println(friendss.indexOf("Jack")); + + //public void add(ItemType item) + friendss.add("Brandon"); + + //public void add(int index, ItemType item) + friendss.add(1,"John"); + + //public void remove(int index) Jack should be removed + friendss.remove(0); + + //public ItemType get(int index) + System.out.println(friendss.get(0)); + + //public void set(int index, ItemType item) + friendss.set(0,"Josh"); + + //public Iterator iterator() + System.out.println(); + Iterator itr = friendss.iterator(); + while(itr.hasNext()) + { + String name = itr.next(); + System.out.println(name); + } + //public void clear() + friendss.clear(); + System.out.println(friendss.size()); + + //Level 2 Requirements (to earn beyond 75-80% credit, up to 100%, if working properly) + + //public void remove(ItemType item) - hint: this method can use indexOf, then remove(index) to do its work + System.out.println(); + friendss.add("Jack"); + friendss.add("Josh"); + friendss.add("Brandon"); + friendss.remove("Jack"); + for(String name : friendss) + { + System.out.println(name); + } + + //**public int lastIndexOf(ItemType item) + //the Index of Josh should be 2 + friendss.add("Josh"); + System.out.println("The lastIndexOf Josh should be 2 : " + friendss.lastIndexOf("Josh")); + + //**public void addAll(Collection otherCollection) + SinglyLinkedList friendsss = new SinglyLinkedList<>(); + friendsss.add("Josh"); + friendsss.add("Brandon"); + friendsss.add("Josh"); + friendss.addAll(friendsss); + System.out.println(); + for (String name : friendss) + { + System.out.println(name); + } + + //**public boolean equals(Object otherObject) + System.out.println(); + System.out.println(friendss.equals(friendsss)); + friendsss.add("Josh"); + friendsss.add("Brandon"); + friendsss.add("Josh"); + System.out.println(); + System.out.println(friendss.equals(friendsss)); + + //public ListIterator listIterator() + System.out.println(); + ListIterator itrOne = friendsss.listIterator(); + while(itrOne.hasNext()) + { + String name = itrOne.next(); + System.out.println(name); + } + + //Move code to validate indexes into its own private method, something like: private boolean isValidIndex(int index) + /* + private void checkIndex(int index) + { + if (index < 0 || index >= size) + { + throw new IndexOutOfBoundsException(); + } + } + */ } + } \ 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..93c926d --- /dev/null +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -0,0 +1,501 @@ +package edu.greenriver.sdev333; + +import java.lang.reflect.Array; +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 the # of spots that are used in the data array + // size is different than length + private int size; + + + public ArrayList() { + size = 0; + data = (ItemType[]) new Object[10]; + } + + public ArrayList(int capacity) { + 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() { + if (size == 0) { + return true; + } + return false; + + //alternate way to write it : 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) { + 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(); + } + + /*** Move resizing code into its own private method, something like: private void resize(int newSize) */ + private void checkSize() { + if (size == data.length) { + //resize up (double up the array size) + + // step 1 - create a new larger array + ItemType[] temp = (ItemType[]) new Object[size * 2]; + + // Step 2 = copy items from data to temp + for (int i = 0; i < data.length; i++) { + temp[i] = data[i]; + } + + //Step 3 - repoint/ reference data to point to new array + data = temp; + + // Optional: + //overwrite + temp = null; + } //end of if (need to resize) + }// end of method + /** + * 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) { + checkSize(); + data[size] = item; + size++; + + }// end of method + public void addToFront(ItemType item) + { + checkSize(); + ItemType[] temp = (ItemType[]) new Object[data.length+1]; + temp[0] = item; + for(int i =0; i otherCollection) { + // fail fast (fail loud) + Iterator itr = (Iterator) otherCollection.iterator(); + while (itr.hasNext()) { + ItemType itemToCheck = itr.next(); + if (!contains(itemToCheck)) { + return false; + } + } + return true; + + +/* for(ItemType itemtoCheck : otherCollection) + { + if(!contains(itemTocheck)) + { + return false; + } + }*/ + // 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) { + int num = 0; + List dataOne = new ArrayList(otherCollection.size()); + Iterator itr = (Iterator) otherCollection.iterator(); + while(itr.hasNext()) + { + checkSize(); + ItemType name = itr.next(); + dataOne.add(name); + data[size] = dataOne.get(num); + size++; + num++; + } + + } + + /** + * 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) { + isValidIndex(index); + return data[index]; + } + + + /*Move code to validate indexes into its own private method, something like: private boolean isValidIndex(int index)*/ + + private boolean isValidIndex(int index) + { + if (index < 0 || index >= size()) { + throw new IndexOutOfBoundsException("index is beyond size"); + } + return true; + } + /** + * 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) { + isValidIndex(index); + + data[index] = 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) { + checkSize(); + //focus on trying this one ... + for (int i = size; i >= index + 1; i--) { + data[i] = data[i - 1]; + } + data[index] = item; + size++; + } + + /** + * 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) { + //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) { + + 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(); + } + int element = -1; + for (int i = 0; i < size; i++) { + if (item.equals(data[i])) { + element = i; + } + } + return element; + } + + /** + * 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 SecondCustomIterator(); + } + + @Override + public boolean equals(Object otherObject) + { + if(otherObject == this) + { + return true; + } + if (!(otherObject instanceof ArrayList)) { + return false; + } + ArrayList other = (ArrayList) otherObject; + + if(size != other.size) + { + return false; + } + + for(int i =0; i { + + //fields + private int currentPosition; + + public OurCustomIterator() { + currentPosition = 0; + } + /** + * 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() { + return currentPosition < size(); + } + + /** + * 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 = get(currentPosition); + currentPosition++; + return result; + } + + } + + private class SecondCustomIterator implements ListIterator { + + + 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() { + return currentPosition > 0; + } + + @Override + public ItemType previous() { + currentPosition--; + ItemType result = get(currentPosition); + return result; + } + + @Override + public int nextIndex() { + int result = currentPosition; + return result; + } + + @Override + public int previousIndex() { + int result = currentPosition; + return result; + } + @Override + public void remove() { + + } + + @Override + public void set(ItemType itemType) { + + } + + @Override + public void add(ItemType itemType) { + + } + } + +}// end of class ArrayList diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java new file mode 100644 index 0000000..16a50bf --- /dev/null +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -0,0 +1,575 @@ +package edu.greenriver.sdev333; + +import javax.naming.OperationNotSupportedException; +import java.util.Iterator; +import java.util.ListIterator; + +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; + } + + /** + * Constructor + */ + public SinglyLinkedList() { + // an empty list has no nodes, + // which means it has no head, so set head to null + head = null; + size = 0; + } + + /** + * 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(); + } + + /** + * 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 + /* + * 1 create the node + * put data in node + * set next to null + * head gets the addr of new node + * size++ + * */ + public void add(ItemType item) { + //if size ==0 + //the index at the end of the list is size -1 + //example: if list is size 5, last index is 4 + //so we can just insert at the last index + //add(size()-1,item); + if (item == null) { + throw new NullPointerException(); + } + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = null; + if (head == null) { + head = theNewOne; + } else { + //get to back. last item in list + Node current = head; + while (current.next != null) { + current = current.next; + } + //current is pointing to last node + //add new node at end + current.next = theNewOne; + //add new node at end + } + size++; + + } +/* public void addToTheBack(ItemType item) + { + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = null; + if (head == null) { + head = theNewOne; + } else { + //get to back. last item in list + Node current = head; + while (current.next != null) { + current = current.next; + } + //current is pointing to last node + //add new node at end + current.next = theNewOne; + //add new node at end + } + size++; + } + + public void addToTheHead(ItemType item) { + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = head; + head = theNewOne; + 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 to write, 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) { +/* for(ItemType item : otherCollection) + { + if(!this.contains(item)) + { + return false; + } + } + return true;*/ + + Iterator itr = (Iterator) otherCollection.iterator(); + while (itr.hasNext()) { + ItemType item = itr.next(); + if (!contains(item)) { + 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) { + //walk through the other collection + // for-each loop + // or use Iterator + + Iterator itr = (Iterator) otherCollection.iterator(); + while (itr.hasNext()) { + ItemType currentItem = itr.next(); + this.add(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) { + checkIndex(index); + if (item == null) { + throw new NullPointerException(); + } + if (index == 0) { + // if someone wants to add at the beginning, i need to change the head + head.data = item; + } else { + Node current = head; + //stop at the position i want to insert at + for (int i = 0; i < index; i++) { + current = current.next; + } + current.data = 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) { + 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 the node *BEFORE* the node 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; + } + + //when I get here - current is pointing to the node BEFORE the on 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; + } + //if we get here, it's not found + 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(); + } + int element = -1; + int counter = -1; + Node current = head; + while (current != null) { + element++; + if (current.data.equals(item)) { + counter = element; + } + current = current.next; + } + + return counter; + } + + @Override + public boolean equals(Object otherObject) { + if (otherObject == null) { + return false; + } + SinglyLinkedList other = (SinglyLinkedList) otherObject; + if (size != other.size) { + return false; + } + + Node current = head; + Node currentOne = other.head; + while (current != null) { + if (!current.data.equals(currentOne.data)) { + return false; + } + current = current.next; + currentOne = currentOne.next; + } + + return true; + } + + /** + * 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; + } + + @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; + } + + @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; + } + + @Override + public boolean hasNext() { + return currentPosition != null; + } + + @Override + public ItemType next() { + ItemType result = currentPosition.data; + currentPosition = currentPosition.next; + return result; + } + + @Override + public boolean hasPrevious() { + throw new UnsupportedOperationException(); + } + + @Override + public ItemType previous() { + throw new UnsupportedOperationException(); + } + + @Override + public int nextIndex() { + throw new UnsupportedOperationException(); + } + + @Override + public int previousIndex() { + throw new UnsupportedOperationException(); + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + + @Override + public void set(ItemType itemType) { + throw new UnsupportedOperationException(); + } + + @Override + public void add(ItemType itemType) { + throw new UnsupportedOperationException(); + } + } + +} \ No newline at end of file