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..6a3d4f7 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,162 @@ +import edu.greenriver.sdev333.Arraylist; +import edu.greenriver.sdev333.List; +import edu.greenriver.sdev333.SinglyLinkedList; + +import java.util.Iterator; +import java.util.ListIterator; + +import javax.imageio.metadata.IIOMetadataFormatImpl; + public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + //System.out.println("Hello world!"); + + + List friends = new SinglyLinkedList(); + + System.out.println(); + System.out.println(friends); + System.out.println("Friends SingleyLinkedList initial size is " + friends.size()); + System.out.println("Is friends SingleyLinkedList currently empty:"+friends.isEmpty()); + + System.out.println("Adding friends..."); + System.out.println(); + + + friends.add("Jess"); + friends.add("adam"); + friends.add("kevin"); + friends.add("carl"); + friends.add("alex"); + friends.add("tony"); + friends.add("meadow"); + + System.out.println("Is friends SingleyLinkedlist still empty:"+friends.isEmpty()); + System.out.println(); + + Iterator itr = friends.iterator(); + while(itr.hasNext()){ + String name = itr.next(); + System.out.println(name); + } + + // this is not implemented yet! + ListIterator fancyItr = friends.listIterator(); + while(itr.hasNext()){ + String name = itr.next(); + System.out.println(name); + } + System.out.println(); + while(fancyItr.hasPrevious()){ + String name = fancyItr.previous(); + System.out.println(name); + } + + + System.out.println("The friends SingleyLinkedList returns true if Jess in found within: " +friends.contains("Jess")); + + System.out.println(); + System.out.println("friends:"+friends); + + System.out.println(); + System.out.println("size is now " + friends.size()); + System.out.println("The index of Jess is " +friends.indexOf("Jess")); + friends.remove("Jess"); + friends.remove(0); + System.out.println( " Bye-Bye Jess and adam"); + System.out.println("what , did jess and adam actually leave? "+friends); + System.out.println("looks like it"); + System.out.println(" oh crap its the cops, everybody scram!");friends.clear(); + System.out.println(friends); + System.out.println("lets add some new friends"); + friends.add(0,"Kenny"); + friends.add(1,"Ty"); + friends.add(0,"Bobby"); + System.out.println(friends); + System.out.println("Kenny is my second friend because he sits in position:"+friends.indexOf("Kenny")); + System.out.println("And "+friends.get(0)+" is my favorite friend because they sit in position: "+friends.indexOf(friends.get(0))); + System.out.println("Actually my new second best friend is Cole now"); + friends.set(1,"Cole"); + System.out.println(friends); + List nemesis = new SinglyLinkedList(); + nemesis.addAll(friends); + System.out.println("oh no, my anti friends are here! they are jus tlike my friends but their order is reversed!"); + System.out.println(nemesis); + nemesis.add(0,"Bobby"); + System.out.println(nemesis); + System.out.println("what is the last index of bobby now?"+nemesis.lastIndexOf("Bobby")); + +// System.out.println(); +// System.out.println("adding friend karly to index 4"); +// friends.add(4,"karly"); +// System.out.println(friends); +// System.out.println("size is now " + friends.size()+" and it should be 9"); +// +// System.out.println(); +// System.out.println("adding frank to index 0"); +// friends.add(0,"frank"); +// System.out.println(friends); +// System.out.println("size is now " + friends.size()+" and it should be 10"); +// System.out.println(); +// +// System.out.println( "aye, some body get "+friends.get(4)); +// +// +// System.out.println("kevin is at index "+friends.indexOf("kevin")); +// friends.set(4,"parker"); +// System.out.println("wait actually " +friends.get(4) +" is at index 4, i forgot"); +// System.out.println(); +// System.out.println("removing friend at index 0, and de has got to go!"); +// friends.remove(0); +// friends.remove("de"); +// System.out.println(friends); +// System.out.println("size is now " + friends.size()); +// System.out.println(); +// +// friends.add(7,"tanner"); +// System.out.println(friends); +// System.out.println("size is now " + friends.size()); +// System.out.println("adding a few more friends"); +// friends.add("Tasha"); +// friends.add("Ozzy"); +// System.out.println("friends now has a size of "+friends.size()); +// for (int i = 0; i < 50; i++) { +// friends.add(" all the homies"); +// +// } +// System.out.println("Oh snap, all the homies came through!"); +// System.out.println("friends now has a size of "+ friends.size()); +// System.out.println("friends:"+friends); +// nemesis.clear(); +// System.out.println(""); +// System.out.println("oh crap, you just said you liked pineapple on pizza."); +// // nemesis.addAll(friends); +// System.out.println("nemesis:"+nemesis); +// friends.removeAll(nemesis); +// System.out.println("friends:"+friends); +// +// System.out.println();// seperation space +// System.out.println("all right parties over! clear out" ); +// friends.clear(); +// System.out.println("The friends array list now has a size of "+friends.size()+" because of your pizza peccadilloes"); +// +// // demoing how the iterator object is basically an enhanced forloop +//// for (int i = 0; i < friends.size(); i++) { +//// System.out.println(friends.get(i)); +//// +//// } +//// */ +//// System.out.println(); +//// +//// Iterator itr = friends.iterator(); +//// while(itr.hasNext()){ +//// String name= itr.next(); +//// System.out.println(name); +//// } +//// System.out.println(); +//// for( String name: friends){ +//// System.out.println(name); +//// } +//// // friends.addAll(); will throw exception..fail fast } } \ 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..5feca98 --- /dev/null +++ b/src/edu/greenriver/sdev333/Arraylist.java @@ -0,0 +1,494 @@ +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 the number of elements that are not null in the array + //size is different from length + private int size; + + // constructor + + public Arraylist(){ + size = 0; + data =(ItemType[]) new Object[10]; + } + + public Arraylist(int intCapacity){ + size = 0; + data =(ItemType[]) new Object[intCapacity]; + } + + + /** + * 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) { + + // create an enhanced for loop to check each item in the list for the item being passed through + // to the contains method + for (ItemType E : data) { + if (E != null && E.equals(item)) + 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(); + } + public 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 for data to temp + for (int i = 0; i < size; i++) { + temp[i] = data[i]; + } + + //Step 3 - repoint/reference data to point to new array + data = temp; + + temp = null; + + + }// end of if need to re + } + + /** + * 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++; + + } + + + /** + * 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) { + + for (int i = 0; i < size; i++) { + if(data[i].equals(item)){ + for (int j = i; j < size -1; j++) { + data[j] = data[j +1]; + } + data[size-1] = null; + size--; + + } + + } + } + + /** + * Removes all items from this collection. + * The collection will be empty after this method returns. + */ + @Override + public void clear() { + + // lazy deletion - length will still be whatever it last was + 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; + +// this is the other way to write this method + for (ItemType itemToCheck : otherCollection) { + 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) { + for(ItemType itemToAdd : otherCollection){ + add(itemToAdd); + } + //throw new UnsupportedOperationException(" Not Implemented"); + } + + /** + * 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) { + for(ItemType itemToAdd : otherCollection){ + remove(itemToAdd); + } + } + + /** + * 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(); + } + 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(); + } + 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(); + 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){ + + //create a loop to iterate through the array + for (int i = 0; i < size; i++) { + // conditional looks to see if the parameter passed is in the array + if (i == index) { + // if the parameter exists in the array, we iterate through the list again, as many times as there are elements in our array + for (int j = i; j < size - 1; j++) { + // we replace the element occupying the index we passed as parameter with the next element in line, and we shift all elements toward the head of the array + data[j] = data[j + 1]; + } + // after all items have been shift, we decrement the size of the array list + data[size - 1] = null; + 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 result = 0; + + for (int i = 0; i < size; i++) { + if (item.equals(data[i])){ + result = i; + return result; + } + else{ + result = -1; + } + } + return result; + } + + /** + * 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) { + int lastIndex = -1; + for (int i = size-1; i >= 0 ; i--) { + if(data[i].equals(item)){ + lastIndex = i; + break; + + } + + } + return lastIndex; + } + + /** + * 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; + } + + private class OurCustomIterator implements Iterator { + + //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 SecondCustomerIterator implements ListIterator{ + // Fancier ITerator - lets us go forwards and backwards + + private int currentPosition; + + public SecondCustomerIterator(){ + 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 check curretnPosition with size + //hasPrevious check currentPosition against 0 + return currentPosition < 0; + } + + @Override + public ItemType previous() { + ItemType result = get(currentPosition); + currentPosition--; + return result; + } + + @Override + public int nextIndex() { + return 0; + } + + @Override + public int previousIndex() { + return 0; + } + + @Override + public void remove() { + + } + + @Override + public void set(ItemType itemType) { + + } + + @Override + public void add(ItemType itemType) { + + } + } + + // this is a method i had to create as i was not able to do a toString to check what was in my arrayListd + @Override + public String toString() { + if (size == 0) { + return "[]"; + } + StringBuilder sb = new StringBuilder(); + sb.append("["); + for (int i = 0; i < size; i++) { + sb.append(data[i]); + if (i < size - 1) { + sb.append(", "); + } + } + sb.append("]"); + return sb.toString(); + } +} diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java new file mode 100644 index 0000000..c71de85 --- /dev/null +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -0,0 +1,683 @@ +package edu.greenriver.sdev333; + +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; + Node previous; + } + + /** + * Constructor + */ + public SinglyLinkedList(){ + 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) { + //create node to hold our place + Node current = head; + //while our current place does not equal null,we will run + while(current != null){ + // we will check if the data field in our current place matches the + //parameter passed in, and if it does, we return true + if(current.data == item){ + return true; + } + //if current.data does not match our parameter , we set current to point to + // the next node in the list, until current is null, at which point + //we will know without a doubt if our parameter is in our list or not + current = current.next; + } + + 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 OurCustomerIterator(); + } + + + + public void addHead(ItemType item){ + if(head==null){ + head = new Node(); + head.data = item; + } + } + /** + * 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) { + + + //index at thee 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); + Node newNode = new Node(); + newNode.data = item; + newNode.next = null; + if(head == null){ + head = newNode; + }else { + Node current = head; + while (current.next != null) { + current = current.next; + } + //current is pointing to last node + 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 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) { + throw new UnsupportedOperationException(); + } + + /** + * 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 + // must cast the 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) { + throw new UnsupportedOperationException(); + } + + /** + * 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) { + throw new UnsupportedOperationException(); + } + + /** + * 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) { + checkIndex(index); + + 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); + Node current = head; + int counter = 0; + while(counter!= index){ + current = current.next; + counter++; + } + 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 (head == null) { + Node theNewOne = new Node(); + theNewOne.data = item; + head = theNewOne; + } else 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; + //stop one before the position i want to insert at + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + // current is point 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; + } + + //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) { + // since we return -1 if the item does not appear in our list, + // we can create a variable called last index to hold that value. + // we will update this variable as we traverse the linked list, and eventually + // it will hold the number of the last position our node.data=item will be in + int lastIndex = -1; + // we will create a variable to count how many positions we have traveled too, + // so this number will be updated every time we traverse through our list + // eventually we will assign last index to this number and our method will be complete + int index=0; + + // we create a new node to hold our position, and we assign this node the value of whatever our head node is + // then we go from there + Node current = head; + // this is the standard traversal loop we use to traverse through nodes in a linked list + // if current equals null, then there are no longer any more objects or nodes in our linked list, so the + //loop will stop + while (current != null){ + // this conditional if statement says if our current nodes data field is the same as the item we passed into the method + // then we will set the int variable lastIndex to the value of index + // at the start, last index is -1, so if current.data never equals item, we will exit this loop when current equals null + // and our method will return the default -1. Otherwise, if the item passed through our parameter does equal current.data + // we will update last index to the value of index. we always increment the value of index, and assign current to current.next, so + // our pointer is looking to the next node in our list. our loop will run until current equals null, so we will continue to check current.data against + //our item parameter, and if we get a hit, we will update lastIndex to our newly incremented index value everytime. + if(current.data.equals(item)){ + lastIndex = index; + } + index++; + current = current.next; + } + // we return last index every time, -1 for default but whatever the appropriate index is if we have the item in our list + return lastIndex; + } + + /** + * 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(); + } + + public String toString(){ + if( head == null){ + return "[]"; + } + Node current = head; + String result = "[" ; + while(current != null){ + result += current.data; + if(current.next != null){ + result+=","; + } + current = current.next; + } + return result + "]"; + } + + private class OurCustomerIterator implements Iterator{ + + //field + private Node currentPosition; + + + public OurCustomerIterator(){ + 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 Im on the last node: if (current.next == null) + // see if i made it past the last nde: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 Node previousPosition; + 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() { + if(!hasNext()){ + throw new NoSuchElementException(); + } + 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 previousPosition != null; + } + + /** + * 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() { + if(!hasPrevious()){ + throw new NoSuchElementException(); + } + currentPosition = previousPosition; + previousPosition = previousPosition.previous; + return currentPosition.data; + } + + /** + * 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 currentIndex; + } + + /** + * 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 currentIndex - 1; + } + + /** + * 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) { + + } + } + +} +