From 7afa2fc711e37e61b0fa1975a7dc52d262b30d84 Mon Sep 17 00:00:00 2001 From: Kevin Stone Date: Wed, 11 Jan 2023 14:27:44 -0800 Subject: [PATCH 01/10] Created private fields, constructor and add method. Test code in Main. --- src/Main.java | 23 ++ src/edu/greenriver/sdev333/ArrayList.java | 271 ++++++++++++++++++++++ 2 files changed, 294 insertions(+) create mode 100644 src/edu/greenriver/sdev333/ArrayList.java diff --git a/src/Main.java b/src/Main.java index 3e59c38..4ea80f6 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,28 @@ +import edu.greenriver.sdev333.ArrayList; +import edu.greenriver.sdev333.List; + 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("a"); + friends.add("a"); + friends.add("a"); + friends.add("a"); + friends.add("a"); + friends.add("a"); + friends.add("a"); + friends.add("a"); + + 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..ad95596 --- /dev/null +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -0,0 +1,271 @@ +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 number of spots that are not NULL + // 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() { + 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) { + + // HOWEVER: when size becomes length - we run out of room + if (size == data.length){ + // create new array, double the size, copy elements over + + // create larger array twice the size as the current + ItemType[] temp = (ItemType[]) new Object[size * 2]; + + // copy items from data array to temp array + for (int i = 0; i < size; i++) { + temp[i] = data[i]; + } + + // repoint/reference data to point to temp array + data = temp; + + // optional + temp = null; + } + + data[size] = item; + size++; + } + + /** + * Removes a single instance of the specified item from this collection, + * if it is present. + * + * @param item item to be removed from this collection, if present + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void remove(ItemType item) { + + } + + /** + * 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 07c3fb8ae78a7b0cee53ec2df148e8e707ab7724 Mon Sep 17 00:00:00 2001 From: Kevin Stone Date: Wed, 11 Jan 2023 14:32:56 -0800 Subject: [PATCH 02/10] Created private fields, constructor and add method. Test code in Main. --- .idea/vcs.xml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .idea/vcs.xml 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 From 46ef6a5e1f1d0a4c4b2b4766390437d3392708e6 Mon Sep 17 00:00:00 2001 From: Kevin Stone Date: Wed, 11 Jan 2023 14:50:02 -0800 Subject: [PATCH 03/10] Created a few more methods ( get, set, clear ). --- src/edu/greenriver/sdev333/ArrayList.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index ad95596..6bd502b 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -38,7 +38,7 @@ public int size() { */ @Override public boolean isEmpty() { - return false; + return size == 0; } /** @@ -116,7 +116,8 @@ public void remove(ItemType item) { */ @Override public void clear() { - + // lazy deletion = + size = 0; } /** @@ -178,7 +179,11 @@ public void retainAll(Collection otherCollection) { */ @Override public ItemType get(int index) { - return null; + if(index >= size){ + throw new IndexOutOfBoundsException("index is beyond size"); + } + return data[index]; + } /** @@ -194,7 +199,10 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { - + if(index >= size){ + throw new IndexOutOfBoundsException("index is beyond size"); + } + data[index] = item; } /** @@ -211,7 +219,7 @@ public void set(int index, ItemType item) { */ @Override public void add(int index, ItemType item) { - + // focus on trying this one } /** From 4feaba483143d5e2f38ad56d43921cf8a31897e9 Mon Sep 17 00:00:00 2001 From: Kevin Stone Date: Wed, 18 Jan 2023 14:47:03 -0800 Subject: [PATCH 04/10] Implemented a few more methods as a class. --- src/Main.java | 48 ++-- src/edu/greenriver/sdev333/ArrayList.java | 267 +++++++++++++++++++--- 2 files changed, 268 insertions(+), 47 deletions(-) diff --git a/src/Main.java b/src/Main.java index 4ea80f6..a62370c 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,28 +1,42 @@ import edu.greenriver.sdev333.ArrayList; import edu.greenriver.sdev333.List; +import java.util.Iterator; + 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("a"); - friends.add("a"); - friends.add("a"); - friends.add("a"); - friends.add("a"); - friends.add("a"); - friends.add("a"); - friends.add("a"); + //System.out.println("Initial size is " + friends.size()); + friends.add("1"); + friends.add("2"); + friends.add("3"); + friends.add("4"); + friends.add("5"); + friends.add("6"); + friends.add("7"); + friends.add("8"); + friends.add("9"); + friends.add("10"); + friends.add("11"); - System.out.println("Size is now " + friends.size()); + System.out.println("Size before add: " + friends.size()); + friends.add(10, "test"); + System.out.println("Size AFTER add: " + friends.size()); + for (int i = 0; i < friends.size(); i++) { + System.out.println(friends.get(i)); + } + // import java.util.Iterator; + // iterator test code +// 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 6bd502b..b0fc22f 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -51,6 +51,10 @@ public boolean isEmpty() { */ @Override public boolean contains(ItemType item) { + int i = indexOf(item); + if ( i != -1 ) { + return true; + } return false; } @@ -61,7 +65,8 @@ public boolean contains(ItemType item) { */ @Override public Iterator iterator() { - return null; + + return new OurCustomIterator(); } /** @@ -75,23 +80,7 @@ public Iterator iterator() { public void add(ItemType item) { // HOWEVER: when size becomes length - we run out of room - if (size == data.length){ - // create new array, double the size, copy elements over - - // create larger array twice the size as the current - ItemType[] temp = (ItemType[]) new Object[size * 2]; - - // copy items from data array to temp array - for (int i = 0; i < size; i++) { - temp[i] = data[i]; - } - - // repoint/reference data to point to temp array - data = temp; - - // optional - temp = null; - } + checkSize(); data[size] = item; size++; @@ -107,7 +96,10 @@ public void add(ItemType item) { */ @Override public void remove(ItemType item) { - + int i = indexOf(item); + if( i != -1 ){ + remove(i); + } } /** @@ -130,7 +122,16 @@ public void clear() { */ @Override public boolean containsAll(Collection otherCollection) { - return false; + + Iterator itr = (Iterator) otherCollection.iterator(); + while (itr.hasNext()){ + ItemType itemToCheck = itr.next(); + if (!contains(itemToCheck)){ + return false; + } + } + + return true; } /** @@ -220,19 +221,55 @@ public void set(int index, ItemType item) { @Override public void add(int index, ItemType item) { // focus on trying this one + + checkSize(); + + for (int i = size; i >= index + 1; i--) { + data[i] = data[i - 1]; + } + data[index] = item; + size++; + + +// if ( index < 0 || index >= size() ){ +// throw new IndexOutOfBoundsException("Please enter a valid number"); +// } +// if ( item == null ){ +// throw new NullPointerException("Item cannot be null"); +// } +// +// // create larger array with one more index +// +// // copy each element stopping before the indicated index +// ItemType[] temp = (ItemType[]) new Object[size + 1]; +// for (int i = 0; i <= size; i++) { +// temp[i] = data[i]; +// +// if ( i == index ){ +// // if i equals the indicated index, assign item to index +// temp[index] = item; +// } +// // if i is greater than the index, assign temp's current index value +// // to data's previous index's value +// else if ( i > index ){ +// temp[i] = data[i - 1]; +// } +// } +// +// // increase size and repoint/reference data to point to temp array address +// size++; +// data = temp; + } - /** - * 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--; } /** @@ -247,7 +284,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; } /** @@ -276,4 +319,168 @@ public int lastIndexOf(ItemType item) { public ListIterator listIterator() { return null; } + + private void checkSize(){ + // HOWEVER: when size becomes length - we run out of room + if (size == data.length){ + // create new array, double the size, copy elements over + + // create larger array twice the size as the current + ItemType[] temp = (ItemType[]) new Object[size * 2]; + + // copy items from data array to temp array + for (int i = 0; i < size; i++) { + temp[i] = data[i]; + } + + // repoint/reference data to point to temp array + data = temp; + + // optional + temp = null; + } + } + + private class OurCustomIterator implements Iterator { + + //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; + } + } + + + // listIterator to work on + 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 currentPosition < size(); + } + + + @Override + public ItemType next() { + ItemType result = get(currentPosition); + currentPosition++; + return result; } + + + @Override + public boolean hasPrevious() { + // hasNext checked currentPosition with size + // hasPrevious check currentPosition against 0 + return currentPosition > 0; + } + + @Override + public ItemType previous() { + return null; + } + + @Override + public int nextIndex() { + return 0; + } + + + @Override + public int previousIndex() { + return 0; + } + + /** + * Removes from the list the last element that was returned by {@link + * #next} or {@link #previous} (optional operation). This call can + * only be made once per call to {@code next} or {@code previous}. + * It can be made only if {@link #add} has not been + * called after the last call to {@code next} or {@code previous}. + * + * @throws UnsupportedOperationException if the {@code remove} + * operation is not supported by this list iterator + * @throws IllegalStateException if neither {@code next} nor + * {@code previous} have been called, or {@code remove} or + * {@code add} have been called after the last call to + * {@code next} or {@code previous} + */ + @Override + public void remove() { + + } + + /** + * Replaces the last element returned by {@link #next} or + * {@link #previous} with the specified element (optional operation). + * This call can be made only if neither {@link #remove} nor {@link + * #add} have been called after the last call to {@code next} or + * {@code previous}. + * + * @param itemType the element with which to replace the last element returned by + * {@code next} or {@code previous} + * @throws UnsupportedOperationException if the {@code set} operation + * is not supported by this list iterator + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this list + * @throws IllegalArgumentException if some aspect of the specified + * element prevents it from being added to this list + * @throws IllegalStateException if neither {@code next} nor + * {@code previous} have been called, or {@code remove} or + * {@code add} have been called after the last call to + * {@code next} or {@code previous} + */ + @Override + public void set(ItemType itemType) { + + } + + /** + * Inserts the specified element into the list (optional operation). + * The element is inserted immediately before the element that + * would be returned by {@link #next}, if any, and after the element + * that would be returned by {@link #previous}, if any. (If the + * list contains no elements, the new element becomes the sole element + * on the list.) The new element is inserted before the implicit + * cursor: a subsequent call to {@code next} would be unaffected, and a + * subsequent call to {@code previous} would return the new element. + * (This call increases by one the value that would be returned by a + * call to {@code nextIndex} or {@code previousIndex}.) + * + * @param itemType the element to insert + * @throws UnsupportedOperationException if the {@code add} method is + * not supported by this list iterator + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this list + * @throws IllegalArgumentException if some aspect of this element + * prevents it from being added to this list + */ + @Override + public void add(ItemType itemType) { + + } + } + } From 96fe2363eab1010e8ffe2ffa8f2d2f100b836d43 Mon Sep 17 00:00:00 2001 From: Kevin Stone Date: Wed, 18 Jan 2023 17:46:43 -0800 Subject: [PATCH 05/10] Added more documentation and test code. --- src/Main.java | 62 ++++++++++++++++++----- src/edu/greenriver/sdev333/ArrayList.java | 39 +++++++++++++- 2 files changed, 85 insertions(+), 16 deletions(-) diff --git a/src/Main.java b/src/Main.java index a62370c..9c581f4 100644 --- a/src/Main.java +++ b/src/Main.java @@ -7,7 +7,6 @@ public class Main { public static void main(String[] args) { List friends = new ArrayList(); - //System.out.println("Initial size is " + friends.size()); friends.add("1"); friends.add("2"); friends.add("3"); @@ -20,23 +19,58 @@ public static void main(String[] args) { friends.add("10"); friends.add("11"); + // contains test code + System.out.println(""); + System.out.println("CONTAINS TEST CODE............"); + System.out.println(""); + //String stringToTest = null; + String stringToTest = "10"; + System.out.println("Does our list contain " + stringToTest + "? " + + friends.contains(stringToTest)); + + // add( item ) test code + // add( index, item ) test code + System.out.println(""); + System.out.println("ADD TEST CODE............"); + System.out.println(""); System.out.println("Size before add: " + friends.size()); - friends.add(10, "test"); - System.out.println("Size AFTER add: " + friends.size()); + int indexToTest = 10; + String stringToAddWithIndex = " added this string"; + String stringToAddWithoutIndex = "This should be added at the end"; + + friends.add(indexToTest, stringToAddWithIndex); + friends.add(stringToAddWithoutIndex); + System.out.println("Size AFTER adds: " + friends.size()); + for (int i = 0; i < friends.size(); i++) { + System.out.println(friends.get(i)); + } + + + // remove test code + System.out.println(""); + System.out.println("REMOVE TEST CODE............"); + System.out.println(""); + System.out.println("Size BEFORE remove: " + friends.size()); + int indexToRemove = 0; + friends.remove(indexToRemove); + System.out.println("Size AFTER remove: " + friends.size()); for (int i = 0; i < friends.size(); i++) { System.out.println(friends.get(i)); } - // import java.util.Iterator; - // iterator test code -// Iterator itr = friends.iterator(); -// while (itr.hasNext()){ -// String name = itr.next(); -// System.out.println(name); -// } -// -// for (String name : friends) { -// System.out.println(name); -// } + // get test code + // set test code + System.out.println(""); + System.out.println("GET/SET TEST CODE............"); + System.out.println(""); + int indexToGet = 0; + System.out.println("Get item at index " + indexToGet + + " = " + friends.get(indexToGet) ); + int indexToSet = 0; + String newStringToSet = "Set this at index " + indexToSet; + friends.set(indexToSet, newStringToSet); + System.out.println("After set at index " + indexToGet + ": " + + friends.get(indexToSet)); + } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index b0fc22f..a607686 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -16,11 +16,24 @@ public class ArrayList implements List { private int size; + /** + * Constructor initializing private fields + */ public ArrayList(){ size = 0; data = (ItemType[]) new Object[10]; } + /** + * Second constructor that allows the client to ask for an initial capacity for + * the underlying data array (instead of using our default of 10) + * @param capacity the capacity + */ + public ArrayList(int capacity){ + size = 0; + data = (ItemType[]) new Object[capacity]; + } + /** * Returns the number of items in this collection. * @@ -51,6 +64,9 @@ public boolean isEmpty() { */ @Override public boolean contains(ItemType item) { + if ( item == null ){ + throw new NullPointerException("Item cannot be null"); + } int i = indexOf(item); if ( i != -1 ) { return true; @@ -96,6 +112,9 @@ public void add(ItemType item) { */ @Override public void remove(ItemType item) { + if ( item == null ){ + throw new NullPointerException("Item cannot be null"); + } int i = indexOf(item); if( i != -1 ){ remove(i); @@ -184,7 +203,6 @@ public ItemType get(int index) { throw new IndexOutOfBoundsException("index is beyond size"); } return data[index]; - } /** @@ -222,6 +240,13 @@ public void set(int index, ItemType item) { public void add(int index, ItemType item) { // focus on trying this one + if ( index < 0 || index >= size() ){ + throw new IndexOutOfBoundsException("Please enter a valid number"); + } + if ( item == null ){ + throw new NullPointerException("Item cannot be null"); + } + checkSize(); for (int i = size; i >= index + 1; i--) { @@ -230,7 +255,7 @@ public void add(int index, ItemType item) { data[index] = item; size++; - +// Personal try during class // if ( index < 0 || index >= size() ){ // throw new IndexOutOfBoundsException("Please enter a valid number"); // } @@ -284,6 +309,10 @@ public void remove(int index) { */ @Override public int indexOf(ItemType item) { + if ( item == null ){ + throw new NullPointerException("Item cannot be null"); + } + for (int i = 0; i < size; i++) { if ( item.equals(data[i])){ return i; @@ -305,6 +334,9 @@ public int indexOf(ItemType item) { */ @Override public int lastIndexOf(ItemType item) { + if ( item == null ){ + throw new NullPointerException("Item cannot be null"); + } return 0; } @@ -320,6 +352,9 @@ public ListIterator listIterator() { return null; } + /** + * Resizing code ( creates larger array to add new objects to ) + */ private void checkSize(){ // HOWEVER: when size becomes length - we run out of room if (size == data.length){ From 159e02bed769d5ca83f33ea0ac6f8b61df81f14f Mon Sep 17 00:00:00 2001 From: Kevin Stone Date: Mon, 30 Jan 2023 14:05:08 -0800 Subject: [PATCH 06/10] Implemented add and remove methods. --- .../greenriver/sdev333/SinglyLinkedList.java | 520 ++++++++++++++++++ 1 file changed, 520 insertions(+) create mode 100644 src/edu/greenriver/sdev333/SinglyLinkedList.java diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java new file mode 100644 index 0000000..c589310 --- /dev/null +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -0,0 +1,520 @@ +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; + } + + + + + /** + * 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) { + 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) { + if( head == null ){ + Node newNode = new Node(); + newNode.data = item; + newNode.next = null; + head = newNode; + } + else { + Node current = head; + while (current.next != null){ + current = current.next; + } + Node newNode = new Node(); + newNode.data = item; + newNode.next = null; + current.next = newNode; + + } + size++; + + } + + /** + * Removes a single instance of the specified item from this collection, + * if it is present. + * + * @param item item to be removed from this collection, if present + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void remove(ItemType item) { + + } + + /** + * 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) { + if ( index < 0 || index >= size ){ + throw new IndexOutOfBoundsException(); + } + + Node current = head; + int counter = 0; + while ( counter != index ){ + current = current.next; + counter++; + } + return current.data; + } + + /** + * Replaces the item at the specified position in this list + * with the specified item + * + * @param index index of the item to replace + * @param item item to be stored at the specified position + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void set(int index, ItemType item) { + + } + + /** + * 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) { + + 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 to the node + // BEFORE the one at the index + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = current.next; + + 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) { + + Node current = head; + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + + // 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) { + 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 new OurEnhancedIterator(); + } + + private class OurCustomIterator implements Iterator { + + // field + 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() { + // 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; + } + + /** + * Returns the next element in the iteration. + * + * @return the next element in the iteration + * @throws NoSuchElementException if the iteration has no more elements + */ + @Override + public ItemType next() { + ItemType result = currentPosition.data; + currentPosition = currentPosition.next; + return result; + } + } + + + private class OurEnhancedIterator implements ListIterator { + + private Node currentPosition; + private int currentIndex; + + public OurEnhancedIterator(){ + currentPosition = head; + currentIndex = 0; + } + + + + /** + * Returns {@code true} if this list iterator has more elements when + * traversing the list in the forward direction. (In other words, + * returns {@code true} if {@link #next} would return an element rather + * than throwing an exception.) + * + * @return {@code true} if the list iterator has more elements when + * traversing the list in the forward direction + */ + @Override + public boolean hasNext() { + return currentPosition != null; + } + + /** + * Returns the next element in the list and advances the cursor position. + * This method may be called repeatedly to iterate through the list, + * or intermixed with calls to {@link #previous} to go back and forth. + * (Note that alternating calls to {@code next} and {@code previous} + * will return the same element repeatedly.) + * + * @return the next element in the list + * @throws NoSuchElementException if the iteration has no next element + */ + @Override + public ItemType next() { + ItemType result = currentPosition.data; + currentPosition = currentPosition.next; + return result; } + + /** + * Returns {@code true} if this list iterator has more elements when + * traversing the list in the reverse direction. (In other words, + * returns {@code true} if {@link #previous} would return an element + * rather than throwing an exception.) + * + * @return {@code true} if the list iterator has more elements when + * traversing the list in the reverse direction + */ + @Override + public boolean hasPrevious() { + return false; + } + + /** + * Returns the previous element in the list and moves the cursor + * position backwards. This method may be called repeatedly to + * iterate through the list backwards, or intermixed with calls to + * {@link #next} to go back and forth. (Note that alternating calls + * to {@code next} and {@code previous} will return the same + * element repeatedly.) + * + * @return the previous element in the list + * @throws NoSuchElementException if the iteration has no previous + * element + */ + @Override + public ItemType previous() { + return null; + } + + /** + * Returns the index of the element that would be returned by a + * subsequent call to {@link #next}. (Returns list size if the list + * iterator is at the end of the list.) + * + * @return the index of the element that would be returned by a + * subsequent call to {@code next}, or list size if the list + * iterator is at the end of the list + */ + @Override + public int nextIndex() { + return 0; + } + + /** + * Returns the index of the element that would be returned by a + * subsequent call to {@link #previous}. (Returns -1 if the list + * iterator is at the beginning of the list.) + * + * @return the index of the element that would be returned by a + * subsequent call to {@code previous}, or -1 if the list + * iterator is at the beginning of the list + */ + @Override + public int previousIndex() { + return 0; + } + + /** + * Removes from the list the last element that was returned by {@link + * #next} or {@link #previous} (optional operation). This call can + * only be made once per call to {@code next} or {@code previous}. + * It can be made only if {@link #add} has not been + * called after the last call to {@code next} or {@code previous}. + * + * @throws UnsupportedOperationException if the {@code remove} + * operation is not supported by this list iterator + * @throws IllegalStateException if neither {@code next} nor + * {@code previous} have been called, or {@code remove} or + * {@code add} have been called after the last call to + * {@code next} or {@code previous} + */ + @Override + public void remove() { + + } + + /** + * Replaces the last element returned by {@link #next} or + * {@link #previous} with the specified element (optional operation). + * This call can be made only if neither {@link #remove} nor {@link + * #add} have been called after the last call to {@code next} or + * {@code previous}. + * + * @param itemType the element with which to replace the last element returned by + * {@code next} or {@code previous} + * @throws UnsupportedOperationException if the {@code set} operation + * is not supported by this list iterator + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this list + * @throws IllegalArgumentException if some aspect of the specified + * element prevents it from being added to this list + * @throws IllegalStateException if neither {@code next} nor + * {@code previous} have been called, or {@code remove} or + * {@code add} have been called after the last call to + * {@code next} or {@code previous} + */ + @Override + public void set(ItemType itemType) { + + } + + /** + * Inserts the specified element into the list (optional operation). + * The element is inserted immediately before the element that + * would be returned by {@link #next}, if any, and after the element + * that would be returned by {@link #previous}, if any. (If the + * list contains no elements, the new element becomes the sole element + * on the list.) The new element is inserted before the implicit + * cursor: a subsequent call to {@code next} would be unaffected, and a + * subsequent call to {@code previous} would return the new element. + * (This call increases by one the value that would be returned by a + * call to {@code nextIndex} or {@code previousIndex}.) + * + * @param itemType the element to insert + * @throws UnsupportedOperationException if the {@code add} method is + * not supported by this list iterator + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this list + * @throws IllegalArgumentException if some aspect of this element + * prevents it from being added to this list + */ + @Override + public void add(ItemType itemType) { + + } + } + +} From 0d1f38da0ae24c8ac031ae5f4c43331fd94f8a45 Mon Sep 17 00:00:00 2001 From: Kevin Stone Date: Mon, 30 Jan 2023 14:48:41 -0800 Subject: [PATCH 07/10] Implemented more methods. --- src/Main.java | 111 ++++++++++-------- .../greenriver/sdev333/SinglyLinkedList.java | 111 +++++++++++++++--- 2 files changed, 154 insertions(+), 68 deletions(-) diff --git a/src/Main.java b/src/Main.java index 9c581f4..34b1dcb 100644 --- a/src/Main.java +++ b/src/Main.java @@ -2,6 +2,7 @@ import edu.greenriver.sdev333.List; import java.util.Iterator; +import java.util.ListIterator; public class Main { public static void main(String[] args) { @@ -19,58 +20,70 @@ public static void main(String[] args) { friends.add("10"); friends.add("11"); - // contains test code - System.out.println(""); - System.out.println("CONTAINS TEST CODE............"); - System.out.println(""); - //String stringToTest = null; - String stringToTest = "10"; - System.out.println("Does our list contain " + stringToTest + "? " + - friends.contains(stringToTest)); +// // contains test code +// System.out.println(""); +// System.out.println("CONTAINS TEST CODE............"); +// System.out.println(""); +// //String stringToTest = null; +// String stringToTest = "10"; +// System.out.println("Does our list contain " + stringToTest + "? " + +// friends.contains(stringToTest)); +// +// // add( item ) test code +// // add( index, item ) test code +// System.out.println(""); +// System.out.println("ADD TEST CODE............"); +// System.out.println(""); +// System.out.println("Size before add: " + friends.size()); +// int indexToTest = 10; +// String stringToAddWithIndex = " added this string"; +// String stringToAddWithoutIndex = "This should be added at the end"; +// +// friends.add(indexToTest, stringToAddWithIndex); +// friends.add(stringToAddWithoutIndex); +// System.out.println("Size AFTER adds: " + friends.size()); +// for (int i = 0; i < friends.size(); i++) { +// System.out.println(friends.get(i)); +// } +// +// +// // remove test code +// System.out.println(""); +// System.out.println("REMOVE TEST CODE............"); +// System.out.println(""); +// System.out.println("Size BEFORE remove: " + friends.size()); +// int indexToRemove = 0; +// friends.remove(indexToRemove); +// System.out.println("Size AFTER remove: " + friends.size()); +// for (int i = 0; i < friends.size(); i++) { +// System.out.println(friends.get(i)); +// } +// +// // get test code +// // set test code +// System.out.println(""); +// System.out.println("GET/SET TEST CODE............"); +// System.out.println(""); +// int indexToGet = 0; +// System.out.println("Get item at index " + indexToGet + +// " = " + friends.get(indexToGet) ); +// int indexToSet = 0; +// String newStringToSet = "Set this at index " + indexToSet; +// friends.set(indexToSet, newStringToSet); +// System.out.println("After set at index " + indexToGet + ": " +// + friends.get(indexToSet)); - // add( item ) test code - // add( index, item ) test code - System.out.println(""); - System.out.println("ADD TEST CODE............"); - System.out.println(""); - System.out.println("Size before add: " + friends.size()); - int indexToTest = 10; - String stringToAddWithIndex = " added this string"; - String stringToAddWithoutIndex = "This should be added at the end"; - - friends.add(indexToTest, stringToAddWithIndex); - friends.add(stringToAddWithoutIndex); - System.out.println("Size AFTER adds: " + friends.size()); - for (int i = 0; i < friends.size(); i++) { - System.out.println(friends.get(i)); + ListIterator fancyItr = friends.listIterator(); + System.out.println("Testing listIterator<>"); + while (fancyItr.hasNext()) { + String name = fancyItr.next(); + System.out.println(name); } - - - // remove test code - System.out.println(""); - System.out.println("REMOVE TEST CODE............"); - System.out.println(""); - System.out.println("Size BEFORE remove: " + friends.size()); - int indexToRemove = 0; - friends.remove(indexToRemove); - System.out.println("Size AFTER remove: " + friends.size()); - for (int i = 0; i < friends.size(); i++) { - System.out.println(friends.get(i)); + System.out.println(); + while (fancyItr.hasPrevious()) { + String name = fancyItr.previous(); + System.out.println(name); } - // get test code - // set test code - System.out.println(""); - System.out.println("GET/SET TEST CODE............"); - System.out.println(""); - int indexToGet = 0; - System.out.println("Get item at index " + indexToGet + - " = " + friends.get(indexToGet) ); - int indexToSet = 0; - String newStringToSet = "Set this at index " + indexToSet; - friends.set(indexToSet, newStringToSet); - System.out.println("After set at index " + indexToGet + ": " - + friends.get(indexToSet)); - } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index c589310..df9eeb6 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -1,7 +1,9 @@ package edu.greenriver.sdev333; +import javax.naming.OperationNotSupportedException; import java.util.Iterator; import java.util.ListIterator; +import java.util.NoSuchElementException; public class SinglyLinkedList implements List { @@ -53,7 +55,12 @@ public boolean isEmpty() { */ @Override public boolean contains(ItemType item) { - return false; + // assume indexOf is working... + int position = indexOf(item); + if ( position == -1 ) { + return false; + } + return true; } /** @@ -92,8 +99,9 @@ public void add(ItemType item) { newNode.next = null; current.next = newNode; + size++; + } - size++; } @@ -107,7 +115,33 @@ public void add(ItemType item) { */ @Override public void remove(ItemType item) { + if (item == null) { + throw new NullPointerException(); + } + /* // alternative, easier 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--; + } + } + } } /** @@ -141,6 +175,7 @@ public boolean containsAll(Collection otherCollection) { @Override public void addAll(Collection otherCollection) { + throw new UnsupportedOperationException(); } /** @@ -222,20 +257,41 @@ public void set(int index, ItemType item) { */ @Override public void add(int index, ItemType item) { + checkIndex(index); - Node current = head; - // stop one before the position I want to insert at - for (int i = 0; i < index - 1; i++) { - current = current.next; + 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 to the node - // BEFORE the one at the index - Node theNewOne = new Node(); - theNewOne.data = item; - theNewOne.next = current.next; + // when I get here, current is pointing to the node + // BEFORE the one 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(); + } } /** @@ -248,17 +304,25 @@ public void add(int index, ItemType item) { */ @Override public void remove(int index) { + checkIndex(index); - Node current = head; - for (int i = 0; i < index - 1; i++) { - current = current.next; + if ( index == 0) { + head = head.next; } - // here, current is pointing to the node BEFORE the one - // at index - current.next = current.next.next; + else { + Node current = head; + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + + // here, current is pointing to the node BEFORE the one + // at index + current.next = current.next.next; + + size--; + } - size--; } /** @@ -273,7 +337,16 @@ public void remove(int index) { */ @Override public int indexOf(ItemType item) { - return 0; + int counter = 0; + Node current = head; + while ( current != null ) { + if ( current.data.equals(item) ) { + return counter; + } + counter++; + current = current.next; + } + return -1; } /** From ff3bd8b83530f9e39ffe86281548fcc2b92cf625 Mon Sep 17 00:00:00 2001 From: Kevin Stone Date: Wed, 1 Feb 2023 14:49:56 -0800 Subject: [PATCH 08/10] Implemented addAll method in SinglyLinkedList --- src/edu/greenriver/sdev333/SinglyLinkedList.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index df9eeb6..f0c2af3 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -174,8 +174,14 @@ public boolean containsAll(Collection otherCollection) { */ @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(0, currentItem); + } - throw new UnsupportedOperationException(); } /** From 57025a2def7b2a3ba95837feb8ba91ad6de8dea4 Mon Sep 17 00:00:00 2001 From: Kevin Stone Date: Tue, 7 Feb 2023 12:20:43 -0800 Subject: [PATCH 09/10] Implemented recursive methods. --- src/Main.java | 140 ++++---- src/edu/greenriver/sdev333/ArrayList.java | 36 ++- .../sdev333/RecursiveLinkedList.java | 305 ++++++++++++++++++ .../greenriver/sdev333/SinglyLinkedList.java | 27 ++ 4 files changed, 446 insertions(+), 62 deletions(-) create mode 100644 src/edu/greenriver/sdev333/RecursiveLinkedList.java diff --git a/src/Main.java b/src/Main.java index 34b1dcb..8363da6 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,7 @@ import edu.greenriver.sdev333.ArrayList; import edu.greenriver.sdev333.List; +import edu.greenriver.sdev333.RecursiveLinkedList; +import edu.greenriver.sdev333.SinglyLinkedList; import java.util.Iterator; import java.util.ListIterator; @@ -7,7 +9,8 @@ public class Main { public static void main(String[] args) { - List friends = new ArrayList(); + List friends = new SinglyLinkedList<>(); + System.out.println("The initial size is: " + friends.size()); friends.add("1"); friends.add("2"); friends.add("3"); @@ -19,71 +22,86 @@ public static void main(String[] args) { friends.add("9"); friends.add("10"); friends.add("11"); + ((SinglyLinkedList) friends).addToFront("Friday"); + //((ArrayList) friends).addToBack("Friday"); + System.out.println("The size is now: " + friends.size()); -// // contains test code -// System.out.println(""); -// System.out.println("CONTAINS TEST CODE............"); -// System.out.println(""); -// //String stringToTest = null; -// String stringToTest = "10"; -// System.out.println("Does our list contain " + stringToTest + "? " + -// friends.contains(stringToTest)); -// -// // add( item ) test code -// // add( index, item ) test code -// System.out.println(""); -// System.out.println("ADD TEST CODE............"); -// System.out.println(""); -// System.out.println("Size before add: " + friends.size()); -// int indexToTest = 10; -// String stringToAddWithIndex = " added this string"; -// String stringToAddWithoutIndex = "This should be added at the end"; -// -// friends.add(indexToTest, stringToAddWithIndex); -// friends.add(stringToAddWithoutIndex); -// System.out.println("Size AFTER adds: " + friends.size()); -// for (int i = 0; i < friends.size(); i++) { -// System.out.println(friends.get(i)); + + for (int i = 0; i < friends.size(); i++) { + System.out.println(friends.get(i)); + } + +// ListIterator fancyItr = friends.listIterator(); +// System.out.println("Testing listIterator<>"); +// while (fancyItr.hasNext()) { +// String name = fancyItr.next(); +// System.out.println(name); // } -// -// -// // remove test code -// System.out.println(""); -// System.out.println("REMOVE TEST CODE............"); -// System.out.println(""); -// System.out.println("Size BEFORE remove: " + friends.size()); -// int indexToRemove = 0; -// friends.remove(indexToRemove); -// System.out.println("Size AFTER remove: " + friends.size()); -// for (int i = 0; i < friends.size(); i++) { -// System.out.println(friends.get(i)); +// System.out.println(); +// while (fancyItr.hasPrevious()) { +// String name = fancyItr.previous(); +// System.out.println(name); // } -// -// // get test code -// // set test code -// System.out.println(""); -// System.out.println("GET/SET TEST CODE............"); -// System.out.println(""); -// int indexToGet = 0; -// System.out.println("Get item at index " + indexToGet + -// " = " + friends.get(indexToGet) ); -// int indexToSet = 0; -// String newStringToSet = "Set this at index " + indexToSet; -// friends.set(indexToSet, newStringToSet); -// System.out.println("After set at index " + indexToGet + ": " -// + friends.get(indexToSet)); - - ListIterator fancyItr = friends.listIterator(); - System.out.println("Testing listIterator<>"); - while (fancyItr.hasNext()) { - String name = fancyItr.next(); - System.out.println(name); + + + + + } + + + public void testArrayListClass(ArrayList friends) { + // contains test code + System.out.println(""); + System.out.println("CONTAINS TEST CODE............"); + System.out.println(""); + //String stringToTest = null; + String stringToTest = "10"; + System.out.println("Does our list contain " + stringToTest + "? " + + friends.contains(stringToTest)); + + // add( item ) test code + // add( index, item ) test code + System.out.println(""); + System.out.println("ADD TEST CODE............"); + System.out.println(""); + System.out.println("Size before add: " + friends.size()); + int indexToTest = 10; + String stringToAddWithIndex = " added this string"; + String stringToAddWithoutIndex = "This should be added at the end"; + + friends.add(indexToTest, stringToAddWithIndex); + friends.add(stringToAddWithoutIndex); + System.out.println("Size AFTER adds: " + friends.size()); + for (int i = 0; i < friends.size(); i++) { + System.out.println(friends.get(i)); } - System.out.println(); - while (fancyItr.hasPrevious()) { - String name = fancyItr.previous(); - System.out.println(name); + + + // remove test code + System.out.println(""); + System.out.println("REMOVE TEST CODE............"); + System.out.println(""); + System.out.println("Size BEFORE remove: " + friends.size()); + int indexToRemove = 0; + friends.remove(indexToRemove); + System.out.println("Size AFTER remove: " + friends.size()); + for (int i = 0; i < friends.size(); i++) { + System.out.println(friends.get(i)); } + // get test code + // set test code + System.out.println(""); + System.out.println("GET/SET TEST CODE............"); + System.out.println(""); + int indexToGet = 0; + System.out.println("Get item at index " + indexToGet + + " = " + friends.get(indexToGet) ); + int indexToSet = 0; + String newStringToSet = "Set this at index " + indexToSet; + friends.set(indexToSet, newStringToSet); + System.out.println("After set at index " + indexToGet + ": " + + friends.get(indexToSet)); } + } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index a607686..3e842df 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -41,6 +41,7 @@ public ArrayList(int capacity){ */ @Override public int size() { + return size; } @@ -51,6 +52,7 @@ public int size() { */ @Override public boolean isEmpty() { + return size == 0; } @@ -224,6 +226,35 @@ public void set(int index, ItemType item) { data[index] = item; } + + + public void addToFront(ItemType item) { + // create larger array just in case + ItemType[] temp = (ItemType[]) new Object[size * 2]; + for (int i = 0; i < size; i++) { + temp[i] = data[i]; + } + size++; + + temp[0] = item; + for (int i = 1; i < size; i++) { + temp[i] = data[i - 1]; + } + data = temp; + } + + public void addToBack(ItemType item) { + // create larger array just in case + ItemType[] temp = (ItemType[]) new Object[size * 2]; + for (int i = 0; i < size; i++) { + temp[i] = data[i]; + } + size++; + + temp[size - 1] = item; + data = temp; + } + /** * Inserts the specified item at the specified position in this list. * Shifts the item currently at that position (if any) and any subsequent @@ -414,6 +445,7 @@ public SecondCustomIterator(){ @Override public boolean hasNext() { + return currentPosition < size(); } @@ -422,7 +454,8 @@ public boolean hasNext() { public ItemType next() { ItemType result = get(currentPosition); currentPosition++; - return result; } + return result; + } @Override @@ -434,6 +467,7 @@ public boolean hasPrevious() { @Override public ItemType previous() { + return null; } diff --git a/src/edu/greenriver/sdev333/RecursiveLinkedList.java b/src/edu/greenriver/sdev333/RecursiveLinkedList.java new file mode 100644 index 0000000..f488a5c --- /dev/null +++ b/src/edu/greenriver/sdev333/RecursiveLinkedList.java @@ -0,0 +1,305 @@ +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; + } + + + + + /** + * Returns the number of items in this collection. + * + * @return the number of items in this collection + */ + @Override + public int size() { + return size(head); + } + + // private helper method that does the actual + //work + private int size (Node theHead) { + if( theHead == null ){ + return 0; + } + else { + // VV "the rest" + return size(theHead.next) + 1; + } + } + + /** + * 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) { + // adds an item to back + // loopy way: + // - created a current variable and set it the head value + // - wrote a loop while (current.next != null) + // - once we are at last node, create one and attach it + head = helpAdd(head, item); + + } + + private Node helpAdd(Node theHead, ItemType item){ + // if I'm at the end / hit bottom + if (theHead == null){ + 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; + } + } + + + + /** + * 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) { + // add (0, item) + if ( index == 0 ) { + // add to the front list may or may not be empy + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = head; + head = theNewOne; + } + else { + // anywhere else other than 0 + 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; + } + + } + + /** + * 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 index f0c2af3..6095ccf 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -42,6 +42,7 @@ public int size() { */ @Override public boolean isEmpty() { + return size == 0; } @@ -74,6 +75,17 @@ public Iterator iterator() { return new OurCustomIterator(); } + + public void addToBack(ItemType item) { + + } + public void addToFront(ItemType item) { + Node nodeToAdd = new Node(); + nodeToAdd.data = item; + nodeToAdd.next = head; + head = nodeToAdd; + size++; + } /** * Adds the specified item to the collection. * @@ -246,6 +258,21 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { + if ( index < 0 || index >= size ){ + throw new IndexOutOfBoundsException(); + } + + Node current = head; + Node temp = current; + int counter = 0; + while ( counter != index ){ + temp = current; + current = current.next; + counter++; + } + // need to finish + current.data.equals(item); + current.next = temp; } From 53be73bda7d48525ccb1d6b6e588236c46c19902 Mon Sep 17 00:00:00 2001 From: Kevin Stone Date: Mon, 13 Feb 2023 14:04:13 -0800 Subject: [PATCH 10/10] Implemented recursive methods. --- src/Main.java | 20 ++----------- src/edu/greenriver/sdev333/ArrayList.java | 30 ------------------- .../greenriver/sdev333/SinglyLinkedList.java | 17 +++++++++++ 3 files changed, 19 insertions(+), 48 deletions(-) diff --git a/src/Main.java b/src/Main.java index 8363da6..f1d2de0 100644 --- a/src/Main.java +++ b/src/Main.java @@ -21,9 +21,8 @@ public static void main(String[] args) { friends.add("8"); friends.add("9"); friends.add("10"); - friends.add("11"); - ((SinglyLinkedList) friends).addToFront("Friday"); - //((ArrayList) friends).addToBack("Friday"); + ((SinglyLinkedList) friends).addToBack("Friday"); + //((ArrayList) friends).addToFront("Friday"); System.out.println("The size is now: " + friends.size()); @@ -31,21 +30,6 @@ public static void main(String[] args) { System.out.println(friends.get(i)); } -// ListIterator fancyItr = friends.listIterator(); -// System.out.println("Testing 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); -// } - - - - } diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index 3e842df..e7be023 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -286,36 +286,6 @@ public void add(int index, ItemType item) { data[index] = item; size++; -// Personal try during class -// if ( index < 0 || index >= size() ){ -// throw new IndexOutOfBoundsException("Please enter a valid number"); -// } -// if ( item == null ){ -// throw new NullPointerException("Item cannot be null"); -// } -// -// // create larger array with one more index -// -// // copy each element stopping before the indicated index -// ItemType[] temp = (ItemType[]) new Object[size + 1]; -// for (int i = 0; i <= size; i++) { -// temp[i] = data[i]; -// -// if ( i == index ){ -// // if i equals the indicated index, assign item to index -// temp[index] = item; -// } -// // if i is greater than the index, assign temp's current index value -// // to data's previous index's value -// else if ( i > index ){ -// temp[i] = data[i - 1]; -// } -// } -// -// // increase size and repoint/reference data to point to temp array address -// size++; -// data = temp; - } diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index 6095ccf..cd5bd86 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -78,8 +78,25 @@ public Iterator iterator() { public void addToBack(ItemType item) { + Node current = head; + // stop one before the position I want to insert at + for (int i = 0; i < size - 1; i++) { + current = current.next; + } + + // when I get here, current is pointing to the node + // BEFORE the one at the index + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = current.next; + + current.next = theNewOne; + size++; } + + public void addToFront(ItemType item) { + Node nodeToAdd = new Node(); nodeToAdd.data = item; nodeToAdd.next = head;