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..ad5ee5a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,62 @@ +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!"); + + List friends = new RecursiveLinkedList(); + + 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)); + //} + + // above: import java.util.Iterator; + /* + Iterator itr = friends.iterator(); + while (itr.hasNext()) { + String name = itr.next(); + System.out.println(name); + } + + + for (String name : friends) { + System.out.println(name); + } + + ListIterator fancyItr = friends.listIterator(); + while (fancyItr.hasNext()) { + String name = fancyItr.next(); + System.out.println(name); + } + System.out.println(); + while (fancyItr.hasPrevious()) { + String name = fancyItr.previous(); + System.out.println(name); + } + + */ + + + + } } \ 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..483ec57 --- /dev/null +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -0,0 +1,417 @@ +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 # 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]; + } + + + /** + * 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(); + } + + 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 < size; i++) { + temp[i] = data[i]; + } + + // Step 3 - repoint/refererence data to point to new array + data = temp; + + // Optional: + temp = null; + } // end of if (need to resize) + } + + /** + * 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 + + /** + * 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) { + int i = indexOf(item); + if (i != -1) { + // if it's found, use the other remove method to do the work + 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; + + + //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) { + throw new UnsupportedOperationException("Not gonna do it!"); + } + + /** + * 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; + } + + /** + * 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) { + // 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) { + 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; + } + + + private class OurCustomIterator implements Iterator { + 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; + } + } + + + 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 false; + } + + @Override + public ItemType next() { + return null; + } + + @Override + public boolean hasPrevious() { + // hasNext checked currentPosition with size + // hasPrevious check currentPosition against 0 + return false; + } + + @Override + public ItemType previous() { + return null; + } + + @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) { + + } + } + +} // end of class ArrayList diff --git a/src/edu/greenriver/sdev333/RecursiveLinkedList.java b/src/edu/greenriver/sdev333/RecursiveLinkedList.java new file mode 100644 index 0000000..a02a516 --- /dev/null +++ b/src/edu/greenriver/sdev333/RecursiveLinkedList.java @@ -0,0 +1,160 @@ +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; + } + + @Override + public int size() { + return helpSize(head); + } + + // private helper method that does the actual recursive work + private int helpSize(Node theHead){ + if (theHead == null) { + // base case (the bottom / the end) + return 0; + } + else { + // VV "the rest" + return helpSize(theHead.next) + 1; + } + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public boolean contains(ItemType item) { + return false; + } + + @Override + public Iterator iterator() { + return null; + } + + @Override + public void add(ItemType item) { + // adds an item to back + // loopy way: + // - created a current variable, set it to be same value as head + // - wrote a loop while (current.next != null) { current = current.next; } + // - once we are at the last node, create new node and attach it + head = helpAdd(head, item); + } + + private Node helpAdd(Node theHead, ItemType item) { + if (theHead == null) { + // hit bottom (at the end) + 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; + } + } + + @Override + public void remove(ItemType item) { + + } + + @Override + public void clear() { + + } + + @Override + public boolean containsAll(Collection otherCollection) { + return false; + } + + @Override + public void addAll(Collection otherCollection) { + + } + + @Override + public void removeAll(Collection otherCollection) { + + } + + @Override + public void retainAll(Collection otherCollection) { + + } + + @Override + public ItemType get(int index) { + return null; + } + + @Override + public void set(int index, ItemType item) { + + } + + @Override + public void add(int index, ItemType item) { + // add(0, item) + if (index == 0) { + // add to the front - list may or may not be empty + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = head; + head = theNewOne; + } + else { + // anywhere else other than 0 (front) + 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; + } + } + + @Override + public void remove(int index) { + + } + + @Override + public int indexOf(ItemType item) { + return 0; + } + + @Override + public int lastIndexOf(ItemType item) { + return 0; + } + + @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..4ae2b20 --- /dev/null +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -0,0 +1,487 @@ +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 + public void add(ItemType item) { + if (item == null) { + throw new NullPointerException(); + } + // 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); + } + + /** + * 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(); + add(currentItem); + } + + /* + // alternative implementation using a for-each loop + for (ItemType currentItem : otherCollection) { + 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; + + /*for (int i = 0; i < index; i++) { + current = current.next; + }*/ + } + + /** + * 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) { + 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 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) { + 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; + } + + @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() { + return false; + } + + @Override + public ItemType previous() { + return null; + } + + @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) { + + } + } + +}