From 4cb33bc26a41a7405ec6aa9677b14be2ff658c62 Mon Sep 17 00:00:00 2001 From: Stewart Lovell Date: Wed, 11 Jan 2023 14:32:42 -0800 Subject: [PATCH 01/10] Added fields and an add method to ArrayList --- .idea/misc.xml | 2 +- src/Main.java | 19 ++ src/edu/greenriver/sdev333/ArrayList.java | 268 ++++++++++++++++++++++ 3 files changed, 288 insertions(+), 1 deletion(-) create mode 100644 src/edu/greenriver/sdev333/ArrayList.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 07115cd..7464918 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 3e59c38..0b13883 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,24 @@ +import edu.greenriver.sdev333.*; + public class Main { public static void main(String[] args) { System.out.println("Hello world!"); + + List friends = new ArrayList(); + System.out.println("Initial size is " + friends.size()); + + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + friends.add("Susan"); + friends.add("Tyler"); + friends.add("Usman"); + friends.add("Dee"); + friends.add("Rose"); + friends.add("Blanche"); + friends.add("Dorothy"); + friends.add("Sophia"); + System.out.println("size is now " + friends.size()); + } } \ 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..4765e4a --- /dev/null +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -0,0 +1,268 @@ +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 (size = indices that are NOT null, length is # of indices in the array) + 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() { + return false; + } + + /** + * Returns true if this collection contains the specified item. + * + * @param item items whose presence in this collection is to be tested + * @return true if this collection contains the specified item + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public boolean contains(ItemType item) { + return false; + } + + /** + * Returns an iterator over the elements in this collection. + * + * @return an Iterator over the elements in this collection + */ + @Override + public Iterator iterator() { + return null; + } + + /** + * Adds the specified item to the collection. + * + * @param item item to be added to the collection + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void add(ItemType item) { + + // check if the current array is out of room + if (size == data.length) { + // resize (double up the array size) + ItemType[] nextData = (ItemType[]) new Object[data.length * 2]; + // loop over the original array and copy its indices over to the new array + int i = 0; + for (ItemType it : data) { + // copy over each index to the new array + nextData[i] = data [i]; + // increment our counter to move to the next index + i++; + } + // set data equal to the new array (old array will be garbage collected) + data = nextData; + } + // array is not capped out, add the specified element to the next available index + data[size] = item; + // increment size + 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) { + + } + + /** + * Removes all items from this collection. + * The collection will be empty after this method returns. + */ + @Override + public void clear() { + + } + + /** + * Returns true if this collection contains all the items + * in the specified other collection. + * + * @param otherCollection collection to be checked for containment in this collection + * @return true if this collection contains all the items + * in the specified other collection + */ + @Override + public boolean containsAll(Collection otherCollection) { + return false; + } + + /** + * Adds all the items in this specified other collection to this collection. + * + * @param otherCollection collection containing items to be added to this collection + */ + @Override + public void addAll(Collection otherCollection) { + + } + + /** + * Removes all of this collection's items that are also contained in the + * specified other collection. After this call returns, this collection will + * contain no elements in common with the specified other collection. + * + * @param otherCollection collection containing elements to be removed + * from this collection + */ + @Override + public void removeAll(Collection otherCollection) { + + } + + /** + * Retains only the items in this collection that are contained in the + * specified other collection. In other words, removes from this collection + * all of its items that are not contained in the specified other collection + * + * @param otherCollection collection containing elements to be retained in + * this collection + */ + @Override + public void retainAll(Collection otherCollection) { + + } + + /** + * Returns the item at the specified position in this list + * + * @param index index of the item to return + * @return the item at the specified position in this list + * @throws IndexOutOfBoundsException if this index is out of range + * (index < 0 || index >= size()) + */ + @Override + public ItemType get(int index) { + return null; + } + + /** + * Replaces the item at the specified position in this list + * with the specified item + * + * @param index index of the item to replace + * @param item item to be stored at the specified position + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void set(int index, ItemType item) { + + } + + /** + * Inserts the specified item at the specified position in this list. + * Shifts the item currently at that position (if any) and any subsequent + * items to the right. + * + * @param index index at which the specified item is to be inserted + * @param item item to be inserted + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void add(int index, ItemType item) { + + } + + /** + * 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; + } +} From 8753f0d669430a105417351038b981e0f55159b5 Mon Sep 17 00:00:00 2001 From: Stewart Lovell Date: Wed, 18 Jan 2023 14:49:36 -0800 Subject: [PATCH 02/10] Implemented methods listed in level 1 and level 2 requirements --- src/Main.java | 19 ++- src/edu/greenriver/sdev333/ArrayList.java | 179 +++++++++++++++++++--- 2 files changed, 174 insertions(+), 24 deletions(-) diff --git a/src/Main.java b/src/Main.java index 0b13883..803d276 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,10 +1,11 @@ import edu.greenriver.sdev333.*; +import java.util.Iterator; public class Main { public static void main(String[] args) { System.out.println("Hello world!"); - List friends = new ArrayList(); + List friends = new ArrayList<>(); System.out.println("Initial size is " + friends.size()); friends.add("Jess"); @@ -18,7 +19,23 @@ public static void main(String[] args) { 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 iterator import + Iterator itr = friends.iterator(); + while(itr.hasNext()) { + String name = itr.next(); + System.out.println(name); + } + + for (String name : friends) { + 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 index 4765e4a..902470c 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -37,7 +37,7 @@ public int size() { */ @Override public boolean isEmpty() { - return false; + return size == 0; } /** @@ -50,7 +50,7 @@ public boolean isEmpty() { */ @Override public boolean contains(ItemType item) { - return false; + return indexOf(item) >= 0; } /** @@ -60,20 +60,10 @@ public boolean contains(ItemType item) { */ @Override public Iterator iterator() { - return null; + 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) { - - // check if the current array is out of room + private void checkSize() { if (size == data.length) { // resize (double up the array size) ItemType[] nextData = (ItemType[]) new Object[data.length * 2]; @@ -88,6 +78,19 @@ public void add(ItemType item) { // set data equal to the new array (old array will be garbage collected) data = nextData; } + } + + /** + * 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(); // array is not capped out, add the specified element to the next available index data[size] = item; // increment size @@ -104,7 +107,11 @@ public void add(ItemType item) { */ @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); + } } /** @@ -113,7 +120,8 @@ public void remove(ItemType item) { */ @Override public void clear() { - + // lazy deletion + size = 0; } /** @@ -126,7 +134,13 @@ public void clear() { */ @Override public boolean containsAll(Collection otherCollection) { - return false; + + for (ItemType it : otherCollection) { + if (!contains(it)) { + return false; + } + } + return true; } /** @@ -136,6 +150,7 @@ public boolean containsAll(Collection otherCollection) { */ @Override public void addAll(Collection otherCollection) { + throw new UnsupportedOperationException("Not implemented"); } @@ -175,7 +190,10 @@ public void retainAll(Collection otherCollection) { */ @Override public ItemType get(int index) { - return null; + if (index >= size) { + throw new IndexOutOfBoundsException("Index out of bounds"); + } + return data[index]; } /** @@ -191,7 +209,10 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { - + if (index >= size) { + throw new IndexOutOfBoundsException("Index out of bounds"); + } + data[index] = item; } /** @@ -208,7 +229,18 @@ public void set(int index, ItemType item) { */ @Override public void add(int index, ItemType item) { - + checkSize(); + if (item == null) { + throw new NullPointerException("Specified item is null"); + } + if (index >= size) { + throw new IndexOutOfBoundsException("Index out of bounds"); + } + for (int i = size; i > index; i--) { + data[i] = data[i-1]; + } + data[index] = item; + size++; } /** @@ -221,7 +253,13 @@ public void add(int index, ItemType item) { */ @Override public void remove(int index) { - + if (index >= size) { + throw new IndexOutOfBoundsException("Index out of bounds"); + } + for (int i = index; i < data.length; i++) { + data[i] = data[i+1]; + } + size--; } /** @@ -236,7 +274,13 @@ public void remove(int index) { */ @Override public int indexOf(ItemType item) { - return 0; + 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; } /** @@ -265,4 +309,93 @@ public int lastIndexOf(ItemType item) { public ListIterator listIterator() { return null; } -} + + private class OurCustomIterator implements Iterator { + // fields + private int currentPosition; + + public OurCustomIterator() { + currentPosition = 0; + } + + @Override + public boolean hasNext() { + // does not need to be size - 1 + 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 + + // fields + private int currentPosition; + + public SecondCustomIterator() { + currentPosition = 0; + } + + @Override + public boolean hasNext() { + return currentPosition < size(); + } + + @Override + public ItemType next() { + ItemType result = get(currentPosition); + currentPosition++; + return result; + } + + @Override + public boolean hasPrevious() { + return currentPosition > 0; + } + + @Override + public ItemType previous() { + ItemType result = get(currentPosition-1); + currentPosition--; + return result; + } + + @Override + public int nextIndex() { + if (currentPosition+1 < size()) { + throw new IndexOutOfBoundsException("Index out of bounds"); + } + return currentPosition+1; + } + + @Override + public int previousIndex() { + if (currentPosition-1 < 0) { + throw new IndexOutOfBoundsException("Index out of bounds"); + } + return currentPosition-1; + } + + @Override + public void remove() { + + } + + @Override + public void set(ItemType itemType) { + + } + + @Override + public void add(ItemType itemType) { + + } + } + +} // end of class ArrayList From 7296b9de45825e1e5dd0c7f293cb8bc000b1362c Mon Sep 17 00:00:00 2001 From: stewlove Date: Wed, 18 Jan 2023 18:53:08 -0800 Subject: [PATCH 03/10] implemented a few methods and wrote tests for most methods --- .idea/misc.xml | 2 +- src/Main.java | 68 +++++++++++++++++++---- src/edu/greenriver/sdev333/ArrayList.java | 50 ++++++++++++++--- 3 files changed, 101 insertions(+), 19 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 7464918..0806ea5 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 803d276..073074a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,14 +1,31 @@ import edu.greenriver.sdev333.*; + +import java.util.Arrays; import java.util.Iterator; public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + // constructor test List friends = new ArrayList<>(); + // size test System.out.println("Initial size is " + friends.size()); + System.out.println(); + // is empty test - should return true; + System.out.println(friends.isEmpty()); + System.out.println(); + // add test friends.add("Jess"); + // contains test - should return true + System.out.println(friends.contains("Jess")); + System.out.println(); + // indexOf test should return 0 + System.out.println(friends.indexOf("Jess")); + System.out.println(); + // is empty test after the array has an element - should return false + System.out.println(friends.isEmpty()); + System.out.println(); friends.add("Tina"); friends.add("Josh"); friends.add("Susan"); @@ -19,23 +36,52 @@ public static void main(String[] args) { friends.add("Blanche"); friends.add("Dorothy"); friends.add("Sophia"); + // alternate add method test (adding at specific index) friends.add(2, "Wednesday"); + // size changing after elements added - should return 12 System.out.println("size is now " + friends.size()); - - //for (int i = 0; i < friends.size(); i++) { - // System.out.println(friends.get(i)); - //} - - // above iterator import - Iterator itr = friends.iterator(); - while(itr.hasNext()) { - String name = itr.next(); + System.out.println(); + // iterator test - also shows Wednesday added at index 2 + for (String name : friends) { System.out.println(name); } - + System.out.println(); + // remove test + friends.remove(0); + // index 0 removed proof for (String name : friends) { System.out.println(name); } + System.out.println(); + // get test - should return Wednesday + System.out.println(friends.get(1)); + System.out.println(); + // set test - should change index 1 to "Stewart" + friends.set(1, "Stewart"); + System.out.println(friends.get(1)); + System.out.println(); + + // second constructor test (Creating ArrayList with a specified size) + List arrayTwo = new ArrayList<>(20); + arrayTwo.add("Michael"); + + // clear test on new array + System.out.println(arrayTwo.size()); + arrayTwo.clear(); + System.out.println(arrayTwo.size()); + System.out.println(); + + // lastIndex test - should return 2 + arrayTwo.add("Mark"); + arrayTwo.add("James"); + arrayTwo.add("Mark"); + System.out.println(arrayTwo.lastIndexOf("Mark")); + + // addAll test + List arrayThree = new ArrayList<>(); + arrayThree.add("John"); + arrayThree.add("Jacob"); + arrayTwo.addAll(arrayThree); } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index 902470c..7d6e17c 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -20,6 +20,11 @@ public ArrayList() { data = (ItemType[]) new Object[10]; } + public ArrayList(int arraySize) { + size = 0; + data = (ItemType[]) new Object[arraySize]; + } + /** * Returns the number of items in this collection. * @@ -150,8 +155,9 @@ public boolean containsAll(Collection otherCollection) { */ @Override public void addAll(Collection otherCollection) { - throw new UnsupportedOperationException("Not implemented"); - + for (ItemType it : otherCollection) { + add(it); + } } /** @@ -164,7 +170,11 @@ public void addAll(Collection otherCollection) { */ @Override public void removeAll(Collection otherCollection) { - + for (ItemType it : otherCollection) { + if (contains(it)) { + remove(it); + } + } } /** @@ -177,7 +187,17 @@ public void removeAll(Collection otherCollection) { */ @Override public void retainAll(Collection otherCollection) { - + for (ItemType it : data) { + int match = 0; + for (ItemType other : otherCollection) { + if (it.equals(other)) { + match++; + } + } + if (match < 1) { + remove(it); + } + } } /** @@ -256,7 +276,7 @@ public void remove(int index) { if (index >= size) { throw new IndexOutOfBoundsException("Index out of bounds"); } - for (int i = index; i < data.length; i++) { + for (int i = index; i < size - 1; i++) { data[i] = data[i+1]; } size--; @@ -288,14 +308,30 @@ public int indexOf(ItemType 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 + * @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) { - return 0; + int lastIndex = -1; + int occurrences = 0; + int matches = 0; + for (ItemType it : data) { + if (item.equals(it)) { + occurrences++; + } + } + while (matches != occurrences) { + for (int i = 0; i < size; i++) { + if (data[i].equals(item)) { + lastIndex = i; + matches++; + } + } + } + return lastIndex; } /** From 3f97a5d787fcd091e3da6bd351a151a40c8a061b Mon Sep 17 00:00:00 2001 From: stewlove Date: Wed, 18 Jan 2023 18:55:28 -0800 Subject: [PATCH 04/10] implemented a few methods and wrote tests for most methods --- src/Main.java | 1 + src/edu/greenriver/sdev333/ArrayList.java | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Main.java b/src/Main.java index 073074a..12643aa 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,3 +1,4 @@ + import edu.greenriver.sdev333.*; import java.util.Arrays; diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index 7d6e17c..b0778d0 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -342,6 +342,7 @@ public int lastIndexOf(ItemType item) { * (in proper sequence) */ @Override + // not complete public ListIterator listIterator() { return null; } From 9a2bc335b68952feab6912362579fc50a1bff312 Mon Sep 17 00:00:00 2001 From: stewlove Date: Wed, 18 Jan 2023 18:56:19 -0800 Subject: [PATCH 05/10] implemented a few methods and wrote tests for most methods --- src/Main.java | 1 - src/edu/greenriver/sdev333/ArrayList.java | 1 - 2 files changed, 2 deletions(-) diff --git a/src/Main.java b/src/Main.java index 12643aa..073074a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,4 +1,3 @@ - import edu.greenriver.sdev333.*; import java.util.Arrays; diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index b0778d0..7d6e17c 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -342,7 +342,6 @@ public int lastIndexOf(ItemType item) { * (in proper sequence) */ @Override - // not complete public ListIterator listIterator() { return null; } From a581648ed157dd498ba5fde041f3e8fb433b323a Mon Sep 17 00:00:00 2001 From: stewlove Date: Wed, 18 Jan 2023 18:59:09 -0800 Subject: [PATCH 06/10] implemented a few methods and wrote tests for most methods --- src/Main.java | 1 + src/edu/greenriver/sdev333/ArrayList.java | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 073074a..12643aa 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,3 +1,4 @@ + import edu.greenriver.sdev333.*; import java.util.Arrays; diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index 7d6e17c..9735808 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -345,7 +345,6 @@ public int lastIndexOf(ItemType item) { public ListIterator listIterator() { return null; } - private class OurCustomIterator implements Iterator { // fields private int currentPosition; From b5e4e6596bdbdba96844bbae56cb48fbea4fd70a Mon Sep 17 00:00:00 2001 From: Stewart Lovell Date: Mon, 13 Feb 2023 12:05:55 -0800 Subject: [PATCH 07/10] Full implementation of SinglyLinkedList class --- src/Main.java | 23 +- .../greenriver/sdev333/SinglyLinkedList.java | 493 ++++++++++++++++++ 2 files changed, 514 insertions(+), 2 deletions(-) create mode 100644 src/edu/greenriver/sdev333/SinglyLinkedList.java diff --git a/src/Main.java b/src/Main.java index 803d276..61739dc 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,10 +1,9 @@ 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 ArrayList<>(); System.out.println("Initial size is " + friends.size()); @@ -37,5 +36,25 @@ public static void main(String[] args) { System.out.println(name); } + System.out.println(); + + 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); + } + + System.out.println(); + System.out.println("END OF ARRAY LIST TESTS"); + System.out.println(); + + List linkedFriends = new SinglyLinkedList<>(); + System.out.println("Initial size is " + linkedFriends.size()); + } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java new file mode 100644 index 0000000..3e97031 --- /dev/null +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -0,0 +1,493 @@ +package edu.greenriver.sdev333; + +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; + } + + 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) { + if (item == null) { + throw new NullPointerException("Item is null"); + } + Node n = head; + while(n.next != null) { + if (n.data == item) { + return true; + } + n = n.next; + } + return false; + } + + private class OurEnhancedIterator implements ListIterator { + + private Node currentPosition; + private int currentIndex; + + public OurEnhancedIterator() { + currentPosition = head; + } + + @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) { + + } + } + + private class OurCustomIterator implements Iterator { + // fields + private Node currentPosition; + + public OurCustomIterator() { + currentPosition = head; + } + + @Override + public boolean hasNext() { + return currentPosition.next != null; + } + + @Override + public ItemType next() { + ItemType result = currentPosition.data; + currentPosition = currentPosition.next; + return result; + } + } + + /** + * 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("Item is null"); + } + + Node newNode = new Node(); + newNode.data = item; + newNode.next = null; + + if (head == null) { + head = newNode; + } else { + Node node = head; + while (node.next != null) { + node = node.next; + } + node.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) { + // check if the item is null + if (item == null) { + throw new NullPointerException("Specified item is null"); + } + // if the head is holding the item + if (head.data.equals(item)) { + // set the head equal to head.next (effectively deleting the node) + head = head.next; + // decrement size + size--; + } else { + // create a new node to hold the spot of our current node + Node current = head; + // create another node that will keep track of the previous node + Node previous; + // head is not holding the item, search the LinkedList for the item + while (current.next != null) { + // set previous equal to current + previous = current; + // set current equal to the next node + current = current.next; + // check if current's data is equal to item + if (current.data.equals(item)) { + // it does, delete the node + previous.next = current.next; + // decrement size + 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) { + // loop for every item in otherCollection + for (ItemType it : otherCollection) { + // check if the current item is contained in the LinkedList + if (!contains(it)) { + // if it isn't, return false + return false; + } + } + // all items were found in the LinkedList, return true + 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) { + // loop for each item in otherCollection + for (ItemType it : otherCollection) { + // add the current item to the LinkedList + add(0, 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) { + // loop for every item in otherCollection + for (ItemType it : otherCollection) { + // remove the specified item + remove(it); + } + } + + /** + * 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) { + // create a new LinkedList to hold all retained items + SinglyLinkedList retained = new SinglyLinkedList<>(); + // get each item in the LinkedList + for (int i=0; i= size()) + */ + @Override + public ItemType get(int index) { + // check if the index is out bounds + checkIndex(index); + // keep track of the current node + Node current = head; + // create a counter (we don't have an index system) + int counter = 0; + // loop over the LinkedList while count != index + while (counter != index) { + // set current equal to the next node + current = current.next; + // increment the counter + counter++; + } + // return the current node's data + 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) { + // check if the index is out of bounds + checkIndex(index); + // keep track of the current node + Node current = head; + // create a counter + int counter = 0; + // loop over the LinkedList while count != index + while (counter != index) { + current = current.next; + counter++; + } + // set the current node's data equal to the specified item + 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) { + // check if the item is null + if (item == null) { + throw new NullPointerException("Item is null"); + } + checkIndex(index); + + if (index == 0) { + Node newNode = new Node(); + newNode.data = item; + newNode.next = head; + head = newNode; + } else { + Node current = head; + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + Node newNode = new Node(); + newNode.data = item; + newNode.next = current.next; + current.next = newNode; + } + + size++; + } + + private void checkIndex(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index out of bounds"); + } + } + + /** + * 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 listIterator() { + return new OurEnhancedIterator(); + } +} From 1bb2abe0cdbfb874b7deb58aaf73f83872e3207e Mon Sep 17 00:00:00 2001 From: Stewart Lovell Date: Mon, 13 Feb 2023 12:09:34 -0800 Subject: [PATCH 08/10] Full implementation of SinglyLinkedList class --- src/Main.java | 7 ------- src/edu/greenriver/sdev333/.idea/.gitignore | 8 ++++++++ src/edu/greenriver/sdev333/.idea/misc.xml | 6 ++++++ src/edu/greenriver/sdev333/.idea/modules.xml | 8 ++++++++ src/edu/greenriver/sdev333/.idea/vcs.xml | 6 ++++++ src/edu/greenriver/sdev333/sdev333.iml | 11 +++++++++++ 6 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 src/edu/greenriver/sdev333/.idea/.gitignore create mode 100644 src/edu/greenriver/sdev333/.idea/misc.xml create mode 100644 src/edu/greenriver/sdev333/.idea/modules.xml create mode 100644 src/edu/greenriver/sdev333/.idea/vcs.xml create mode 100644 src/edu/greenriver/sdev333/sdev333.iml diff --git a/src/Main.java b/src/Main.java index 61739dc..8fa9e26 100644 --- a/src/Main.java +++ b/src/Main.java @@ -49,12 +49,5 @@ public static void main(String[] args) { System.out.println(name); } - System.out.println(); - System.out.println("END OF ARRAY LIST TESTS"); - System.out.println(); - - List linkedFriends = new SinglyLinkedList<>(); - System.out.println("Initial size is " + linkedFriends.size()); - } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/.idea/.gitignore b/src/edu/greenriver/sdev333/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/src/edu/greenriver/sdev333/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/src/edu/greenriver/sdev333/.idea/misc.xml b/src/edu/greenriver/sdev333/.idea/misc.xml new file mode 100644 index 0000000..639900d --- /dev/null +++ b/src/edu/greenriver/sdev333/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/.idea/modules.xml b/src/edu/greenriver/sdev333/.idea/modules.xml new file mode 100644 index 0000000..14c94f3 --- /dev/null +++ b/src/edu/greenriver/sdev333/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/.idea/vcs.xml b/src/edu/greenriver/sdev333/.idea/vcs.xml new file mode 100644 index 0000000..4fce1d8 --- /dev/null +++ b/src/edu/greenriver/sdev333/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/sdev333.iml b/src/edu/greenriver/sdev333/sdev333.iml new file mode 100644 index 0000000..a32d0e6 --- /dev/null +++ b/src/edu/greenriver/sdev333/sdev333.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file From 70df85c34ba615034b6e133a7de5b7d59955f106 Mon Sep 17 00:00:00 2001 From: Stewart Lovell Date: Mon, 13 Feb 2023 12:12:48 -0800 Subject: [PATCH 09/10] Full implementation of SinglyLinkedList class --- src/Main.java | 1 + src/edu/greenriver/sdev333/SinglyLinkedList.java | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Main.java b/src/Main.java index 8fa9e26..8bb0d7f 100644 --- a/src/Main.java +++ b/src/Main.java @@ -48,6 +48,7 @@ public static void main(String[] args) { String name = fancyItr.previous(); System.out.println(name); } + // } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index 3e97031..fe7960f 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -452,6 +452,7 @@ public void remove(int index) { */ @Override public int indexOf(ItemType item) { + // int counter = 0; Node current = head; while (current != null) { From 78ba10783374eb851701254fe2a3c9455184b87a Mon Sep 17 00:00:00 2001 From: Stewart Lovell Date: Mon, 13 Feb 2023 12:45:34 -0800 Subject: [PATCH 10/10] Testing for SinglyLinkedList --- src/Main.java | 147 +++++++++++++++--- .../greenriver/sdev333/SinglyLinkedList.java | 1 - 2 files changed, 129 insertions(+), 19 deletions(-) diff --git a/src/Main.java b/src/Main.java index 8bb0d7f..366fa48 100644 --- a/src/Main.java +++ b/src/Main.java @@ -4,10 +4,25 @@ public class Main { public static void main(String[] args) { + // constructor test List friends = new ArrayList<>(); + // size test System.out.println("Initial size is " + friends.size()); - + System.out.println(); + // is empty test - should return true; + System.out.println(friends.isEmpty()); + System.out.println(); + // add test friends.add("Jess"); + // contains test - should return true + System.out.println(friends.contains("Jess")); + System.out.println(); + // indexOf test should return 0 + System.out.println(friends.indexOf("Jess")); + System.out.println(); + // is empty test after the array has an element - should return false + System.out.println(friends.isEmpty()); + System.out.println(); friends.add("Tina"); friends.add("Josh"); friends.add("Susan"); @@ -18,37 +33,133 @@ public static void main(String[] args) { friends.add("Blanche"); friends.add("Dorothy"); friends.add("Sophia"); + // alternate add method test (adding at specific index) friends.add(2, "Wednesday"); + // size changing after elements added - should return 12 System.out.println("size is now " + friends.size()); - - //for (int i = 0; i < friends.size(); i++) { - // System.out.println(friends.get(i)); - //} - - // above iterator import - Iterator itr = friends.iterator(); - while(itr.hasNext()) { - String name = itr.next(); + System.out.println(); + // iterator test - also shows Wednesday added at index 2 + for (String name : friends) { System.out.println(name); } - + System.out.println(); + // remove test + friends.remove(0); + // index 0 removed proof for (String name : friends) { System.out.println(name); } + System.out.println(); + // get test - should return Wednesday + System.out.println(friends.get(1)); + System.out.println(); + // set test - should change index 1 to "Stewart" + friends.set(1, "Stewart"); + System.out.println(friends.get(1)); + System.out.println(); + // second constructor test (Creating ArrayList with a specified size) + List arrayTwo = new ArrayList<>(20); + arrayTwo.add("Michael"); + // clear test on new array + System.out.println(arrayTwo.size()); + arrayTwo.clear(); + System.out.println(arrayTwo.size()); + System.out.println(); + // lastIndex test - should return 2 + arrayTwo.add("Mark"); + arrayTwo.add("James"); + arrayTwo.add("Mark"); + System.out.println(arrayTwo.lastIndexOf("Mark")); + // addAll test + List arrayThree = new ArrayList<>(); + arrayThree.add("John"); + arrayThree.add("Jacob"); + arrayTwo.addAll(arrayThree); + + + System.out.println(); + System.out.println("END OF ARRAYLIST TESTS"); + System.out.println(); + - ListIterator fancyItr = friends.listIterator(); - while (fancyItr.hasNext()) { - String name = fancyItr.next(); + + + List linkedFriends = new SinglyLinkedList<>(); + // size test + System.out.println("Initial size is " + linkedFriends.size()); + System.out.println(); + // is empty test - should return true; + System.out.println(linkedFriends.isEmpty()); + System.out.println(); + // add test + linkedFriends.add("Jess"); + // contains test - should return true + System.out.println(linkedFriends.contains("Jess")); + System.out.println(); + // indexOf test should return 0 + System.out.println(linkedFriends.indexOf("Jess")); + System.out.println(); + // is empty test after the array has an element - should return false + System.out.println(linkedFriends.isEmpty()); + System.out.println(); + linkedFriends.add("Tina"); + linkedFriends.add("Josh"); + linkedFriends.add("Susan"); + linkedFriends.add("Tyler"); + linkedFriends.add("Usman"); + linkedFriends.add("Dee"); + linkedFriends.add("Rose"); + linkedFriends.add("Blanche"); + linkedFriends.add("Dorothy"); + linkedFriends.add("Sophia"); + // alternate add method test (adding at specific index) + linkedFriends.add(2, "Wednesday"); + // size changing after elements added - should return 12 + System.out.println("size is now " + linkedFriends.size()); + System.out.println(); + // iterator test - also shows Wednesday added at index 2 + for (String name : linkedFriends) { System.out.println(name); } System.out.println(); - while (fancyItr.hasPrevious()) { - String name = fancyItr.previous(); + // remove test + linkedFriends.remove(0); + // index 0 removed proof + for (String name : linkedFriends) { + System.out.println(name); + } + System.out.println(); + // get test - should return Wednesday + System.out.println(linkedFriends.get(1)); + System.out.println(); + // set test - should change index 1 to "Stewart" + linkedFriends.set(1, "Stewart"); + System.out.println(linkedFriends.get(1)); + System.out.println(); + //containsAll test + List duplicate = linkedFriends; + System.out.println("containsAll: " + linkedFriends.containsAll(duplicate)); + // retainAll test, will show that "Stewart" was not retained + duplicate.remove("Stewart"); + linkedFriends.retainAll(duplicate); + System.out.println(); + for (String name : linkedFriends) { + System.out.println(name); + } + //addAll test, there should now be a duplicate of each name + linkedFriends.addAll(duplicate); + System.out.println(); + for (String name : linkedFriends) { + System.out.println(name); + } + System.out.println("If nothing is printed below, removeAll worked"); + //removeAll test + linkedFriends.removeAll(duplicate); + System.out.println(); + for (String name : linkedFriends) { System.out.println(name); } - // - } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index fe7960f..3e97031 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -452,7 +452,6 @@ public void remove(int index) { */ @Override public int indexOf(ItemType item) { - // int counter = 0; Node current = head; while (current != null) {