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/ImplementingLists.iml b/ImplementingLists.iml index c90834f..2125485 100644 --- a/ImplementingLists.iml +++ b/ImplementingLists.iml @@ -7,5 +7,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 3e59c38..ef4ce51 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,50 @@ +import edu.greenriver.sdev333.*; + +import java.util.Iterator; + public class Main { public static void main(String[] args) { System.out.println("Hello world!"); + + //test for ArrayList constructor + List friends = new ArrayList(); + + System.out.println("Test isEmpty result: " + friends.isEmpty()); + + List nemeses = new SinglyLinkedList<>(); + + nemeses.add("BR"); + nemeses.add("H2"); + 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"); + + //test for size and add(item) + System.out.println("size is now " + friends.size()); + + //test for add(index,item) + friends.add(2, "Wedensday"); + + //test for iterator implementation + for (String name: friends + ) { + System.out.println(name); + } + + //test for containsAll + System.out.println(friends.containsAll(nemeses)); + + + } } \ 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..da24675 --- /dev/null +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -0,0 +1,414 @@ +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 + //number of spots use in the data array different from length. (number of spots not null) + private int size; + + public ArrayList(){ + size = 0; + //casting array type to whatever item type is + 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; + } + // or return size == 0; + return false; + } + + /** + * Returns true if this collection contains the specified item. + * + * @param item items whose presence in this collection is to be tested + * @return true if this collection contains the specified item + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public boolean contains(ItemType item) { + 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(){ + //All of the above code works until I run out of room which is when size = length + if (size == data.length) { + //resize array (double it) + + //Step 1 - create larger array + ItemType[] temp = (ItemType[]) new Object[data.length * 2]; + + //Step 2 - Copy 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 + temp = null; + } + } + + /** + * Adds the specified item to the collection. + * + * @param item item to be added to the collection + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void add(ItemType item) { + checkSize(); + data[size] = item; + size ++; + //data[size++] = item; this works too + } + + /** + * 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); + + //item is in the list + if ( i != -1) { + //if found use the other remove method to remove + 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) { + for (ItemType name: otherCollection + ) { + if(!contains(name)){ + 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 name : otherCollection + ) { + checkSize(); + add(name); + } + + } + + /** + * 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 name : otherCollection + ) { + checkSize(); + remove(name); + } + } + + /** + * 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) { + //if item in other collection do nothing if item not in other collection remove + + } + + public void isValidIndex(int index){ + if (index >= size()) { + throw new IndexOutOfBoundsException("Index is beyond size"); + } + } + /** + * 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]; + } + + /** + * 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(); + for (int i = size + 1; i > index ; 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) { + 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 -1 item not 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 last 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 indexOf = -1; + for (int i = 0; i < size; i++){ + if (item.equals(data[i])){ + indexOf = i; + } + } + //if -1 item not in the array + return indexOf; + } + + /** + * 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(); + } + + private class OurCustomIterator implements Iterator{ + + //instance fields + private int currentPosition; + + public OurCustomIterator() { + currentPosition = 0; + } + + @Override + public boolean hasNext() { + return currentPosition < size(); + } + + @Override + public ItemType next() { + ItemType result = get(currentPosition); + currentPosition ++; + return result; + } + } + + private class secondCustomIterator implements ListIterator { + //fancier iterator - lets us go forward and backwards + private int currentPosition; + + public secondCustomIterator(){ + currentPosition = 0; + } + @Override + public boolean hasNext() { + return currentPosition < size(); + } + + @Override + public ItemType next() { + ItemType result = get(currentPosition); + currentPosition ++; + return result; + } + + @Override + public boolean hasPrevious() { + // current position greater than 0 + return currentPosition > 0; + } + + @Override + public ItemType previous() { + ItemType result = get(currentPosition); + currentPosition --; + return result; + } + + @Override + public int nextIndex() { + return currentPosition ++; + } + + @Override + public int previousIndex() { + return currentPosition --; + } + + @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/ArrayListTest.java b/src/edu/greenriver/sdev333/ArrayListTest.java new file mode 100644 index 0000000..3f2ecb6 --- /dev/null +++ b/src/edu/greenriver/sdev333/ArrayListTest.java @@ -0,0 +1,237 @@ +package edu.greenriver.sdev333; + + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ArrayListTest { + + @Test + void size() { + List friends = new ArrayList(); + assertEquals(0,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"); + assertEquals(11,friends.size()); + } + + @Test + void isEmpty() { + List friends = new ArrayList(); + assertEquals(true,friends.isEmpty()); + 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"); + assertEquals(false,friends.isEmpty()); + } + + @Test + void contains() { + List friends = new ArrayList(); + 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"); + assertEquals(true,friends.contains("Tyler")); + assertEquals(false,friends.contains("Thomas")); + } + + @Test + void iterator() { + //not sure what to use for a test here + } + + @Test + void add() { + List friends = new ArrayList(); + assertEquals(0, 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"); + assertEquals(11, friends.size()); + } + + @Test + void remove() { + List friends = new ArrayList(); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + friends.remove("Tina"); + assertEquals(2, friends.size()); + assertEquals("Jess", friends.get(0)); + assertEquals("Josh", friends.get(1)); + } + + @Test + void clear() { + List friends = new ArrayList(); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + assertEquals(3, friends.size()); + friends.clear(); + assertEquals(0, friends.size()); + } + + @Test + void containsAll() { + List nemeses = new ArrayList(); + nemeses.add("BR"); + nemeses.add("H2"); + List friends = new ArrayList(); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + assertEquals(false,friends.containsAll(nemeses)); + nemeses.clear(); + nemeses.add("Jess"); + assertEquals(true,friends.containsAll(nemeses)); + } + + @Test + void addAll() { + List nemeses = new ArrayList(); + nemeses.add("BR"); + nemeses.add("H2"); + List friends = new ArrayList(); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + assertEquals(3,friends.size()); + friends.addAll(nemeses); + assertEquals(5,friends.size()); + } + + @Test + void removeAll() { + List nemeses = new ArrayList(); + nemeses.add("BR"); + nemeses.add("H2"); + List friends = new ArrayList(); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + friends.add("BR"); + friends.add("H2"); + assertEquals(5,friends.size()); + friends.removeAll(nemeses); + assertEquals(3,friends.size()); + } + + @Test + void retainAll() { + } + + @Test + void get() { + List friends = new ArrayList(); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + assertEquals("Jess", friends.get(0)); + assertEquals("Tina", friends.get(1)); + assertEquals("Josh", friends.get(2)); + } + + @Test + void set() { + List friends = new ArrayList(); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + friends.set(1,"Thomas"); + assertEquals("Jess", friends.get(0)); + assertEquals("Thomas", friends.get(1)); + assertEquals("Josh", friends.get(2)); + + } + + @Test + void testAdd() { + List friends = new ArrayList(); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + friends.add(1,"Thomas"); + assertEquals("Jess", friends.get(0)); + assertEquals("Thomas", friends.get(1)); + assertEquals("Tina", friends.get(2)); + assertEquals("Josh", friends.get(3)); + friends.add(4,"Thomas"); + assertEquals("Thomas", friends.get(4)); + } + + @Test + void testRemove() { + List friends = new ArrayList(); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + friends.remove(1); + assertEquals("Jess", friends.get(0)); + assertEquals("Josh", friends.get(1)); + assertEquals(false, friends.contains("Tina")); + } + + @Test + void indexOf() { + List friends = new ArrayList(); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + assertEquals(1, friends.indexOf("Tina")); + assertEquals(-1, friends.indexOf("Thomas")); + } + + @Test + void lastIndexOf() { + List friends = new ArrayList(); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + assertEquals(4, friends.lastIndexOf("Tina")); + assertEquals(-1, friends.lastIndexOf("Thomas")); + } + + @Test + void listIterator() { + //not sure how to test this + } +} \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/DoublyLinkedList.java b/src/edu/greenriver/sdev333/DoublyLinkedList.java new file mode 100644 index 0000000..31c8a6e --- /dev/null +++ b/src/edu/greenriver/sdev333/DoublyLinkedList.java @@ -0,0 +1,251 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; +import java.util.ListIterator; + +public class DoublyLinkedList implements List { + //Fields + private DoublyLinkedList.Node head; + private int size; + + //helper/inner classes + private class Node { + ItemType data; + DoublyLinkedList.Node next; + } + + public DoublyLinkedList(){ + // an empty list has no nodes, which means it has no head + 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 head == null; + //or 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) { + return false; + } + + /** + * Returns an iterator over the elements in this collection. + * + * @return an Iterator over the elements in this collection + */ + @Override + public Iterator iterator() { + return null; + } + + /** + * Adds the specified item to the collection. + * + * @param item item to be added to the collection + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void add(ItemType item) { + + } + + /** + * Removes a single instance of the specified item from this collection, + * if it is present. + * + * @param item item to be removed from this collection, if present + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void remove(ItemType item) { + + } + + /** + * Removes all items from this collection. + * The collection will be empty after this method returns. + */ + @Override + public void clear() { + head = null; + size = 0; + } + + /** + * Returns true if this collection contains all the items + * in the specified other collection. + * + * @param otherCollection collection to be checked for containment in this collection + * @return true if this collection contains all the items + * in the specified other collection + */ + @Override + public boolean containsAll(Collection otherCollection) { + return false; + } + + /** + * Adds all the items in this specified other collection to this collection. + * + * @param otherCollection collection containing items to be added to this collection + */ + @Override + public void addAll(Collection otherCollection) { + + } + + /** + * Removes all of this collection's items that are also contained in the + * specified other collection. After this call returns, this collection will + * contain no elements in common with the specified other collection. + * + * @param otherCollection collection containing elements to be removed + * from this collection + */ + @Override + public void removeAll(Collection otherCollection) { + + } + + /** + * Retains only the items in this collection that are contained in the + * specified other collection. In other words, removes from this collection + * all of its items that are not contained in the specified other collection + * + * @param otherCollection collection containing elements to be retained in + * this collection + */ + @Override + public void retainAll(Collection otherCollection) { + + } + + /** + * Returns the item at the specified position in this list + * + * @param index index of the item to return + * @return the item at the specified position in this list + * @throws IndexOutOfBoundsException if this index is out of range + * (index < 0 || index >= size()) + */ + @Override + public ItemType get(int index) { + return null; + } + + /** + * Replaces the item at the specified position in this list + * with the specified item + * + * @param index index of the item to replace + * @param item item to be stored at the specified position + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void set(int index, ItemType item) { + + } + + /** + * Inserts the specified item at the specified position in this list. + * Shifts the item currently at that position (if any) and any subsequent + * items to the right. + * + * @param index index at which the specified item is to be inserted + * @param item item to be inserted + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void add(int index, ItemType item) { + + } + + /** + * Removes the element at the specified position in this list. + * Shifts any subsequent items to the left. + * + * @param index the index of the item to be removed + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void remove(int index) { + + } + + /** + * Returns the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item. + * + * @param item the item to search for + * @return the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item + * @throws NullPointerException if the specified item is null and this + * list does not permit null items + */ + @Override + public int indexOf(ItemType item) { + return 0; + } + + /** + * Returns the index of the last occurrence of the specified item + * in this list, or -1 if this list does not contain the item. + * + * @param item the item to search for + * @return the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item + * @throws NullPointerException if the specified item is null and this + * list does not permit null items + */ + @Override + public int lastIndexOf(ItemType item) { + return 0; + } + + /** + * Returns a list iterator over the elements in this list + * (in proper sequence). + * + * @return a list iterator over the elements in this list + * (in proper sequence) + */ + @Override + public ListIterator listIterator() { + return null; + } +} diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java new file mode 100644 index 0000000..b254959 --- /dev/null +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -0,0 +1,499 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; +import java.util.ListIterator; + +public class SinglyLinkedList implements List { + //Fields + private Node head; + private int size; + + //helper/inner classes + private class Node { + ItemType data; + Node next; + + public Node(ItemType data, Node next){ + this.data = data; + this.next = next; + } + } + + public SinglyLinkedList(){ + // an empty list has no nodes, which means it has no head + 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 head == null; + //or 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) { + Node currentNode = head; + while(currentNode.next != null){ + currentNode = currentNode.next; + if (currentNode.data.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(); + } + + /** + * 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) { + Node theNewOne = new Node(item, null); + if (head == null) { + head = theNewOne; + } else { + Node currentNode = head; + while(currentNode.next != null){ + currentNode = currentNode.next; + } + currentNode.next = theNewOne; + } + size ++; + } + + public boolean equalsAll(SinglyLinkedList otherCollection) { + if (otherCollection.equals(this)) { + return true; + } + + Node p1 = this.head; + Node p2 = otherCollection.head; + while (p1 != null && p2 != null) { + if (p1.data != p2.data) { + return false; + } + p1 = p1.next; + p2 = p2.next; + } + + return (p1 == null && p2 == null); + } + /** + * 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(); + } + + if(head.data == item) + { + head = head.next; + size --; + } else { + Node current = head; + Node previous; + while (current.next != null){ + previous = current; + current = current.next; + + if (current.data.equals(item)){ + previous.next = current.next; + size --; + } + } + + } + } + + /** + * Removes all items from this collection. + * The collection will be empty after this method returns. + */ + @Override + public void clear() { + head = null; + size = 0; + } + + /** + * Returns true if this collection contains all the items + * in the specified other collection. + * + * @param otherCollection collection to be checked for containment in this collection + * @return true if this collection contains all the items + * in the specified other collection + */ + @Override + public boolean containsAll(Collection otherCollection) { + return false; + } + + /** + * Adds all the items in this specified other collection to this collection. + * + * @param otherCollection collection containing items to be added to this collection + */ + @Override + public void addAll(Collection otherCollection) { + //Walk thourgh the other collection + //for each loop + //or use iterator + + //CAN DO LIKE THIS +// Iterator itr = (Iterator)otherCollection.iterator(); +// +// while(itr.hasNext()){ +// ItemType currentItem = itr.next(); +// add(0,currentItem); //add to front for better performance +// } + + //OR LIKE THIS + for (ItemType item : otherCollection + ) { + add(0,item); + } + } + + /** + * 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) { + Node currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + return currentNode.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) { + Node currentNode = head; + for (int i = 0; i < index; i++) { + currentNode = currentNode.next; + } + currentNode.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) { +// Check this +// if(index ==0){ +// //if someone wants to add at the beginning +// Node theNewOne = new Node(item, head); +// head = theNewOne; + if (index == 0){ + Node theNewOne = new Node(item, head); + head = theNewOne; + } +// } + + //take me to index + Node currentNode = head; + for (int i = 0; i < index - 1; i++) { + currentNode = currentNode.next; + } + + //create new node set next to current.next + Node newNode = new Node(item, currentNode.next); + //increment size + size++; + //update currentnode.next to new node + currentNode.next = newNode; + + } + + /** + * 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) { + + //take me to index + Node currentNode = head; + + //index = 0 (remove head) + if(index == 0){ + head = currentNode.next; + } + + //index less than 0 do nothing + else if (index < 0 || index >=size() ) + { + throw new IndexOutOfBoundsException(); + } + + // + for (int i = 0; i < index - 1; i++) { + currentNode = currentNode.next; + } + + currentNode.next = currentNode.next.next; + size--; + } + + /** + * Returns the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item. + * + * @param item the item to search for + * @return the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item + * @throws NullPointerException if the specified item is null and this + * list does not permit null items + */ + @Override + public int indexOf(ItemType item) { + int counter = 0; + Node current = head; + while (current != null) + { + if (current.data.equals(item)){ + return counter; + } + counter++; + current = current.next; + } + return -1; + } + + /** + * Returns the index of the last occurrence of the specified item + * in this list, or -1 if this list does not contain the item. + * + * @param item the item to search for + * @return the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item + * @throws NullPointerException if the specified item is null and this + * list does not permit null items + */ + @Override + public int lastIndexOf(ItemType item) { + int lastIndex = -1; + int counter = 0; + for (ItemType currentItem : this + ) { + if(currentItem.equals(item)){ + lastIndex = counter; + } + counter ++; + } + 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; + } + + public boolean isValidIndex(int index){ + if (index > 0 && index < size()){ + return true; + } else { + return false; + } + } + + private class OurCustomIterator implements Iterator{ + private Node currentPosition; + + public OurCustomIterator() { + + currentPosition = head; + } + /** + * Returns {@code true} if the iteration has more elements. + * (In other words, returns {@code true} if {@link #next} would + * return an element rather than throwing an exception.) + * + * @return {@code true} if the iteration has more elements + */ + @Override + public boolean hasNext() { + 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.data + currentPosition = currentPosition.next; //currentPosition.next; + return result; + } + } + +// private class secondCustomIterator implements ListIterator { +// //fancier iterator - lets us go forward and backwards +// private Node currentPosition; +// +// public secondCustomIterator(){ +// currentPosition = head; +// } +// @Override +// public boolean hasNext() { +// return currentPosition.next != null; +// } +// +// @Override +// public Node next() { +// return currentPosition.next; +// } +// +// @Override +// public boolean hasPrevious() { +// // current position greater than 0 +// return currentPosition != head; +// } +// +// @Override +// public ItemType previous() { +// //Not sure +// return o; +// } +// +// @Override +// public int nextIndex() { +// return indexOf(currentPosition.next.data); +// } +// +// @Override +// public int previousIndex() { +// return currentPosition --; +// } +// +// @Override +// public void remove() { +// +// } +// +// @Override +// public void set(ItemType itemType) { +// +// } +// +// @Override +// public void add(ItemType itemType) { +// +// } +// } +} diff --git a/src/edu/greenriver/sdev333/SinglyLinkedListTest.java b/src/edu/greenriver/sdev333/SinglyLinkedListTest.java new file mode 100644 index 0000000..39a49a4 --- /dev/null +++ b/src/edu/greenriver/sdev333/SinglyLinkedListTest.java @@ -0,0 +1,286 @@ +package edu.greenriver.sdev333; + + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class SinglyLinkedListTest { + + @Test + void size() { + List friends = new SinglyLinkedList(); + assertEquals(0,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"); + assertEquals(11,friends.size()); + } + + @Test + void isEmpty() { + List friends = new SinglyLinkedList(); + assertEquals(true,friends.isEmpty()); + 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"); + assertEquals(false,friends.isEmpty()); + } + + @Test + void contains() { + List friends = new SinglyLinkedList(); + 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"); + assertEquals(true,friends.contains("Tyler")); + assertEquals(false,friends.contains("Thomas")); + } + + @Test + void iterator() { + List friends = new SinglyLinkedList(); + 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"); + for (String friend : friends + ) { + System.out.println(friend); + } + } + + @Test + void add() { + List friends = new SinglyLinkedList(); + assertEquals(0, 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"); + assertEquals(11, friends.size()); + friends.add(5,"Sophia"); + assertEquals(12, friends.size()); + assertEquals("Sophia", friends.get(5)); + assertEquals("Tyler", friends.get(4)); + assertEquals("Usman", friends.get(6)); + friends.add(6,"Dorothy"); + friends.add(7,"Sophia"); + assertEquals("Sophia", friends.get(7)); + assertEquals("Dorothy", friends.get(6)); + friends.add(0,"Sophia"); + assertEquals("Sophia", friends.get(0)); + } + + @Test + void remove() { + List friends = new SinglyLinkedList(); + friends.add("Jess"); //change next //previous + friends.add("Tina"); //current + friends.add("Josh"); // to here //current.next + friends.remove(1); + System.out.println(friends.toString()); + assertEquals(2, friends.size()); + assertEquals("Jess", friends.get(0)); + assertEquals("Josh", friends.get(1)); + friends.remove(0); + assertEquals("Josh", friends.get(0)); + + + } + @Test + void equalsAll(){ + SinglyLinkedList friends = new SinglyLinkedList(); + friends.add("Jess"); //change next //previous + friends.add("Tina"); //current + friends.add("Josh"); // to here //current.next + SinglyLinkedList friends2 = new SinglyLinkedList(); + friends2.add("Jess"); //change next //previous + friends2.add("Tina"); //current + friends2.add("Josh"); // to here //current.next + assertEquals(true, friends.equalsAll(friends2)); + friends2.add("Thomas"); + assertEquals(false, friends.equalsAll(friends2)); + + + } + @Test + void clear() { + List friends = new SinglyLinkedList(); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + assertEquals(3, friends.size()); + friends.clear(); + assertEquals(0, friends.size()); + } + +// @Test +// void containsAll() { +// List nemeses = new SinglyLinkedList(); +// nemeses.add("BR"); +// nemeses.add("H2"); +// List friends = new SinglyLinkedList(); +// friends.add("Jess"); +// friends.add("Tina"); +// friends.add("Josh"); +// assertEquals(false,friends.containsAll(nemeses)); +// nemeses.clear(); +// nemeses.add("Jess"); +// assertEquals(true,friends.containsAll(nemeses)); +// } + + @Test + void addAll() { + List nemeses = new SinglyLinkedList(); + nemeses.add("BR"); + nemeses.add("H2"); + List friends = new SinglyLinkedList(); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + assertEquals(3,friends.size()); + friends.addAll(nemeses); + assertEquals(5,friends.size()); + } + +// @Test +// void removeAll() { +// List nemeses = new SinglyLinkedList(); +// nemeses.add("BR"); +// nemeses.add("H2"); +// List friends = new SinglyLinkedList(); +// friends.add("Jess"); +// friends.add("Tina"); +// friends.add("Josh"); +// friends.add("BR"); +// friends.add("H2"); +// assertEquals(5,friends.size()); +// friends.removeAll(nemeses); +// assertEquals(3,friends.size()); +// } + + @Test + void retainAll() { + } + + @Test + void get() { + List friends = new SinglyLinkedList(); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + assertEquals("Jess", friends.get(0)); + assertEquals("Tina", friends.get(1)); + assertEquals("Josh", friends.get(2)); + } + + @Test + void set() { + List friends = new SinglyLinkedList(); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + friends.set(1,"Thomas"); + assertEquals("Jess", friends.get(0)); + assertEquals("Thomas", friends.get(1)); + assertEquals("Josh", friends.get(2)); + + } + + @Test + void testAdd() { + List friends = new SinglyLinkedList(); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + friends.add(1,"Thomas"); + assertEquals("Jess", friends.get(0)); + assertEquals("Thomas", friends.get(1)); + assertEquals("Tina", friends.get(2)); + assertEquals("Josh", friends.get(3)); + } + + @Test + void testRemove() { + List friends = new SinglyLinkedList(); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + friends.remove(1); + assertEquals("Jess", friends.get(0)); + assertEquals("Josh", friends.get(1)); + assertEquals(false, friends.contains("Tina")); + friends.remove("Josh"); + assertEquals(false, friends.contains("Josh")); + } + + @Test + void indexOf() { + List friends = new SinglyLinkedList(); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + assertEquals(1, friends.indexOf("Tina")); + assertEquals(-1, friends.indexOf("Thomas")); + } + + @Test + void lastIndexOf() { + List friends = new SinglyLinkedList(); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + assertEquals(4, friends.lastIndexOf("Tina")); + assertEquals(-1, friends.lastIndexOf("Thomas")); + } + + @Test + void listIterator() { + //not sure how to test this (print out in for each loop) + } + + + +}