From d594e25a83c0053fff92cb61479937bede8062ed Mon Sep 17 00:00:00 2001 From: sukanvisapearyoo <116777929+sukanvisapearyoo@users.noreply.github.com> Date: Thu, 19 Jan 2023 22:12:36 -0800 Subject: [PATCH 01/10] Create README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..d6dd83d --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# ImplementingLists +Starter files to practice implementing lists (ArrayList and LinkedList). The starter files are a reduced and modified subset of the Collection and List interfaces from the Java Collections Framework (in java.util in the JDK) to facilitate teaching and learning of foundational principles and practices in an data structures class. From b2c00e03eedcd5ce8e6a43a776565a527aba7164 Mon Sep 17 00:00:00 2001 From: Kendrick Hang Date: Wed, 11 Jan 2023 12:10:34 -0800 Subject: [PATCH 02/10] Initial commit --- src/edu/greenriver/sdev333/ArrayList.java | 292 ++++++++++++++++++++++ 1 file changed, 292 insertions(+) create mode 100644 src/edu/greenriver/sdev333/ArrayList.java diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java new file mode 100644 index 0000000..86777f1 --- /dev/null +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -0,0 +1,292 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; +import java.util.ListIterator; + +public class ArrayList implements List { + +// WE NEED FIELDS!!! + + // one plain old Java Array + private ItemType[] data; + +// one int to keep track of size +// size is the # of spots that are used in the data array +// size is DIFFERENT than length + + private int size; + + public ArrayList() { + size = 0; + data = (ItemType[]) new Object[10] ; + } + /** + * Returns the number of items in this collection. + * + * @return the number of items in this collection + */ + @Override + public int size() { + return size; + } + + /** + * Returns true if this collection contains no items. + * + * @return true if this collection contains no items + */ + @Override + public boolean isEmpty() { +// //Option B: +// if (size ==0 ){ +// return true; +// } +// return false; + + + //option B: + 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) { + //Take an item check to see if its in the array and return true or false + 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) { + + + //all of the above code until i run out of room + //when size becomes the same as length, no more room + + if (size == data.length){ + //resize up (double up array size) + + // step 1: create a new larger array (temp) + ItemType[] temp = (ItemType[]) new Object[size * 2]; + + //step 2: copy item from data to temp via for loop + for (int i = 0; i < size; i++) { + temp[i] = data[i]; //take data i and saved into temp i + } + + // step 3: re-reference data to point to new array + data = temp; + + //optional: take the null value and save in temp to override the object? + temp = null; + }// end of if (need to resize) + + data[size] = item; + size ++; + } //end of method + + /** + * Removes a single instance of the specified item from this collection, + * if it is present. + * + * @param item item to be removed from this collection, if present + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void remove(ItemType item) { + + } + + /** + * Removes all items from this collection. + * The collection will be empty after this method returns. + */ + @Override + public void clear() { + size =0; + + } + + /** + * Returns true if this collection contains all the items + * in the specified other collection. + * + * @param otherCollection collection to be checked for containment in this collection + * @return true if this collection contains all the items + * in the specified other collection + */ + @Override + public boolean containsAll(Collection otherCollection) { + throw new UnsupportedOperationException("Not implemented"); + + //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 >= size ){ + throw new IndexOutOfBoundsException("Index out of Bound"); + } + return data [index]; + + } + + /** + * Replaces the item at the specified position in this list + * with the specified item + * + * @param index index of the item to replace + * @param item item to be stored at the specified position + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void set(int index, ItemType item) { + data[index] =item; + + + } + + /** + * Inserts the specified item at the specified position in this list. + * Shifts the item currently at that position (if any) and any subsequent + * items to the right. + * + * @param index index at which the specified item is to be inserted + * @param item item to be inserted + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void add(int index, ItemType item) { + + } + + /** + * 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 2356c38210f55ad0e81f33eed3f0911f9a98931a Mon Sep 17 00:00:00 2001 From: sukanvisapearyoo Date: Thu, 19 Jan 2023 20:19:20 -0800 Subject: [PATCH 03/10] Initial commit --- .idea/vcs.xml | 6 + src/Main.java | 39 +++ src/edu/greenriver/sdev333/ArrayList.java | 291 ++++++++++++++++++++-- 3 files changed, 315 insertions(+), 21 deletions(-) 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 diff --git a/src/Main.java b/src/Main.java index 3e59c38..ed20632 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,44 @@ +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("Kuma"); + friends.add("Jazmin"); + friends.add("Jess"); + friends.add("Mint"); + friends.add("John"); + friends.add("M"); + friends.add("K"); + friends.add("A"); + friends.add("f"); + friends.add("e"); + friends.add(2,"Wednesday"); + System.out.println("Size is now " + friends.size()); + + +// //printing out friends name +// for (int i = 0; i < friends.size(); i++) { +// System.out.println(friends.get(i)); +// } + + //iterator is + 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 86777f1..f9dcfd3 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -3,6 +3,8 @@ import java.util.Iterator; import java.util.ListIterator; +import static java.util.Objects.isNull; + public class ArrayList implements List { // WE NEED FIELDS!!! @@ -58,6 +60,10 @@ public boolean isEmpty() { */ @Override public boolean contains(ItemType item) { + int i = indexOf(item); + if (i != -1){ + return true; + } //Take an item check to see if its in the array and return true or false return false; } @@ -69,19 +75,11 @@ 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) { + } + private void checkSize(){ //all of the above code until i run out of room //when size becomes the same as length, no more room @@ -94,16 +92,26 @@ public void add(ItemType item) { //step 2: copy item from data to temp via for loop for (int i = 0; i < size; i++) { - temp[i] = data[i]; //take data i and saved into temp i + temp[i] = data[i]; //take data i and saved into temp i } // step 3: re-reference data to point to new array data = temp; //optional: take the null value and save in temp to override the object? - temp = null; + temp = null; }// end of if (need to resize) + } + /** + * Adds the specified item to the collection. + * + * @param item item to be added to the collection + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void add(ItemType item) { data[size] = item; size ++; } //end of method @@ -118,7 +126,16 @@ public void add(ItemType item) { */ @Override public void remove(ItemType item) { + if(isNull(item)){ + throw new NullPointerException(); + }; + + int i = indexOf(item); + if (i != -1){ + //if its found, use the other remove method to do the work + remove(i); + } } /** @@ -141,9 +158,22 @@ public void clear() { */ @Override public boolean containsAll(Collection otherCollection) { - throw new UnsupportedOperationException("Not implemented"); - - //return false; + Iterator itr = (Iterator) otherCollection.iterator(); + while(itr.hasNext()){ + ItemType itemToCheck = itr.next(); + if (!contains(itemToCheck)){ + return false; + } + } + return true; +// for (ItemType itemToCheck : otherCollection){ +// if (!contains(itemToCheck)){ +// return false; +// } +// } +// //throw new UnsupportedOperationException("Not implemented"); +// +// return true; } @@ -193,6 +223,7 @@ public void retainAll(Collection otherCollection) { */ @Override public ItemType get(int index) { + //check data if user enter data beyond what was set, it'll throw index out of bound if (index >= size ){ throw new IndexOutOfBoundsException("Index out of Bound"); } @@ -232,7 +263,11 @@ public void set(int index, ItemType item) { */ @Override public void add(int index, ItemType item) { - + for (int i = size; i >= index +1 ; i--) { + data[i]= data[i -1]; + } + data[2] = item; + size++; } /** @@ -245,7 +280,9 @@ public void add(int index, ItemType item) { */ @Override public void remove(int index) { - + for (int i = index; i < size-1; i++){ + data[i] = data[i+1]; + } } /** @@ -260,7 +297,13 @@ public void remove(int index) { */ @Override public int indexOf(ItemType item) { - return 0; + + for (int i = 0; i < size ; i++) { + if (item == data[i]){ + return i; + } + } + return -1; } /** @@ -275,7 +318,12 @@ public int indexOf(ItemType item) { */ @Override public int lastIndexOf(ItemType item) { - return 0; + for (int i = size; i >= 0; i++) { + if (data[i].equals(item)) { + return i; + } + } + return -1; } /** @@ -289,4 +337,205 @@ public int lastIndexOf(ItemType item) { public ListIterator listIterator() { return null; } -} + + private class OurCustomIterator implements Iterator{ + + private int currentPosition; + + public OurCustomIterator() { + this.currentPosition = 0; + } + + /** + * Returns {@code true} if the iteration has more elements. + * (In other words, returns {@code true} if {@link #next} would + * return an element rather than throwing an exception.) + * + * @return {@code true} if the iteration has more elements + */ + @Override + public boolean hasNext() { + return currentPosition < size(); + } + + /** + * Returns the next element in the iteration. + * + * @return the next element in the iteration + */ + @Override + public ItemType next() { + ItemType result = get(currentPosition); + currentPosition++; + return result; + } + } + private class SecondCustomIerator implements ListIterator{ + + private int currentPosition; + + public SecondCustomIerator(){ + currentPosition = 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 false; + } + + /** + * 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 + */ + @Override + public ItemType next() { + return null; + } + + /** + * 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() { + //hasNext checked currentPosition with size + //hasPrevious check currentPosition against 0 + 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 + * 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) { + + + + } + } + +}//end of class From d429b603877ff5ec8deba2234e5ae7b8c83da3db Mon Sep 17 00:00:00 2001 From: sukanvisapearyoo Date: Wed, 8 Feb 2023 18:37:12 -0800 Subject: [PATCH 04/10] Initial commit --- src/edu/greenriver/sdev333/ArrayList.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index f9dcfd3..b745257 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -124,6 +124,8 @@ public void add(ItemType item) { * @throws NullPointerException if the specified item is null * and this collection does not permit null items */ + + @Override public void remove(ItemType item) { if(isNull(item)){ From 6e1f359605bca2f87492420dcdce3632a8c19161 Mon Sep 17 00:00:00 2001 From: sukanvisapearyoo Date: Wed, 8 Feb 2023 18:40:41 -0800 Subject: [PATCH 05/10] Initial commit --- src/edu/greenriver/sdev333/ArrayList.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index b745257..d6b4bfd 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -248,7 +248,6 @@ public ItemType get(int index) { public void set(int index, ItemType item) { data[index] =item; - } /** From b973ad78e487b6e35f9717651303d1daddb99a3c Mon Sep 17 00:00:00 2001 From: sukanvisapearyoo Date: Fri, 10 Feb 2023 18:50:51 -0800 Subject: [PATCH 06/10] Initial commit --- src/edu/greenriver/sdev333/ArrayList.java | 261 +++++----------------- 1 file changed, 61 insertions(+), 200 deletions(-) diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index d6b4bfd..b7d33a4 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -60,11 +60,11 @@ public boolean isEmpty() { */ @Override public boolean contains(ItemType item) { + int i = indexOf(item); - if (i != -1){ + if (i != -1) { return true; } - //Take an item check to see if its in the array and return true or false return false; } @@ -75,34 +75,21 @@ public boolean contains(ItemType item) { */ @Override public Iterator iterator() { - return new OurCustomIterator(); - + return new CustomIterator(); } - private void checkSize(){ - - //all of the above code until i run out of room - //when size becomes the same as length, no more room - if (size == data.length){ - //resize up (double up array size) - - // step 1: create a new larger array (temp) + private void checkSize() { + if(size == data.length) { + //resize data ItemType[] temp = (ItemType[]) new Object[size * 2]; - - //step 2: copy item from data to temp via for loop for (int i = 0; i < size; i++) { - temp[i] = data[i]; //take data i and saved into temp i + temp[i] = data[i]; } - - // step 3: re-reference data to point to new array data = temp; - - //optional: take the null value and save in temp to override the object? temp = null; - }// end of if (need to resize) + } } - /** * Adds the specified item to the collection. * @@ -112,9 +99,11 @@ private void checkSize(){ */ @Override public void add(ItemType item) { + //works until you hit length of array data[size] = item; - size ++; - } //end of method + size++; + checkSize(); + } /** * Removes a single instance of the specified item from this collection, @@ -124,18 +113,10 @@ public void add(ItemType item) { * @throws NullPointerException if the specified item is null * and this collection does not permit null items */ - - @Override public void remove(ItemType item) { - if(isNull(item)){ - throw new NullPointerException(); - }; - - int i = indexOf(item); - if (i != -1){ - //if its found, use the other remove method to do the work + if(i != -1) { remove(i); } } @@ -146,8 +127,9 @@ public void remove(ItemType item) { */ @Override public void clear() { - size =0; - + for (int i = 0; i < size; i++) { + data[i] = null; + } } /** @@ -160,23 +142,15 @@ public void clear() { */ @Override public boolean containsAll(Collection otherCollection) { + Iterator itr = (Iterator) otherCollection.iterator(); - while(itr.hasNext()){ + while (itr.hasNext()) { ItemType itemToCheck = itr.next(); - if (!contains(itemToCheck)){ + if (!contains(itemToCheck)) { return false; } } return true; -// for (ItemType itemToCheck : otherCollection){ -// if (!contains(itemToCheck)){ -// return false; -// } -// } -// //throw new UnsupportedOperationException("Not implemented"); -// -// return true; - } /** @@ -225,12 +199,10 @@ public void retainAll(Collection otherCollection) { */ @Override public ItemType get(int index) { - //check data if user enter data beyond what was set, it'll throw index out of bound - if (index >= size ){ - throw new IndexOutOfBoundsException("Index out of Bound"); + if(index >= size) { + throw new IndexOutOfBoundsException("No such index"); } - return data [index]; - + return data[index]; } /** @@ -246,8 +218,10 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { - data[index] =item; - + if(index >= size) { + throw new IndexOutOfBoundsException("No such index"); + } + data[index] = item; } /** @@ -264,10 +238,14 @@ public void set(int index, ItemType item) { */ @Override public void add(int index, ItemType item) { - for (int i = size; i >= index +1 ; i--) { - data[i]= data[i -1]; + + checkSize(); + + for (int i = size; i >= index + 1; i--) { + data[i] = data[i - 1]; } - data[2] = item; + + data[index] = item; size++; } @@ -281,9 +259,12 @@ public void add(int index, ItemType item) { */ @Override public void remove(int index) { - for (int i = index; i < size-1; i++){ - data[i] = data[i+1]; + + for (int i = index; i < size - 1; i++) { + data[i] = data[i - 1]; } + data[size] = null; + size--; } /** @@ -299,8 +280,8 @@ public void remove(int index) { @Override public int indexOf(ItemType item) { - for (int i = 0; i < size ; i++) { - if (item == data[i]){ + for (int i = 0; i < size; i++) { + if(item.equals(data[i])) { return i; } } @@ -319,12 +300,12 @@ public int indexOf(ItemType item) { */ @Override public int lastIndexOf(ItemType item) { - for (int i = size; i >= 0; i++) { - if (data[i].equals(item)) { + for (int i = 0; i < size; i++) { + if(data.equals(item)){ return i; } } - return -1; + return 0; } /** @@ -339,31 +320,19 @@ public ListIterator listIterator() { return null; } - private class OurCustomIterator implements Iterator{ + private class CustomIterator implements Iterator{ private int currentPosition; - public OurCustomIterator() { - this.currentPosition = 0; + public CustomIterator () { + currentPosition = 0; } - /** - * Returns {@code true} if the iteration has more elements. - * (In other words, returns {@code true} if {@link #next} would - * return an element rather than throwing an exception.) - * - * @return {@code true} if the iteration has more elements - */ @Override public boolean hasNext() { return currentPosition < size(); } - /** - * Returns the next element in the iteration. - * - * @return the next element in the iteration - */ @Override public ItemType next() { ItemType result = get(currentPosition); @@ -371,172 +340,64 @@ public ItemType next() { return result; } } - private class SecondCustomIerator implements ListIterator{ + + private class SecondCustomIterator implements ListIterator { + + //Fancier Iterator that goes forwards and backwards private int currentPosition; - public SecondCustomIerator(){ + public SecondCustomIterator() { currentPosition = 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 false; + return currentPosition < size(); } - /** - * 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 - */ @Override public ItemType next() { - return null; + ItemType result = get(currentPosition); + currentPosition++; + 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() { - //hasNext checked currentPosition with size - //hasPrevious check currentPosition against 0 - return false; + return currentPosition > 0; } - /** - * 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 - * element - */ @Override public ItemType previous() { - - return null; + ItemType result = get(currentPosition); + currentPosition--; + return result; } - /** - * 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) { - - } } - -}//end of class +} \ No newline at end of file From ea6932aeefff4636ce8f4bd4863a649832295e1b Mon Sep 17 00:00:00 2001 From: sukanvisapearyoo Date: Fri, 10 Feb 2023 18:57:03 -0800 Subject: [PATCH 07/10] Initial commit --- .../greenriver/sdev333/SinglyLinkedList.java | 510 ++++++++++++++++++ 1 file changed, 510 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..933999b --- /dev/null +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -0,0 +1,510 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; +import java.util.ListIterator; + +//singly linked list will have to take in some generic items +// and implements our interface +public class SinglyLinkedList implements List { + + // Fields - what does a linked list actually have in it? + //HEAD!! + private Node head; // we need a node class to store + + private int size; + + // helper/inner classes + private class Node { + ItemType data; + Node next; + //we created a new class called Node and while we + // created a new class, the next is going to refer to Node + } + + public SinglyLinkedList() { + //initializing to null, there is no nodes our list is empty + // which means it has no head + head = null; + size = 0; + + } + + /** + * Returns the number of items in this collection. + * + * @return the number of items in this collection + */ + @Override + public int size() { + return size; + } + + /** + * Returns true if this collection contains no items. + * + * @return true if this collection contains no items + */ + @Override + public boolean isEmpty() { + return size == 0; + //or head == null; + } + + /** + * Returns true if this collection contains the specified item. + * + * @param item items whose presence in this collection is to be tested + * @return true if this collection contains the specified item + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public boolean contains(ItemType item) { + // assume indexOf is working + int position = lastIndexOf(item); + if (position == -1) { + return false; + } + return true; + } + + /** + * Returns an iterator over the elements in this collection. + * + * @return an Iterator over the elements in this collection + */ + @Override + public Iterator iterator() { + return new OurCustomIterator(); + } + + /** + * Adds the specified item to the collection. + * + * @param item item to be added to the collection + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void add(ItemType item) { + //assuming the list is empty + if (head != null) { + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = null; + size++; + } + //assuming the list has nodes in it already + Node current = head; + while (current.next != null) { + current = current.next; + Node theNewNode = new Node(); + theNewNode.data = item; + theNewNode.next = null; + current.next = theNewNode; + 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) { + //first check if item is null + if (item == null) { + throw new NullPointerException(); + } + + //alternative - easier to write, but less efficient + /*int position = indexOf(item); + if (position != -1){ + remove(position); + }*/ + + if (head.data == item) { + head = head.next; + size--; + } else { + Node current = head; + Node previous; + while (current.next != null){ + previous = current; + current = current.next; + + if (current.data.equals(item)) { + previous.next = current.next; + size--; + } + } + } + } + + /** + * Removes all items from this collection. + * The collection will be empty after this method returns. + */ + @Override + public void clear() { + head = null; + size = 0; + } + + /** + * Returns true if this collection contains all the items + * in the specified other collection. + * + * @param otherCollection collection to be checked for containment in this collection + * @return true if this collection contains all the items + * in the specified other collection + */ + @Override + public boolean containsAll(Collection otherCollection) { + return false; + } + + /** + * Adds all the items in this specified other collection to this collection. + * + * @param otherCollection collection containing items to be added to this collection + */ + @Override + public void addAll(Collection otherCollection) { + // walk through the other collection + // use for-each loop + // or an Iterator + + Iterator itr = (Iterator)otherCollection.iterator(); + while (itr.hasNext()){ + ItemType currentItem = itr.next(); + add(0, currentItem); + } + } + + /** + * Removes all of this collection's items that are also contained in the + * specified other collection. After this call returns, this collection will + * contain no elements in common with the specified other collection. + * + * @param otherCollection collection containing elements to be removed + * from this collection + */ + @Override + public void removeAll(Collection otherCollection) { + + } + + /** + * 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) { + //checking to see if the index is valid + checkIndex(index); + + // if someone wants to add at the beginning, + if (index == 0) { + //I need to change the head + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = head; + + head = theNewOne; + } + + // making a variable called current that is a copy of head + Node current = head; + + // start at the same position then move current forward + // its going to move current forward "x" amount of times according to the index + // current stops one before the position we want to be at + for (int i = 0; i < index-1; i++) { + current = current.next; + } + + //when I get here, current is pointing to the node *BEFORE* the index + Node theNewOne = new Node(); + //theNewOne takes the new item + theNewOne.data = item; + //we set next equals to current because current has the address of + // our previous node + theNewOne.next = current.next; + // lastly we want our current next to point to theNewOne since it is now before it + current.next = theNewOne; + //then increment the size + size++; + + + } + + // adding to the front of my linked list + public void addFront(ItemType item){ + // we can do this way if we have our add(Int index, ItemType item) working + // add(0, item); + + // if not, then we need to create a new node that we will be able to link + // at the front of the list + + + } + + private void checkIndex(int index) { + //throw an exception for someone wanting to remove at a + // negative index or index greater than the size + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException(); + } + } + + /** + * Removes the element at the specified position in this list. + * Shifts any subsequent items to the left. + * + * @param index the index of the item to be removed + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void remove(int index) { + checkIndex(index); + + // if we are removing at index 0 (our head) + if (index == 0){ + head = head.next; + } + //if we are removing anywhere else/in the middle + else { + Node current = head; + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + // when I get here, current is pointing to the node BEFORE + // the one at the index + current.next = current.next.next; + } + //when removing reduce size + 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) { + //make a counter to keep track of index + int counter = 0; + //make a current node + Node current = head; + while (current != null) { + if (current.data.equals(item)){ + return counter; + } + counter++; + current = current.next; + } + return -1; + } + + /** + * Returns the index of the last occurrence of the specified item + * in this list, or -1 if this list does not contain the item. + * + * @param item the item to search for + * @return the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item + * @throws NullPointerException if the specified item is null and this + * list does not permit null items + */ + @Override + public int lastIndexOf(ItemType item) { + 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; + } + + + //helper class for the iterator method + private class OurCustomIterator implements Iterator{ + + // field to keep track of the current position + private Node currentPosition; + + //constructor + public OurCustomIterator(){ + //initialize it to head (the front of the list) + 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() { + //how to tell if were at the end of the list (aka 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 no Such Element Exception if the iteration has no more elements + */ + @Override + public ItemType next() { + ItemType result = currentPosition.data; + currentPosition = currentPosition.next; + return result; + } + } + + + //class for ListIterator + private class OurEnhancedIterator implements ListIterator { + + private Node currentPosition; + + 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) { + + } + } +} \ No newline at end of file From d1c2272420517ef9d7c3c79cd6eee0ad70c4d8db Mon Sep 17 00:00:00 2001 From: sukanvisapearyoo Date: Fri, 10 Feb 2023 18:58:09 -0800 Subject: [PATCH 08/10] LinkedList class modified trial #4... --- src/edu/greenriver/sdev333/SinglyLinkedList.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index 933999b..038c290 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -105,6 +105,7 @@ public void add(ItemType item) { size++; } } + // /** * Removes a single instance of the specified item from this collection, From 6f8751bcd4d6df70bd46ebb07ecbac1ea9646077 Mon Sep 17 00:00:00 2001 From: sukanvisapearyoo Date: Mon, 13 Feb 2023 21:33:18 -0800 Subject: [PATCH 09/10] LinkedList class modified trial #4... --- src/Main.java | 11 ++- .../greenriver/sdev333/SinglyLinkedList.java | 95 ++++++++++++------- 2 files changed, 71 insertions(+), 35 deletions(-) diff --git a/src/Main.java b/src/Main.java index ed20632..5b25f4f 100644 --- a/src/Main.java +++ b/src/Main.java @@ -16,7 +16,7 @@ public static void main(String[] args) { friends.add("Jess"); friends.add("Mint"); friends.add("John"); - friends.add("M"); + friends.add("John"); friends.add("K"); friends.add("A"); friends.add("f"); @@ -24,12 +24,20 @@ public static void main(String[] args) { friends.add(2,"Wednesday"); System.out.println("Size is now " + friends.size()); + System.out.println("John is at: " + friends.lastIndexOf("Dee")); + System.out.println("Last index of John: " + friends.lastIndexOf("John")); + //printing out everything in our list + for (int i = 0; i < friends.size(); i++) { + System.out.println(friends.get(i)); + } + // //printing out friends name // for (int i = 0; i < friends.size(); i++) { // System.out.println(friends.get(i)); // } + //iterator is Iterator itr = friends.iterator(); while (itr.hasNext()){ @@ -40,5 +48,6 @@ public static void main(String[] args) { for (String name : friends){ 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 038c290..e3d9e11 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -2,6 +2,7 @@ import java.util.Iterator; import java.util.ListIterator; +import java.util.NoSuchElementException; //singly linked list will have to take in some generic items // and implements our interface @@ -17,8 +18,8 @@ public class SinglyLinkedList implements List { private class Node { ItemType data; Node next; - //we created a new class called Node and while we - // created a new class, the next is going to refer to Node + //we created a new class called Node. Node next is going to refer to Node + Node previous; } public SinglyLinkedList() { @@ -47,7 +48,7 @@ public int size() { @Override public boolean isEmpty() { return size == 0; - //or head == null; + //or return head == null; } /** @@ -60,12 +61,20 @@ public boolean isEmpty() { */ @Override public boolean contains(ItemType item) { - // assume indexOf is working - int position = lastIndexOf(item); - if (position == -1) { - return false; + //create node to hold our place + Node current = head; + //while current place does not equal null + while(current != null){ + //check if the data field in current matches the parameter. if it does, return true + if(current.data == item){ + return true; + } + //if current.data does not match the parameter, set current to point to + // the next node in the list, until current is null. + current = current.next; } - return true; + + return false; } /** @@ -177,8 +186,9 @@ public boolean containsAll(Collection otherCollection) { @Override public void addAll(Collection otherCollection) { // walk through the other collection - // use for-each loop - // or an Iterator + + // use for-each loop or an Iterator + //cast in iterator Iterator itr = (Iterator)otherCollection.iterator(); while (itr.hasNext()){ @@ -197,6 +207,7 @@ public void addAll(Collection otherCollection) { */ @Override public void removeAll(Collection otherCollection) { + throw new UnsupportedOperationException(); } @@ -210,6 +221,7 @@ public void removeAll(Collection otherCollection) { */ @Override public void retainAll(Collection otherCollection) { + throw new UnsupportedOperationException(); } @@ -228,13 +240,19 @@ public ItemType get(int index) { } Node current = head; int counter = 0; + while (counter != index) { current = current.next; counter++; } return current.data; +// Still need the current variable and counter +// for (int i = 0; i < index; i++) { +// current = current.next; +// } } + /** * Replaces the item at the specified position in this list * with the specified item @@ -248,6 +266,14 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { + Node current = head; + int counter = 0; + while (counter != index) { + current = current.next; + counter++; + } + current.data = item; + } @@ -268,9 +294,9 @@ public void add(int index, ItemType item) { //checking to see if the index is valid checkIndex(index); - // if someone wants to add at the beginning, + // add at the beginning, if (index == 0) { - //I need to change the head + //change the head Node theNewOne = new Node(); theNewOne.data = item; theNewOne.next = head; @@ -278,24 +304,23 @@ public void add(int index, ItemType item) { head = theNewOne; } - // making a variable called current that is a copy of head + // making a variable called current that copy head Node current = head; // start at the same position then move current forward // its going to move current forward "x" amount of times according to the index - // current stops one before the position we want to be at + // current stops one before the position for (int i = 0; i < index-1; i++) { current = current.next; } - //when I get here, current is pointing to the node *BEFORE* the index + //current is pointing to the node BEFORE the index Node theNewOne = new Node(); - //theNewOne takes the new item + //theNewOne gets the new item theNewOne.data = item; - //we set next equals to current because current has the address of - // our previous node + //set next equals to current theNewOne.next = current.next; - // lastly we want our current next to point to theNewOne since it is now before it + // current next point to theNewOne current.next = theNewOne; //then increment the size size++; @@ -303,21 +328,14 @@ public void add(int index, ItemType item) { } - // adding to the front of my linked list public void addFront(ItemType item){ - // we can do this way if we have our add(Int index, ItemType item) working - // add(0, item); - - // if not, then we need to create a new node that we will be able to link - // at the front of the list - } private void checkIndex(int index) { - //throw an exception for someone wanting to remove at a // negative index or index greater than the size - if (index < 0 || index >= size) { + if (index < 0 || index > size) { + //throw an exception for someone wanting to remove at a throw new IndexOutOfBoundsException(); } } @@ -334,18 +352,17 @@ private void checkIndex(int index) { public void remove(int index) { checkIndex(index); - // if we are removing at index 0 (our head) + // if we are removing at head (index 0) if (index == 0){ head = head.next; } - //if we are removing anywhere else/in the middle + // else/in the middle else { Node current = head; for (int i = 0; i < index - 1; i++) { current = current.next; } - // when I get here, current is pointing to the node BEFORE - // the one at the index + // current is pointing to the node BEFORE index current.next = current.next.next; } //when removing reduce size @@ -365,7 +382,7 @@ public void remove(int index) { */ @Override public int indexOf(ItemType item) { - //make a counter to keep track of index + //create counter to keep track of index int counter = 0; //make a current node Node current = head; @@ -376,6 +393,7 @@ public int indexOf(ItemType item) { counter++; current = current.next; } + //this means its not found if we get here return -1; } @@ -391,7 +409,13 @@ public int indexOf(ItemType item) { */ @Override public int lastIndexOf(ItemType item) { - return 0; + int lastIndex = -1; + //create a new node to hold the position + Node current = head; + // a loop to look through the nodes in the list.. + + + return lastIndex; } /** @@ -468,6 +492,9 @@ public boolean hasNext() { @Override public ItemType next() { + if (!hasNext()){ + throw new NoSuchElementException(); + } ItemType result = currentPosition.data; currentPosition = currentPosition.next; return result; From d3169a25c658eb82e7da00a74cb2cdcb55eeb379 Mon Sep 17 00:00:00 2001 From: sukanvisapearyoo Date: Mon, 13 Feb 2023 21:41:20 -0800 Subject: [PATCH 10/10] LinkedList class modified trial #4... --- src/edu/greenriver/sdev333/SinglyLinkedList.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index e3d9e11..3c2ae2c 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -294,6 +294,7 @@ public void add(int index, ItemType item) { //checking to see if the index is valid checkIndex(index); + // add at the beginning, if (index == 0) { //change the head