From bdd89f3b7249456dc17017ce1b3eb0d786547505 Mon Sep 17 00:00:00 2001 From: tobygoetz Date: Tue, 9 Jan 2024 13:18:03 -0800 Subject: [PATCH 01/17] Added starter code, finished some of the easy methods --- .idea/inspectionProfiles/Project_Default.xml | 299 +++++++++++++++++++ src/ArrayIntList.java | 171 +++++++++++ src/Main.java | 4 + 3 files changed, 474 insertions(+) create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 src/ArrayIntList.java diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..1b2425b --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,299 @@ + + + + \ No newline at end of file diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java new file mode 100644 index 0000000..fc9d4bd --- /dev/null +++ b/src/ArrayIntList.java @@ -0,0 +1,171 @@ +import java.util.Iterator; + +public class ArrayIntList implements IntList{ + + // fields: + private int size; + private int[] buffer; + + public ArrayIntList() { + //initialize fields + size = 0; + buffer = new int[10]; + } + /** + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) { + for (int i = size; i >= 0; i--) { + buffer[i] = buffer[i - 1]; + + } + + // put the value at the front of the array at position 0 + buffer[0] = value; + size++; + + } + + /** + * Appends (inserts) the specified value at the back of the list (at index size()-1). + * + * @param value value to be inserted + */ + @Override + public void addBack(int value) { + //TODO: check to see if we are full - if so, we need to create a larger buffer + buffer[size] = value; + size++; + + } + + /** + * Inserts the specified value at the specified position in this list. + * Shifts the value currently at that position (if any) and any subsequent + * values to the right. + * + * @param index index at which the specified value is to be inserted + * @param value value to be inserted + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public void add(int index, int value) { + + } + + /** + * Removes the value located at the front of the list + * (at index 0), if it is present. + * Shifts any subsequent values to the left. + */ + @Override + public void removeFront() { + + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() { + + } + + /** + * Removes the value at the specified position in this list. + * Shifts any subsequent values to the left. Returns the value + * that was removed from the list. + * + * @param index the index of the value to be removed + * @return the value previously at the specified position + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int remove(int index) { + return 0; + } + + /** + * Returns the value at the specified position in the list. + * + * @param index index of the value to return + * @return the value at the specified position in this list + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int get(int index) { + if (index > size) { + throw new IndexOutOfBoundsException("No Such Index Value..."); + } + return buffer[index]; + } + + /** + * Returns true if this list contains the specified value. + * + * @param value value whose presence in this list is to be searched for + * @return true if this list contains the specified value + */ + @Override + public boolean contains(int value) { + return false; + } + + /** + * Returns the index of the first occurrence of the specified value + * in this list, or -1 if this list does not contain the value. + * + * @param value value to search for + * @return the index of the first occurrence of the specified value in this list + * or -1 if this list does not contain the value + */ + @Override + public int indexOf(int value) { + return 0; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Returns the number of values in this list. + * + * @return the number of values in this list + */ + @Override + public int size() { + return 0; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() { + + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } +} diff --git a/src/Main.java b/src/Main.java index 930198c..32309d1 100644 --- a/src/Main.java +++ b/src/Main.java @@ -6,6 +6,10 @@ public static void main(String[] args) { // to see how IntelliJ IDEA suggests fixing it. System.out.printf("Hello and welcome!"); + IntList firstList; + + ArrayIntList secondList = new ArrayIntList(); + for (int i = 1; i <= 5; i++) { //TIP Press to start debugging your code. We have set one breakpoint // for you, but you can always add more by pressing . From 906b71991472589bea665a957752e7a833e67db8 Mon Sep 17 00:00:00 2001 From: tobygoetz Date: Tue, 9 Jan 2024 13:47:07 -0800 Subject: [PATCH 02/17] Added starter code, finished some of the easy methods --- src/ArrayIntList.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java index fc9d4bd..966b3c6 100644 --- a/src/ArrayIntList.java +++ b/src/ArrayIntList.java @@ -88,7 +88,26 @@ public void removeBack() { */ @Override public int remove(int index) { - return 0; + // first, check the index to see if it is valid + if ( index < 0) { + throw new IndexOutOfBoundsException("Index cannot be negative"); + } else if (index >= size) { + throw new IndexOutOfBoundsException("Index is higher than size"); + } + + // save a copy of the value to be removed so we can return it later + int copyOfRemovedValue = buffer[index]; + + //shift values to the left + for (int i = 0; i <= size - 1; i++) { + buffer[i] = buffer[i + 1]; + + } + + buffer[size - 1] = 0; + size--; + + return copyOfRemovedValue; } /** From d005389917b0561425ab3a41a16ad5141c44fff8 Mon Sep 17 00:00:00 2001 From: tobygoetz Date: Tue, 9 Jan 2024 13:57:55 -0800 Subject: [PATCH 03/17] Added starter code, finished some of the easy methods --- src/ArrayIntList.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java index 966b3c6..826b689 100644 --- a/src/ArrayIntList.java +++ b/src/ArrayIntList.java @@ -175,7 +175,11 @@ public int size() { */ @Override public void clear() { + for (int i = 0; i < size; i++) { + buffer[i] = 0; + } + size = 0; } /** From ab7b7b21582f81943ef868efa4b6f88a5bf99367 Mon Sep 17 00:00:00 2001 From: tobygoetz Date: Tue, 9 Jan 2024 14:49:19 -0800 Subject: [PATCH 04/17] Added starter code, finished some of the easy methods --- src/ArrayIntList.java | 78 +++++++++++++++++++++++++++++++++++++++++-- src/Main.java | 18 ++++++++++ 2 files changed, 93 insertions(+), 3 deletions(-) diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java index 826b689..1f30df6 100644 --- a/src/ArrayIntList.java +++ b/src/ArrayIntList.java @@ -39,6 +39,11 @@ public void addFront(int value) { @Override public void addBack(int value) { //TODO: check to see if we are full - if so, we need to create a larger buffer + + if ( size == buffer.length) { + resize(size * 2); + } + buffer[size] = value; size++; @@ -55,6 +60,9 @@ public void addBack(int value) { */ @Override public void add(int index, int value) { + if ( size == buffer.length) { + resize(size * 2); + } } @@ -175,13 +183,33 @@ public int size() { */ @Override public void clear() { - for (int i = 0; i < size; i++) { - buffer[i] = 0; - } +// for (int i = 0; i < size; i++) { +// buffer[i] = 0; +// } +// +// size = 0; + buffer = new int[10]; size = 0; } + private void resize(int newSize) { + //create new space, separate from the old space (buffer) + int[] newBuffer = new int[newSize]; + + // copy everything over from buffer into newBuffer + for (int i = 0; i < buffer.length; i++) { + newBuffer[i] = buffer[i]; + + } + + // set the new space into buffer + buffer = newBuffer; + + // the old space is no longer "pointed to" and will eventually + // be cleaned up by the garbage collector + } + /** * Returns an iterator over elements of type {@code T}. * @@ -189,6 +217,50 @@ public void clear() { */ @Override public Iterator iterator() { + + //iterators are what enables main/client to use a for-each lop on IntList return null; } + + //create a private helper Iterator class + private class IntListIterator implements Iterator { + + // private fields: + private int i; + + private IntListIterator() { + i = 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 i < size; + } + + /** + * 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 Integer next() { + //check to see if i is greater than size +// if ( i >= size) { +// throw new +// } + + int currentValue = buffer[i]; + i++; + return currentValue; + } + } + } diff --git a/src/Main.java b/src/Main.java index 32309d1..6667b1a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,3 +1,5 @@ +import java.util.Iterator; + //TIP To Run code, press or // click the icon in the gutter. public class Main { @@ -15,5 +17,21 @@ public static void main(String[] args) { // for you, but you can always add more by pressing . System.out.println("i = " + i); } + + IntList thirdList = new ArrayIntList(); + thirdList.addFront(15); + thirdList.addFront(12); + thirdList.addBack(8); + + for (int value : thirdList) { + System.out.println(value); + } + + //alternate way to use an iterator + Iterator itr = thirdList.iterator(); + while (itr.hasNext()) { + int value = itr.next(); + System.out.println(value); + } } } \ No newline at end of file From 1bab7089ca8dd90417c680a7b5cfa7c69e617bbe Mon Sep 17 00:00:00 2001 From: tobygoetz Date: Tue, 16 Jan 2024 14:36:41 -0800 Subject: [PATCH 05/17] Started DoublyLinkedIntList w/ fields, constructor and created addBack() and removeBack() --- src/DoublyLinkedIntList.java | 205 +++++++++++++++++++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 src/DoublyLinkedIntList.java diff --git a/src/DoublyLinkedIntList.java b/src/DoublyLinkedIntList.java new file mode 100644 index 0000000..52bb853 --- /dev/null +++ b/src/DoublyLinkedIntList.java @@ -0,0 +1,205 @@ +import java.util.Iterator; + +public class DoublyLinkedIntList implements IntList { + + // Fields + private Node pre; + private Node post; + private int size; + + // Constructor + public DoublyLinkedIntList() { + // an empty list has two sentinel (dummy) nodes that serve as bookends + pre = new Node(); + post = new Node(); + pre.next = post; + post.prev = pre; + size = 0; + + } + + private class Node { + int data; + Node next; // address of te node 'after' this one in line + Node prev; // addres of the node 'before' this one in line + + public Node() { + next = null; + prev = null; + } + } + + + + /** + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) { + + } + + /** + * Appends (inserts) the specified value at the back of the list (at index size()-1). + * + * @param value value to be inserted + */ + @Override + public void addBack(int value) { + Node theLastNode = post.prev; + + // set up my new node and fill it out (data, prev, next) + Node theNewNode = new Node(); + theNewNode.data = value; + theNewNode.next = post; + theNewNode.prev = theLastNode; + + // go to the end of the list's sentinel and update it's prev + post.prev = theNewNode; + + // go to the node before the new one and update it's next + theLastNode.next = theNewNode; + + size++; + + } + + /** + * Inserts the specified value at the specified position in this list. + * Shifts the value currently at that position (if any) and any subsequent + * values to the right. + * + * @param index index at which the specified value is to be inserted + * @param value value to be inserted + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public void add(int index, int value) { + + } + + /** + * Removes the value located at the front of the list + * (at index 0), if it is present. + * Shifts any subsequent values to the left. + */ + @Override + public void removeFront() { + + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() { + if (size > 0) { + // set up a temp variable for convenience + Node theOneToRemove = post.prev; + + theOneToRemove.prev.next = post; + post.prev = theOneToRemove.prev; + + // optional to clean up + theOneToRemove.next = null; + theOneToRemove.prev = null; + theOneToRemove.data = 0; + + size--; + } + } + + /** + * Removes the value at the specified position in this list. + * Shifts any subsequent values to the left. Returns the value + * that was removed from the list. + * + * @param index the index of the value to be removed + * @return the value previously at the specified position + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int remove(int index) { + return 0; + } + + /** + * Returns the value at the specified position in the list. + * + * @param index index of the value to return + * @return the value at the specified position in this list + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int get(int index) { + return 0; + } + + /** + * Returns true if this list contains the specified value. + * + * @param value value whose presence in this list is to be searched for + * @return true if this list contains the specified value + */ + @Override + public boolean contains(int value) { + return false; + } + + /** + * Returns the index of the first occurrence of the specified value + * in this list, or -1 if this list does not contain the value. + * + * @param value value to search for + * @return the index of the first occurrence of the specified value in this list + * or -1 if this list does not contain the value + */ + @Override + public int indexOf(int value) { + return 0; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Returns the number of values in this list. + * + * @return the number of values in this list + */ + @Override + public int size() { + return 0; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() { + + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } +} From 8d4c545736bf1501d2356a1e815ec97045ee6a40 Mon Sep 17 00:00:00 2001 From: tobygoetz Date: Tue, 16 Jan 2024 14:38:08 -0800 Subject: [PATCH 06/17] Started DoublyLinkedIntList w/ fields, constructor and created addBack() and removeBack() --- src/LinkedIntList.java | 181 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 src/LinkedIntList.java diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java new file mode 100644 index 0000000..18408ab --- /dev/null +++ b/src/LinkedIntList.java @@ -0,0 +1,181 @@ +import java.util.Iterator; + +public class LinkedIntList implements IntList { + + // define what a node is + private class Node { + + int data; + Node next; + } + + // set up the head + private Node head; + + // set up the size field + private int size; + + // ad a constructor to initialize the fields + public LinkedIntList() { + head = null; + size = 0; + } + + + /** + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) { + // set up a new node + Node theNewOne = new Node(); + + if (head == null) { + // the list is currently empty + head = theNewOne; + size++; + } else { + // the list cureently has some nodes in it + theNewOne.next = head; + head = theNewOne; + + } + + } + + /** + * Appends (inserts) the specified value at the back of the list (at index size()-1). + * + * @param value value to be inserted + */ + @Override + public void addBack(int value) { + + } + + /** + * Inserts the specified value at the specified position in this list. + * Shifts the value currently at that position (if any) and any subsequent + * values to the right. + * + * @param index index at which the specified value is to be inserted + * @param value value to be inserted + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public void add(int index, int value) { + + } + + /** + * Removes the value located at the front of the list + * (at index 0), if it is present. + * Shifts any subsequent values to the left. + */ + @Override + public void removeFront() { + + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() { + + } + + /** + * Removes the value at the specified position in this list. + * Shifts any subsequent values to the left. Returns the value + * that was removed from the list. + * + * @param index the index of the value to be removed + * @return the value previously at the specified position + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int remove(int index) { + return 0; + } + + /** + * Returns the value at the specified position in the list. + * + * @param index index of the value to return + * @return the value at the specified position in this list + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int get(int index) { + return 0; + } + + /** + * Returns true if this list contains the specified value. + * + * @param value value whose presence in this list is to be searched for + * @return true if this list contains the specified value + */ + @Override + public boolean contains(int value) { + return false; + } + + /** + * Returns the index of the first occurrence of the specified value + * in this list, or -1 if this list does not contain the value. + * + * @param value value to search for + * @return the index of the first occurrence of the specified value in this list + * or -1 if this list does not contain the value + */ + @Override + public int indexOf(int value) { + return 0; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Returns the number of values in this list. + * + * @return the number of values in this list + */ + @Override + public int size() { + return 0; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() { + + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } +} From 3719c9e9b4fd31465536c4c62f7ff43885c06856 Mon Sep 17 00:00:00 2001 From: tobygoetz Date: Tue, 16 Jan 2024 17:05:53 -0800 Subject: [PATCH 07/17] Reorganized project structure --- .idea/uiDesigner.xml | 124 +++++++++++++++++++++++ src/{ => Interfaces}/IntList.java | 4 +- src/{ => Lists}/ArrayIntList.java | 9 +- src/{ => Lists}/DoublyLinkedIntList.java | 29 +++--- src/{ => Lists}/LinkedIntList.java | 3 + src/driver/Driver.java | 16 +++ src/{ => driver}/Main.java | 5 + 7 files changed, 174 insertions(+), 16 deletions(-) create mode 100644 .idea/uiDesigner.xml rename src/{ => Interfaces}/IntList.java (97%) rename src/{ => Lists}/ArrayIntList.java (97%) rename src/{ => Lists}/DoublyLinkedIntList.java (90%) rename src/{ => Lists}/LinkedIntList.java (99%) create mode 100644 src/driver/Driver.java rename src/{ => driver}/Main.java (94%) diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/IntList.java b/src/Interfaces/IntList.java similarity index 97% rename from src/IntList.java rename to src/Interfaces/IntList.java index 398c27b..7b352f8 100644 --- a/src/IntList.java +++ b/src/Interfaces/IntList.java @@ -1,5 +1,7 @@ +package Interfaces; + /** - * The IntList interface defines a set of operations + * The Interfaces.IntList interface defines a set of operations * for an ordered (indexed) collection of ints, which * in mathematics is known as a sequence. */ diff --git a/src/ArrayIntList.java b/src/Lists/ArrayIntList.java similarity index 97% rename from src/ArrayIntList.java rename to src/Lists/ArrayIntList.java index 1f30df6..95b42d3 100644 --- a/src/ArrayIntList.java +++ b/src/Lists/ArrayIntList.java @@ -1,6 +1,11 @@ +package Lists; + import java.util.Iterator; +import java.util.NoSuchElementException; + +import Interfaces.IntList; -public class ArrayIntList implements IntList{ +public class ArrayIntList implements IntList { // fields: private int size; @@ -218,7 +223,7 @@ private void resize(int newSize) { @Override public Iterator iterator() { - //iterators are what enables main/client to use a for-each lop on IntList + //iterators are what enables main/client to use a for-each lop on Interfaces.IntList return null; } diff --git a/src/DoublyLinkedIntList.java b/src/Lists/DoublyLinkedIntList.java similarity index 90% rename from src/DoublyLinkedIntList.java rename to src/Lists/DoublyLinkedIntList.java index 52bb853..3f294ed 100644 --- a/src/DoublyLinkedIntList.java +++ b/src/Lists/DoublyLinkedIntList.java @@ -1,26 +1,29 @@ +package Lists; + import java.util.Iterator; +import Interfaces.IntList; public class DoublyLinkedIntList implements IntList { // Fields - private Node pre; - private Node post; + private Node left; + private Node right; private int size; // Constructor public DoublyLinkedIntList() { // an empty list has two sentinel (dummy) nodes that serve as bookends - pre = new Node(); - post = new Node(); - pre.next = post; - post.prev = pre; + left = new Node(); + right = new Node(); + left.next = right; + right.prev = left; size = 0; } private class Node { int data; - Node next; // address of te node 'after' this one in line + Node next; // address of the node 'after' this one in line Node prev; // addres of the node 'before' this one in line public Node() { @@ -50,16 +53,16 @@ public void addFront(int value) { */ @Override public void addBack(int value) { - Node theLastNode = post.prev; + Node theLastNode = right.prev; // set up my new node and fill it out (data, prev, next) Node theNewNode = new Node(); theNewNode.data = value; - theNewNode.next = post; + theNewNode.next = right; theNewNode.prev = theLastNode; // go to the end of the list's sentinel and update it's prev - post.prev = theNewNode; + right.prev = theNewNode; // go to the node before the new one and update it's next theLastNode.next = theNewNode; @@ -100,10 +103,10 @@ public void removeFront() { public void removeBack() { if (size > 0) { // set up a temp variable for convenience - Node theOneToRemove = post.prev; + Node theOneToRemove = right.prev; - theOneToRemove.prev.next = post; - post.prev = theOneToRemove.prev; + theOneToRemove.prev.next = right; + right.prev = theOneToRemove.prev; // optional to clean up theOneToRemove.next = null; diff --git a/src/LinkedIntList.java b/src/Lists/LinkedIntList.java similarity index 99% rename from src/LinkedIntList.java rename to src/Lists/LinkedIntList.java index 18408ab..2997c0f 100644 --- a/src/LinkedIntList.java +++ b/src/Lists/LinkedIntList.java @@ -1,4 +1,7 @@ +package Lists; + import java.util.Iterator; +import Interfaces.IntList; public class LinkedIntList implements IntList { diff --git a/src/driver/Driver.java b/src/driver/Driver.java new file mode 100644 index 0000000..aced502 --- /dev/null +++ b/src/driver/Driver.java @@ -0,0 +1,16 @@ +package driver; + +import Lists.DoublyLinkedIntList; + + + +public class Driver { + + public static void main(String[] args) { + DoublyLinkedIntList firstList = new DoublyLinkedIntList(); + firstList.addBack(0); + + System.out.println(firstList); + + } +} diff --git a/src/Main.java b/src/driver/Main.java similarity index 94% rename from src/Main.java rename to src/driver/Main.java index 6667b1a..2177304 100644 --- a/src/Main.java +++ b/src/driver/Main.java @@ -1,3 +1,8 @@ +package driver; + +import Lists.ArrayIntList; +import Interfaces.IntList; + import java.util.Iterator; //TIP To Run code, press or From 1ba9bcf66a9bc2dee2388d14e9b41558f731c385 Mon Sep 17 00:00:00 2001 From: tobygoetz Date: Tue, 16 Jan 2024 17:32:13 -0800 Subject: [PATCH 08/17] Added printList() to DoublyLinkedIntList --- src/Lists/DoublyLinkedIntList.java | 18 ++++++++++++++++++ src/driver/Driver.java | 6 ++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Lists/DoublyLinkedIntList.java b/src/Lists/DoublyLinkedIntList.java index 3f294ed..c8cfb7d 100644 --- a/src/Lists/DoublyLinkedIntList.java +++ b/src/Lists/DoublyLinkedIntList.java @@ -205,4 +205,22 @@ public void clear() { public Iterator iterator() { return null; } + + /** + * Prints Entire list + */ + public void printList() + { + Node current = this.left.next; + + while (current.next != null) + { + if (current.next != right) { + System.out.print(current.data + " -> "); + } else { + System.out.print(current.data); + } + current = current.next; + } + } } diff --git a/src/driver/Driver.java b/src/driver/Driver.java index aced502..79e5711 100644 --- a/src/driver/Driver.java +++ b/src/driver/Driver.java @@ -8,9 +8,11 @@ public class Driver { public static void main(String[] args) { DoublyLinkedIntList firstList = new DoublyLinkedIntList(); - firstList.addBack(0); + firstList.addBack(1); + firstList.addBack(2); + firstList.addBack(3); - System.out.println(firstList); + firstList.printList(); } } From 8e704fef7a6f90c9a0162c4b5e43b291d50ee558 Mon Sep 17 00:00:00 2001 From: tobygoetz Date: Tue, 16 Jan 2024 19:02:24 -0800 Subject: [PATCH 09/17] Created add() to DoublyLinkedIntList --- src/Lists/DoublyLinkedIntList.java | 78 ++++++++++++++++++++++-------- src/driver/Driver.java | 10 ++++ 2 files changed, 68 insertions(+), 20 deletions(-) diff --git a/src/Lists/DoublyLinkedIntList.java b/src/Lists/DoublyLinkedIntList.java index c8cfb7d..b315e14 100644 --- a/src/Lists/DoublyLinkedIntList.java +++ b/src/Lists/DoublyLinkedIntList.java @@ -6,17 +6,17 @@ public class DoublyLinkedIntList implements IntList { // Fields - private Node left; - private Node right; + private Node front; + private Node back; private int size; // Constructor public DoublyLinkedIntList() { // an empty list has two sentinel (dummy) nodes that serve as bookends - left = new Node(); - right = new Node(); - left.next = right; - right.prev = left; + front = new Node(0); + back = new Node(0); + front.next = back; + back.prev = front; size = 0; } @@ -26,14 +26,13 @@ private class Node { Node next; // address of the node 'after' this one in line Node prev; // addres of the node 'before' this one in line - public Node() { + public Node(int dataValue) { + data = dataValue; next = null; prev = null; } } - - /** * Prepends (inserts) the specified value at the front of the list (at index 0). * Shifts the value currently at the front of the list (if any) and any @@ -44,6 +43,18 @@ public Node() { @Override public void addFront(int value) { + // Create new node with int value; + Node addedToFront = new Node(value); + + // Assign new Node prev and next locators + addedToFront.prev = front; + addedToFront.next = front.next; + + // Connect new Node to list + front.next.prev = addedToFront; + front.next = addedToFront; + + size++; } /** @@ -53,22 +64,20 @@ public void addFront(int value) { */ @Override public void addBack(int value) { - Node theLastNode = right.prev; + Node theLastNode = back.prev; // set up my new node and fill it out (data, prev, next) - Node theNewNode = new Node(); - theNewNode.data = value; - theNewNode.next = right; + Node theNewNode = new Node(value); + theNewNode.next = back; theNewNode.prev = theLastNode; // go to the end of the list's sentinel and update it's prev - right.prev = theNewNode; + back.prev = theNewNode; // go to the node before the new one and update it's next theLastNode.next = theNewNode; size++; - } /** @@ -83,6 +92,32 @@ public void addBack(int value) { @Override public void add(int index, int value) { + // Counter + int i = 0; + // Create new Node + Node nodeAtIndex = new Node(value); + Node current = front.next; + + if (i <= size) { + // Conditional: If index is size of List skip loop and add to back + if (index == size) { + this.addBack(value); + } else { + while (current != back) { + if (index == i) { + // Assign the new node's prev and next locators + nodeAtIndex.prev = current.prev; + nodeAtIndex.next = current; + // Connect new Node to list + current.prev.next = nodeAtIndex; + current.prev = nodeAtIndex; + } + i++; + current = current.next; + } + size++; + } + } } /** @@ -103,10 +138,10 @@ public void removeFront() { public void removeBack() { if (size > 0) { // set up a temp variable for convenience - Node theOneToRemove = right.prev; + Node theOneToRemove = back.prev; - theOneToRemove.prev.next = right; - right.prev = theOneToRemove.prev; + theOneToRemove.prev.next = back; + back.prev = theOneToRemove.prev; // optional to clean up theOneToRemove.next = null; @@ -211,16 +246,19 @@ public Iterator iterator() { */ public void printList() { - Node current = this.left.next; + Node current = this.front.next; + System.out.print("Size: " + size + " "); while (current.next != null) { - if (current.next != right) { + if (current.next != back) { System.out.print(current.data + " -> "); } else { System.out.print(current.data); } current = current.next; } + + System.out.println(); } } diff --git a/src/driver/Driver.java b/src/driver/Driver.java index 79e5711..5dbe00b 100644 --- a/src/driver/Driver.java +++ b/src/driver/Driver.java @@ -14,5 +14,15 @@ public static void main(String[] args) { firstList.printList(); + firstList.addFront(4); + firstList.printList(); + firstList.addFront(5); + firstList.printList(); + + firstList.add(5, 100); + firstList.printList(); + + + } } From 9237dae73ebad27f3893b47e753dd05f3d2cbc39 Mon Sep 17 00:00:00 2001 From: tobygoetz Date: Tue, 16 Jan 2024 21:09:51 -0800 Subject: [PATCH 10/17] Created removeFront() and remove() in DoublyLinkedIntList, updated package names, Override toStringMethods(), tested with Driver class --- src/driver/Driver.java | 39 ++++++++- src/driver/Main.java | 4 +- src/{Interfaces => interfaces}/IntList.java | 2 +- src/{Lists => lists}/ArrayIntList.java | 4 +- src/{Lists => lists}/DoublyLinkedIntList.java | 83 +++++++++++++++++-- src/{Lists => lists}/LinkedIntList.java | 4 +- 6 files changed, 122 insertions(+), 14 deletions(-) rename src/{Interfaces => interfaces}/IntList.java (99%) rename src/{Lists => lists}/ArrayIntList.java (99%) rename src/{Lists => lists}/DoublyLinkedIntList.java (73%) rename src/{Lists => lists}/LinkedIntList.java (99%) diff --git a/src/driver/Driver.java b/src/driver/Driver.java index 5dbe00b..4fd9127 100644 --- a/src/driver/Driver.java +++ b/src/driver/Driver.java @@ -1,11 +1,18 @@ package driver; -import Lists.DoublyLinkedIntList; - +import lists.DoublyLinkedIntList; +/** + * @author tobygoetz + * @version 1.0 + */ public class Driver { + /** + * Program to test DoublyLinkedIntList + * @param args command line arguments + */ public static void main(String[] args) { DoublyLinkedIntList firstList = new DoublyLinkedIntList(); firstList.addBack(1); @@ -22,7 +29,35 @@ public static void main(String[] args) { firstList.add(5, 100); firstList.printList(); + firstList.removeFront(); + firstList.printList(); + + firstList.removeFront(); + firstList.printList(); + + firstList.removeBack(); + firstList.printList(); + + DoublyLinkedIntList emptyList = new DoublyLinkedIntList(); + + emptyList.printList(); + emptyList.removeBack(); + emptyList.removeFront(); + + firstList.printList(); + firstList.addBack(4); + firstList.addBack(5); + firstList.addBack(6); + firstList.addBack(7); + firstList.addBack(8); + firstList.addBack(9); + firstList.addFront(0); + firstList.printList(); + int removedValue = firstList.remove(2); + firstList.printList(); + System.out.println(removedValue); + } } diff --git a/src/driver/Main.java b/src/driver/Main.java index 2177304..5a1cac4 100644 --- a/src/driver/Main.java +++ b/src/driver/Main.java @@ -1,7 +1,7 @@ package driver; -import Lists.ArrayIntList; -import Interfaces.IntList; +import lists.ArrayIntList; +import interfaces.IntList; import java.util.Iterator; diff --git a/src/Interfaces/IntList.java b/src/interfaces/IntList.java similarity index 99% rename from src/Interfaces/IntList.java rename to src/interfaces/IntList.java index 7b352f8..2d8e367 100644 --- a/src/Interfaces/IntList.java +++ b/src/interfaces/IntList.java @@ -1,4 +1,4 @@ -package Interfaces; +package interfaces; /** * The Interfaces.IntList interface defines a set of operations diff --git a/src/Lists/ArrayIntList.java b/src/lists/ArrayIntList.java similarity index 99% rename from src/Lists/ArrayIntList.java rename to src/lists/ArrayIntList.java index 95b42d3..0122f6e 100644 --- a/src/Lists/ArrayIntList.java +++ b/src/lists/ArrayIntList.java @@ -1,9 +1,9 @@ -package Lists; +package lists; import java.util.Iterator; import java.util.NoSuchElementException; -import Interfaces.IntList; +import interfaces.IntList; public class ArrayIntList implements IntList { diff --git a/src/Lists/DoublyLinkedIntList.java b/src/lists/DoublyLinkedIntList.java similarity index 73% rename from src/Lists/DoublyLinkedIntList.java rename to src/lists/DoublyLinkedIntList.java index b315e14..0647b21 100644 --- a/src/Lists/DoublyLinkedIntList.java +++ b/src/lists/DoublyLinkedIntList.java @@ -1,8 +1,14 @@ -package Lists; +package lists; import java.util.Iterator; -import Interfaces.IntList; - +import interfaces.IntList; + +/** + * Creates a doubly linked list and implements methods in IntList + * + * @author tobygoetz + * @version 1.0 + */ public class DoublyLinkedIntList implements IntList { // Fields @@ -10,7 +16,9 @@ public class DoublyLinkedIntList implements IntList { private Node back; private int size; - // Constructor + /** + * Constructor for DoublyLinkedIntList + */ public DoublyLinkedIntList() { // an empty list has two sentinel (dummy) nodes that serve as bookends front = new Node(0); @@ -31,6 +39,15 @@ public Node(int dataValue) { next = null; prev = null; } + + @Override + public String toString() { + return "Node{" + + "data=" + data + + ", next=" + next + + ", prev=" + prev + + '}'; + } } /** @@ -128,6 +145,14 @@ public void add(int index, int value) { @Override public void removeFront() { + if (size > 0) { + // Assign Front next locator to second Node + front.next = front.next.next; + // Assign the new first node's prev locator to front + front.next.prev = front; + // Decrement size + size--; + } } /** @@ -136,6 +161,7 @@ public void removeFront() { */ @Override public void removeBack() { + if (size > 0) { // set up a temp variable for convenience Node theOneToRemove = back.prev; @@ -163,7 +189,45 @@ public void removeBack() { */ @Override public int remove(int index) { - return 0; + // Counter + int i = 0; + // Position + Node current = front.next; + + if (index < size && index >= 0) { + // Conditional to skip looping if index is first node + if (index == 0) { + int removedValue = current.data; + removeFront(); + return removedValue; + // Conditional to skip looping if index is last node + } else if (index == size - 1) { + int removedValue = back.prev.data; + removeBack(); + return removedValue; + } else { + // Loop to find the node at index position + while (current != back) { + if (index == i) { + int removedValue = current.data; + current.prev.next = current.next; + current.next.prev = current.prev; + // Index found so skip looping + return removedValue; + } else { + current = current.next; + i++; + } + // Decrement size of list + } size--; + } + } else { + if (index < 0) { + throw new IndexOutOfBoundsException("Index must be 0 or greater..."); + } else { + throw new IndexOutOfBoundsException("List does not have enough indices..."); + } + } return 0; } /** @@ -261,4 +325,13 @@ public void printList() System.out.println(); } + + @Override + public String toString() { + return "DoublyLinkedIntList{" + + "front=" + front + + ", back=" + back + + ", size=" + size + + '}'; + } } diff --git a/src/Lists/LinkedIntList.java b/src/lists/LinkedIntList.java similarity index 99% rename from src/Lists/LinkedIntList.java rename to src/lists/LinkedIntList.java index 2997c0f..c0d141b 100644 --- a/src/Lists/LinkedIntList.java +++ b/src/lists/LinkedIntList.java @@ -1,7 +1,7 @@ -package Lists; +package lists; import java.util.Iterator; -import Interfaces.IntList; +import interfaces.IntList; public class LinkedIntList implements IntList { From 755d2d3d4d84127e1a848aa29bb50863ce9ad363 Mon Sep 17 00:00:00 2001 From: tobygoetz Date: Tue, 16 Jan 2024 22:03:42 -0800 Subject: [PATCH 11/17] Created get(), contains(), indexOf(), isEmpty(), size() and clear() in DoublyLinkedIntList --- src/driver/Driver.java | 26 ++++++++++- src/lists/DoublyLinkedIntList.java | 70 +++++++++++++++++++++++++++--- 2 files changed, 89 insertions(+), 7 deletions(-) diff --git a/src/driver/Driver.java b/src/driver/Driver.java index 4fd9127..c014eb2 100644 --- a/src/driver/Driver.java +++ b/src/driver/Driver.java @@ -58,6 +58,30 @@ public static void main(String[] args) { int removedValue = firstList.remove(2); firstList.printList(); System.out.println(removedValue); - + + firstList.printList(); + int valueAtIndex = firstList.get(8); + System.out.println(valueAtIndex); + + firstList.printList(); + System.out.println(firstList.contains(10)); + + firstList.printList(); + System.out.println(firstList.indexOf( 6)); + + System.out.println(firstList.isEmpty()); + System.out.println(emptyList.isEmpty()); + + System.out.println(firstList.size()); + System.out.println(emptyList.size()); + + System.out.println(firstList.isEmpty()); + + firstList.clear(); + firstList.printList(); + System.out.println(firstList.isEmpty()); + + + } } diff --git a/src/lists/DoublyLinkedIntList.java b/src/lists/DoublyLinkedIntList.java index 0647b21..ed212fc 100644 --- a/src/lists/DoublyLinkedIntList.java +++ b/src/lists/DoublyLinkedIntList.java @@ -199,11 +199,13 @@ public int remove(int index) { if (index == 0) { int removedValue = current.data; removeFront(); + size--; return removedValue; // Conditional to skip looping if index is last node } else if (index == size - 1) { int removedValue = back.prev.data; removeBack(); + size--; return removedValue; } else { // Loop to find the node at index position @@ -212,6 +214,7 @@ public int remove(int index) { int removedValue = current.data; current.prev.next = current.next; current.next.prev = current.prev; + size--; // Index found so skip looping return removedValue; } else { @@ -219,7 +222,8 @@ public int remove(int index) { i++; } // Decrement size of list - } size--; + } +// size--; } } else { if (index < 0) { @@ -239,7 +243,36 @@ public int remove(int index) { */ @Override public int get(int index) { - return 0; + // Counter + int i = 0; + // Position + Node current = front.next; + + //Skip looping if index is at front of list + if (index == 0) { + return current.data; + // Skip looping if index is at back of list + } else if (index == size - 1) { + return back.prev.data; + // If index is in acceptable range loop to find Node at index value + }else if (index < size && index >= 0){ + while (current != back) { + if (index == i) { + return current.data; + } else { + i++; + current = current.next; + } + } + return current.data; + // Throw IndexOutOfBoundsException if index is not in range + } else { + if (index < 0) { + throw new IndexOutOfBoundsException("Index must be 0 or greater..."); + } else { + throw new IndexOutOfBoundsException("List does not have enough indices..."); + } + } } /** @@ -250,7 +283,17 @@ public int get(int index) { */ @Override public boolean contains(int value) { - return false; + + // Position + Node current = front.next; + + while (current != back) { + if (current.data == value) { + return true; + } else { + current = current.next; + } + } return false; } /** @@ -263,7 +306,16 @@ public boolean contains(int value) { */ @Override public int indexOf(int value) { - return 0; + + int i = 0; + Node current = front.next; + + if (contains(value)) { + while (current.data != value) { + i++; + current = current.next; + } return i; + } return -1; } /** @@ -273,6 +325,10 @@ public int indexOf(int value) { */ @Override public boolean isEmpty() { + + if (front.next == back && back.prev == front) { + return true; + } return false; } @@ -283,7 +339,7 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + return this.size; } /** @@ -292,7 +348,9 @@ public int size() { */ @Override public void clear() { - + front.next = back; + back.prev = front; + size = 0; } /** From a9bc733c85491a23ac9aa1c75d1bb5decde4f9dd Mon Sep 17 00:00:00 2001 From: tobygoetz Date: Thu, 18 Jan 2024 21:25:45 -0800 Subject: [PATCH 12/17] Reworked addFront(), addBack() and add(). Changed int[] field to Integer[] so class will support 0 as an index value and intialize the array to default null values --- src/driver/Driver.java | 159 +++++++++++++++++++++--------------- src/lists/ArrayIntList.java | 98 +++++++++++++++++----- 2 files changed, 172 insertions(+), 85 deletions(-) diff --git a/src/driver/Driver.java b/src/driver/Driver.java index c014eb2..419535c 100644 --- a/src/driver/Driver.java +++ b/src/driver/Driver.java @@ -1,5 +1,6 @@ package driver; +import lists.ArrayIntList; import lists.DoublyLinkedIntList; @@ -14,72 +15,100 @@ public class Driver { * @param args command line arguments */ public static void main(String[] args) { - DoublyLinkedIntList firstList = new DoublyLinkedIntList(); - firstList.addBack(1); - firstList.addBack(2); - firstList.addBack(3); +// DoublyLinkedIntList firstList = new DoublyLinkedIntList(); +// firstList.addBack(1); +// firstList.addBack(2); +// firstList.addBack(3); +// +// firstList.printList(); +// +// firstList.addFront(4); +// firstList.printList(); +// firstList.addFront(5); +// firstList.printList(); +// +// firstList.add(5, 100); +// firstList.printList(); +// +// firstList.removeFront(); +// firstList.printList(); +// +// firstList.removeFront(); +// firstList.printList(); +// +// firstList.removeBack(); +// firstList.printList(); +// +// DoublyLinkedIntList emptyList = new DoublyLinkedIntList(); +// +// emptyList.printList(); +// emptyList.removeBack(); +// emptyList.removeFront(); +// +// firstList.printList(); +// firstList.addBack(4); +// firstList.addBack(5); +// firstList.addBack(6); +// firstList.addBack(7); +// firstList.addBack(8); +// firstList.addBack(9); +// firstList.addFront(0); +// firstList.printList(); +// +// +// int removedValue = firstList.remove(2); +// firstList.printList(); +// System.out.println(removedValue); +// +// firstList.printList(); +// int valueAtIndex = firstList.get(8); +// System.out.println(valueAtIndex); +// +// firstList.printList(); +// System.out.println(firstList.contains(10)); +// +// firstList.printList(); +// System.out.println(firstList.indexOf( 6)); +// +// System.out.println(firstList.isEmpty()); +// System.out.println(emptyList.isEmpty()); +// +// System.out.println(firstList.size()); +// System.out.println(emptyList.size()); +// +// System.out.println(firstList.isEmpty()); +// +// firstList.clear(); +// firstList.printList(); +// System.out.println(firstList.isEmpty()); + + ArrayIntList arrOne = new ArrayIntList(); + + System.out.println(arrOne); + + arrOne.addFront(3); + arrOne.addFront(2); + arrOne.addFront(1); + arrOne.addFront(0); + + arrOne.addBack(4); + arrOne.addBack(5); + arrOne.addBack(6); + arrOne.addBack(7); + arrOne.addBack(8); + arrOne.addBack(9); + arrOne.addBack(10); + + arrOne.add(5, 100); + arrOne.add(-1, 100); + + + + + + System.out.println(arrOne); + System.out.println(); - firstList.printList(); - - firstList.addFront(4); - firstList.printList(); - firstList.addFront(5); - firstList.printList(); - - firstList.add(5, 100); - firstList.printList(); - - firstList.removeFront(); - firstList.printList(); - - firstList.removeFront(); - firstList.printList(); - - firstList.removeBack(); - firstList.printList(); - - DoublyLinkedIntList emptyList = new DoublyLinkedIntList(); - - emptyList.printList(); - emptyList.removeBack(); - emptyList.removeFront(); - - firstList.printList(); - firstList.addBack(4); - firstList.addBack(5); - firstList.addBack(6); - firstList.addBack(7); - firstList.addBack(8); - firstList.addBack(9); - firstList.addFront(0); - firstList.printList(); - - - int removedValue = firstList.remove(2); - firstList.printList(); - System.out.println(removedValue); - - firstList.printList(); - int valueAtIndex = firstList.get(8); - System.out.println(valueAtIndex); - - firstList.printList(); - System.out.println(firstList.contains(10)); - - firstList.printList(); - System.out.println(firstList.indexOf( 6)); - - System.out.println(firstList.isEmpty()); - System.out.println(emptyList.isEmpty()); - - System.out.println(firstList.size()); - System.out.println(emptyList.size()); - - System.out.println(firstList.isEmpty()); - - firstList.clear(); - firstList.printList(); - System.out.println(firstList.isEmpty()); diff --git a/src/lists/ArrayIntList.java b/src/lists/ArrayIntList.java index 0122f6e..afff216 100644 --- a/src/lists/ArrayIntList.java +++ b/src/lists/ArrayIntList.java @@ -1,21 +1,36 @@ package lists; +import java.util.Arrays; import java.util.Iterator; import java.util.NoSuchElementException; import interfaces.IntList; +/** + * Class that instantiates an int[] and provides size field + * contains instance methods implemented from IntList interface + * to help with adding, removing, etc + * @author tobygoetz + * @author Ken Hang + * @version 1.0 + */ public class ArrayIntList implements IntList { // fields: private int size; - private int[] buffer; + private Integer[] buffer; + /** + * Constructor for ArrayIntList created a new + * ArrayIntList with a buffer of 10 + */ public ArrayIntList() { //initialize fields size = 0; - buffer = new int[10]; + //updated int[] to Integer[] so that 0 can be used in this list + buffer = new Integer[10]; } + /** * Prepends (inserts) the specified value at the front of the list (at index 0). * Shifts the value currently at the front of the list (if any) and any @@ -25,15 +40,17 @@ public ArrayIntList() { */ @Override public void addFront(int value) { - for (int i = size; i >= 0; i--) { - buffer[i] = buffer[i - 1]; - - } - - // put the value at the front of the array at position 0 + //loop while index is greater than zero + for (int i = size; i > 0; i--) { + //if buffer is at capacity increase buffer by one index + if (size == buffer.length) { + this.resize(buffer.length + 1); + } + //index at highest buffer gets shifted right + buffer[i] = buffer[i - 1]; + } buffer[0] = value; size++; - } /** @@ -43,12 +60,12 @@ public void addFront(int value) { */ @Override public void addBack(int value) { - //TODO: check to see if we are full - if so, we need to create a larger buffer + //if buffer is at capacity increase buffer by one index if ( size == buffer.length) { - resize(size * 2); + resize(size + 1); } - + //add value to size which is one index greater than last value buffer[size] = value; size++; @@ -65,9 +82,23 @@ public void addBack(int value) { */ @Override public void add(int index, int value) { - if ( size == buffer.length) { - resize(size * 2); + + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Specified Index Must Be " + + "In the Range of 0-" + size); + } else { + //loop while index is greater than index value specified + for (int i = size; i >= index; i--) { + //if buffer is at capacity increase buffer by one index + if (size == buffer.length) { + this.resize(buffer.length + 1); + } + //index at highest buffer gets shifted right + buffer[i] = buffer[i - 1]; + } } + buffer[index] = value; + size++; } @@ -78,7 +109,16 @@ public void add(int index, int value) { */ @Override public void removeFront() { + if (!isEmpty()) { + for (int i = 0; i <= size - 2; i++) { + buffer[i] = buffer[i + 1]; + } + + //Make sure the last value that was shifted left is set to zero + buffer[size - 1] = 0; + size--; + } } /** @@ -87,6 +127,10 @@ public void removeFront() { */ @Override public void removeBack() { + if (!isEmpty()) { + buffer[size - 1] = 0; + size--; + } } @@ -194,13 +238,13 @@ public void clear() { // // size = 0; - buffer = new int[10]; + buffer = new Integer[10]; size = 0; } private void resize(int newSize) { //create new space, separate from the old space (buffer) - int[] newBuffer = new int[newSize]; + Integer[] newBuffer = new Integer[newSize]; // copy everything over from buffer into newBuffer for (int i = 0; i < buffer.length; i++) { @@ -224,7 +268,7 @@ private void resize(int newSize) { public Iterator iterator() { //iterators are what enables main/client to use a for-each lop on Interfaces.IntList - return null; + return new IntListIterator(); } //create a private helper Iterator class @@ -258,14 +302,28 @@ public boolean hasNext() { @Override public Integer next() { //check to see if i is greater than size -// if ( i >= size) { -// throw new -// } + if ( i >= size) { + throw new NoSuchElementException("i is now out of bounds"); + } int currentValue = buffer[i]; i++; return currentValue; } + + @Override + public String toString() { + return "IntListIterator{" + + "i=" + i + + '}'; + } } + @Override + public String toString() { + return "ArrayIntList{" + + "size=" + size + + ", buffer=" + Arrays.toString(buffer) + + '}'; + } } From 881f678276247133083149c968b46a457bb66936 Mon Sep 17 00:00:00 2001 From: tobygoetz Date: Thu, 18 Jan 2024 22:00:23 -0800 Subject: [PATCH 13/17] Reworked addFront(), addBack() and add(). Changed int[] field to Integer[] so class will support 0 as an index value and intialize the array to default null values --- src/driver/Driver.java | 10 +++++- src/lists/ArrayIntList.java | 6 ++-- src/lists/DoublyLinkedIntList.java | 1 - src/lists/LinkedIntList.java | 52 +++++++++++++++++++++++++++++- 4 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/driver/Driver.java b/src/driver/Driver.java index 419535c..3f30333 100644 --- a/src/driver/Driver.java +++ b/src/driver/Driver.java @@ -100,7 +100,15 @@ public static void main(String[] args) { arrOne.addBack(10); arrOne.add(5, 100); - arrOne.add(-1, 100); + arrOne.add(0, 100); + arrOne.add(13, 100); + arrOne.add(0, 100); + + + + + + diff --git a/src/lists/ArrayIntList.java b/src/lists/ArrayIntList.java index afff216..87a7f54 100644 --- a/src/lists/ArrayIntList.java +++ b/src/lists/ArrayIntList.java @@ -94,7 +94,9 @@ public void add(int index, int value) { this.resize(buffer.length + 1); } //index at highest buffer gets shifted right - buffer[i] = buffer[i - 1]; + if (i != 0) { + buffer[i] = buffer[i - 1]; + } } } buffer[index] = value; @@ -115,7 +117,7 @@ public void removeFront() { } //Make sure the last value that was shifted left is set to zero - buffer[size - 1] = 0; + buffer[size - 1] = null; size--; } diff --git a/src/lists/DoublyLinkedIntList.java b/src/lists/DoublyLinkedIntList.java index ed212fc..64a280c 100644 --- a/src/lists/DoublyLinkedIntList.java +++ b/src/lists/DoublyLinkedIntList.java @@ -380,7 +380,6 @@ public void printList() } current = current.next; } - System.out.println(); } diff --git a/src/lists/LinkedIntList.java b/src/lists/LinkedIntList.java index c0d141b..0e93056 100644 --- a/src/lists/LinkedIntList.java +++ b/src/lists/LinkedIntList.java @@ -1,6 +1,8 @@ package lists; import java.util.Iterator; +import java.util.NoSuchElementException; + import interfaces.IntList; public class LinkedIntList implements IntList { @@ -179,6 +181,54 @@ public void clear() { */ @Override public Iterator iterator() { - return null; + return new SinglyLinkedIterator(); + } + + /** + * helper class/type that defines how the iterator works + */ + private class SinglyLinkedIterator implements Iterator { + + private Node current; + + public SinglyLinkedIterator() { + current = head; + } + + /** + * Returns {@code true} if the iteration has more elements. + * (In other words, returns {@code true} if {@link #next} would + * return an element rather than throwing an exception.) + * + * @return {@code true} if the iteration has more elements + */ + @Override + public boolean hasNext() { +/* if (current == null) { + return false; + } else { + return false; + }*/ + + // or.... + + return current != null; + } + + /** + * 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 Integer next() { + if (current == null) { + throw new NoSuchElementException("There is no next one to go to!"); + } + int dataValue = current.data; + current = current.next; + return dataValue; + } } } From 9bb831a5a8428f843cc89cdf3ee69950287ee1b2 Mon Sep 17 00:00:00 2001 From: tobygoetz Date: Sat, 20 Jan 2024 10:36:39 -0800 Subject: [PATCH 14/17] Revised project, Any advice would be helpful. Especially on LinkedIntList. --- src/driver/Driver.java | 143 ++++++++++++++++++++++++++++------- src/lists/ArrayIntList.java | 79 ++++++++++++------- src/lists/LinkedIntList.java | 99 +++++++++++++++++++----- 3 files changed, 251 insertions(+), 70 deletions(-) diff --git a/src/driver/Driver.java b/src/driver/Driver.java index 3f30333..dd1aaad 100644 --- a/src/driver/Driver.java +++ b/src/driver/Driver.java @@ -2,6 +2,7 @@ import lists.ArrayIntList; import lists.DoublyLinkedIntList; +import lists.LinkedIntList; /** @@ -82,40 +83,130 @@ public static void main(String[] args) { // firstList.printList(); // System.out.println(firstList.isEmpty()); - ArrayIntList arrOne = new ArrayIntList(); - - System.out.println(arrOne); - - arrOne.addFront(3); - arrOne.addFront(2); - arrOne.addFront(1); - arrOne.addFront(0); - - arrOne.addBack(4); - arrOne.addBack(5); - arrOne.addBack(6); - arrOne.addBack(7); - arrOne.addBack(8); - arrOne.addBack(9); - arrOne.addBack(10); - - arrOne.add(5, 100); - arrOne.add(0, 100); - arrOne.add(13, 100); - arrOne.add(0, 100); - - - +// ArrayIntList arrOne = new ArrayIntList(); +// +// System.out.println(arrOne); +// +// arrOne.addFront(3); +// arrOne.addFront(2); +// arrOne.addFront(1); +// arrOne.addFront(0); +// +// arrOne.addBack(4); +// arrOne.addBack(5); +// arrOne.addBack(6); +// arrOne.addBack(7); +// arrOne.addBack(8); +// arrOne.addBack(9); +// arrOne.addBack(10); +// +// arrOne.add(5, 100); +// arrOne.add(0, 100); +// arrOne.add(13, 100); +// arrOne.add(0, 100); +// +// +// System.out.println(arrOne); +// System.out.println(); +// +// +// +// arrOne.removeFront(); +// System.out.println(arrOne); +// arrOne.removeFront(); +// System.out.println(arrOne); +// arrOne.removeFront(); +// System.out.println(arrOne); +// arrOne.removeFront(); +// System.out.println(arrOne); +// arrOne.removeFront(); +// System.out.println(arrOne); +// arrOne.removeFront(); +// System.out.println(arrOne); +// arrOne.removeFront(); +// System.out.println(arrOne); +// arrOne.removeFront(); +// System.out.println(arrOne); +// +// arrOne.removeBack(); +// System.out.println(); +// System.out.println(arrOne); +// +// // remove back does not stop because isEmpty is not complete +// +// int removed = arrOne.remove(0); +// System.out.println(arrOne); +// System.out.println(removed); +// +// System.out.println(); +// System.out.println(arrOne); +// int valueAtIndex = arrOne.get(0); +// System.out.println(valueAtIndex); +// +// System.out.println(arrOne.indexOf(11)); +// +// System.out.println(arrOne.isEmpty()); +// ArrayIntList emptyArr = new ArrayIntList(); +// System.out.println(emptyArr.isEmpty()); +// +// System.out.println(); +// System.out.println(arrOne); +// arrOne.removeBack(); +// System.out.println(arrOne); +// arrOne.removeBack(); +// System.out.println(arrOne); +// arrOne.removeBack(); +// System.out.println(arrOne); +// arrOne.removeBack(); +// System.out.println(arrOne); +// arrOne.removeBack(); +// System.out.println(arrOne); +// arrOne.removeBack(); +// System.out.println(arrOne); +// arrOne.removeBack(); +// System.out.println(arrOne); + + +// int removed2 = arrOne.remove(2); +// System.out.println(arrOne); +// System.out.println(removed2); +// +// int removed3 = arrOne.remove(2); +// System.out.println(arrOne); +// System.out.println(removed3); +// +// int removed4 = arrOne.remove(2); +// System.out.println(arrOne); +// System.out.println(removed4); +// +// int removed5 = arrOne.remove(2); +// System.out.println(arrOne); +// System.out.println(removed5); + System.out.println(); + LinkedIntList linkedIntList = new LinkedIntList(); + System.out.println(linkedIntList); + linkedIntList.addBack(1); + System.out.println(linkedIntList); + linkedIntList.addBack(2); + System.out.println(linkedIntList); +// +// linkedIntList.addBack(2); +// System.out.println(linkedIntList); +// linkedIntList.addFront(3); +// System.out.println(linkedIntList); +//// +// linkedIntList.addFront(2); +// System.out.println(linkedIntList); +// linkedIntList.addFront(1); +// System.out.println(linkedIntList); - System.out.println(arrOne); - System.out.println(); diff --git a/src/lists/ArrayIntList.java b/src/lists/ArrayIntList.java index 87a7f54..08202b6 100644 --- a/src/lists/ArrayIntList.java +++ b/src/lists/ArrayIntList.java @@ -115,11 +115,15 @@ public void removeFront() { for (int i = 0; i <= size - 2; i++) { buffer[i] = buffer[i + 1]; } - - //Make sure the last value that was shifted left is set to zero - buffer[size - 1] = null; - size--; + + //Reduce buffer until original buffer size is reach + if (size >= 10) { + resize(size); + //after buffer becomes 10 set removed values back to null + } else { + buffer[size] = null; + } } } @@ -130,10 +134,9 @@ public void removeFront() { @Override public void removeBack() { if (!isEmpty()) { - buffer[size - 1] = 0; + buffer[size - 1] = null; size--; } - } /** @@ -154,18 +157,23 @@ public int remove(int index) { throw new IndexOutOfBoundsException("Index is higher than size"); } - // save a copy of the value to be removed so we can return it later + // save a copy of the value to be removed so that we can return it later int copyOfRemovedValue = buffer[index]; - //shift values to the left - for (int i = 0; i <= size - 1; i++) { - buffer[i] = buffer[i + 1]; - - } + // if index is last index with valid data, set data to null + if (index == size - 1) { + buffer[index] = null; + // shift all values over starting at index to be removed + } else { + for (int i = index; i < size - 1; i++) { + buffer[i] = buffer[i + 1]; + } + } - buffer[size - 1] = 0; size--; - + // set trailing index to null to account for reduced size + buffer[size] = null; + return copyOfRemovedValue; } @@ -178,8 +186,10 @@ public int remove(int index) { */ @Override public int get(int index) { - if (index > size) { - throw new IndexOutOfBoundsException("No Such Index Value..."); + + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Specified Index Must Be " + + "In the Range of 0-" + (size - 1)); } return buffer[index]; } @@ -192,6 +202,12 @@ public int get(int index) { */ @Override public boolean contains(int value) { + + for (int i = 0; i < size; i++) { + if (buffer[i] == value) { + return true; + } + } return false; } @@ -205,7 +221,12 @@ public boolean contains(int value) { */ @Override public int indexOf(int value) { - return 0; + + for (int i = 0; i < size; i++) { + if (buffer[i] == value) { + return i; + } + } return -1; } /** @@ -215,7 +236,11 @@ public int indexOf(int value) { */ @Override public boolean isEmpty() { - return false; + if (size == 0 && buffer[0] == null) { + return true; + } else { + return false; + } } /** @@ -225,7 +250,7 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + return size; } /** @@ -234,11 +259,6 @@ public int size() { */ @Override public void clear() { -// for (int i = 0; i < size; i++) { -// buffer[i] = 0; -// } -// -// size = 0; buffer = new Integer[10]; size = 0; @@ -249,9 +269,14 @@ private void resize(int newSize) { Integer[] newBuffer = new Integer[newSize]; // copy everything over from buffer into newBuffer - for (int i = 0; i < buffer.length; i++) { - newBuffer[i] = buffer[i]; - + if (newSize > buffer.length) { + for (int i = 0; i < buffer.length; i++) { + newBuffer[i] = buffer[i]; + } + } else { + for (int i = 0; i < newBuffer.length; i++) { + newBuffer[i] = buffer[i]; + } } // set the new space into buffer diff --git a/src/lists/LinkedIntList.java b/src/lists/LinkedIntList.java index 0e93056..ac63cfd 100644 --- a/src/lists/LinkedIntList.java +++ b/src/lists/LinkedIntList.java @@ -7,25 +7,32 @@ public class LinkedIntList implements IntList { - // define what a node is - private class Node { - - int data; - Node next; - } - - // set up the head + // Fields private Node head; - - // set up the size field private int size; - // ad a constructor to initialize the fields + // add a constructor to initialize the fields public LinkedIntList() { head = null; size = 0; } + // define what a node is + public class Node { + public Integer data; + public Node next; + + public Node(Integer data) { + this.data = data; + this.next = null; + } + + public Node(Integer data, Node next) { + this.data = data; + this.next = next; + } + } + /** * Prepends (inserts) the specified value at the front of the list (at index 0). @@ -36,17 +43,18 @@ public LinkedIntList() { */ @Override public void addFront(int value) { + // set up a new node - Node theNewOne = new Node(); + Node addedToFront = new Node(value); if (head == null) { // the list is currently empty - head = theNewOne; + head = addedToFront; size++; } else { - // the list cureently has some nodes in it - theNewOne.next = head; - head = theNewOne; + // the list currently has some nodes in it + addedToFront.next = head; + head = addedToFront; } @@ -60,6 +68,43 @@ public void addFront(int value) { @Override public void addBack(int value) { + Node addToBack = new Node(value); + + if (head == null) { + head = addToBack; + } else { + Node current = new Node(head.data, head.next); + + while (current.next != null) { + current = current.next; + } + +// for (int i = 0; i < size; i++) { +// current = current.next; +// } + current.next = addToBack; + head = current; + System.out.println(current); + + +// do { +// current = current.next; +// } while (current.next != null); + +// current.next = addToBack; + +// while (current.next != null) { +// current = current.next; +// if (current.next == null) { +// current.next = addToBack; +// } + +// } + } + + size++; + + } /** @@ -207,7 +252,7 @@ public boolean hasNext() { /* if (current == null) { return false; } else { - return false; + return true; }*/ // or.... @@ -231,4 +276,24 @@ public Integer next() { return dataValue; } } + + @Override + public String toString() { + String list = "LinkedIntList{size=" + size + ", list=["; + Node current = head; + + if (current == null) { + list += "]}"; + } else { + for (Node i = current; i != null; i = current.next) { + list += i.data; + + if (i.next != null) { + list += ", "; + } else { + list += "]}"; + } + } + } return list; + } } From 5d74be2e77fed6ffedff8656d208f06ead94da24 Mon Sep 17 00:00:00 2001 From: tobygoetz Date: Sun, 21 Jan 2024 23:55:55 -0800 Subject: [PATCH 15/17] Ready for review. I believe this is as far as I can get with this package because I am out of time. The list classes are complete and function properly. I tested as I went along with the driver class. I have never written a fully functioning jUnit test class and I am new to the framework. That was written for the ArrayIntList class. I don't think I will be able to get much more accomplished before 5pm tomorrow night. --- IntListReview.iml | 27 ++ src/driver/Driver.java | 118 ++++++- src/lists/ArrayIntList.java | 47 +-- src/lists/LinkedIntList.java | 230 +++++++++---- src/tests/tests/ArrayIntListTest.java | 329 ++++++++++++++++++ src/tests/tests/DoublyLinkedIntListTest.java | 17 + src/tests/tests/LinkedIntListTest.java | 330 +++++++++++++++++++ 7 files changed, 997 insertions(+), 101 deletions(-) create mode 100644 src/tests/tests/ArrayIntListTest.java create mode 100644 src/tests/tests/DoublyLinkedIntListTest.java create mode 100644 src/tests/tests/LinkedIntListTest.java diff --git a/IntListReview.iml b/IntListReview.iml index c90834f..2ce2ff0 100644 --- a/IntListReview.iml +++ b/IntListReview.iml @@ -4,8 +4,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/driver/Driver.java b/src/driver/Driver.java index dd1aaad..846dad7 100644 --- a/src/driver/Driver.java +++ b/src/driver/Driver.java @@ -82,7 +82,7 @@ public static void main(String[] args) { // firstList.clear(); // firstList.printList(); // System.out.println(firstList.isEmpty()); - +// // ArrayIntList arrOne = new ArrayIntList(); // // System.out.println(arrOne); @@ -140,7 +140,7 @@ public static void main(String[] args) { // // System.out.println(); // System.out.println(arrOne); -// int valueAtIndex = arrOne.get(0); +// valueAtIndex = arrOne.get(0); // System.out.println(valueAtIndex); // // System.out.println(arrOne.indexOf(11)); @@ -165,8 +165,8 @@ public static void main(String[] args) { // System.out.println(arrOne); // arrOne.removeBack(); // System.out.println(arrOne); - - +// +// // int removed2 = arrOne.remove(2); // System.out.println(arrOne); // System.out.println(removed2); @@ -182,27 +182,109 @@ public static void main(String[] args) { // int removed5 = arrOne.remove(2); // System.out.println(arrOne); // System.out.println(removed5); - - System.out.println(); - LinkedIntList linkedIntList = new LinkedIntList(); - - System.out.println(linkedIntList); - - linkedIntList.addBack(1); - System.out.println(linkedIntList); - linkedIntList.addBack(2); - System.out.println(linkedIntList); // -// linkedIntList.addBack(2); +// System.out.println(); +// LinkedIntList linkedIntList = new LinkedIntList(); +// +//// System.out.println(linkedIntList); +// +// linkedIntList.addFront(1); // System.out.println(linkedIntList); - -// linkedIntList.addFront(3); +// linkedIntList.addFront(2); // System.out.println(linkedIntList); //// -// linkedIntList.addFront(2); +// linkedIntList.addBack(2); +// System.out.println(linkedIntList); +// +// linkedIntList.addBack(3); +// System.out.println(linkedIntList); +// +// linkedIntList.removeFront(); +// System.out.println(linkedIntList); +// linkedIntList.removeFront(); // System.out.println(linkedIntList); +// linkedIntList.removeFront(); +// System.out.println(linkedIntList); +// //iterate to make sure last node is eliminated +// linkedIntList.removeFront(); +// System.out.println(linkedIntList); +// //iterate to make sure last node is eliminated +// linkedIntList.removeFront(); +// System.out.println(linkedIntList); +// +// LinkedIntList list = new LinkedIntList(); +// linkedIntList.addFront(3); +// linkedIntList.addFront(2); // linkedIntList.addFront(1); // System.out.println(linkedIntList); +// +// System.out.println(linkedIntList.get(2)); +// +// +// System.out.println(); +// System.out.println(linkedIntList); +// System.out.println(linkedIntList.remove(1)); +// +// System.out.println(linkedIntList); +// linkedIntList.add(1,4); +// System.out.println(linkedIntList); +// +// +// System.out.println(linkedIntList.contains(0)); +// System.out.println(linkedIntList.isEmpty()); +// System.out.println(linkedIntList); +// System.out.println(linkedIntList.indexOf(0)); +// System.out.println(linkedIntList.size()); +// linkedIntList.clear(); +// System.out.println(linkedIntList); +// System.out.println(linkedIntList.size()); +// +// System.out.println(linkedIntList); +// +// +// +// System.out.println(linkedIntList); +// linkedIntList.removeBack(); +// System.out.println(linkedIntList); +// linkedIntList.removeBack(); +// System.out.println(linkedIntList); +// linkedIntList.removeBack(); +// System.out.println(linkedIntList); +// linkedIntList.removeBack(); +// System.out.println(linkedIntList); +// linkedIntList.removeBack(); +// System.out.println(linkedIntList); +// +// ArrayIntList array = new ArrayIntList(); +// array.addFront(3); +// array.addFront(2); +// array.addFront(1); +// System.out.println(array); +// +// System.out.println(array.get(0)); +// array.addFront(0); +// System.out.println(array); +// +// array.add(-1, 1000); + + LinkedIntList list = new LinkedIntList(); + System.out.println(list); + list.addFront(3); + list.addFront(2); + list.addFront(1); + System.out.println(list); + + list.addBack(4); + list.addBack(5); + list.addBack(6); + System.out.println(list); + + list.clear(); + System.out.println(list); + + + + diff --git a/src/lists/ArrayIntList.java b/src/lists/ArrayIntList.java index 08202b6..0e63296 100644 --- a/src/lists/ArrayIntList.java +++ b/src/lists/ArrayIntList.java @@ -168,9 +168,7 @@ public int remove(int index) { for (int i = index; i < size - 1; i++) { buffer[i] = buffer[i + 1]; } - } - - size--; + } size--; // set trailing index to null to account for reduced size buffer[size] = null; @@ -187,11 +185,19 @@ public int remove(int index) { @Override public int get(int index) { - if (index < 0 || index >= size) { - throw new IndexOutOfBoundsException("Specified Index Must Be " + - "In the Range of 0-" + (size - 1)); - } - return buffer[index]; + if (index < 0 ) { + throw new IndexOutOfBoundsException( + "Index must be greater than 0"); + } else if (index >= size ) { + if (size == 0) { + throw new IndexOutOfBoundsException( + "This list is empty"); + } else { + throw new IndexOutOfBoundsException( + "Specified Index Must Be " + + "In the Range of 0-" + (size - 1)); + } + } return buffer[index]; } /** @@ -236,11 +242,7 @@ public int indexOf(int value) { */ @Override public boolean isEmpty() { - if (size == 0 && buffer[0] == null) { - return true; - } else { - return false; - } + return size == 0 && buffer[0] == null; } /** @@ -264,6 +266,11 @@ public void clear() { size = 0; } + /** + * Helper method to resize ArrayIntlist to support + * more data + * @param newSize the new size of the internal Array + */ private void resize(int newSize) { //create new space, separate from the old space (buffer) Integer[] newBuffer = new Integer[newSize]; @@ -302,10 +309,10 @@ public Iterator iterator() { private class IntListIterator implements Iterator { // private fields: - private int i; + private int index; private IntListIterator() { - i = 0; + index = 0; } /** @@ -317,7 +324,7 @@ private IntListIterator() { */ @Override public boolean hasNext() { - return i < size; + return index < size; } /** @@ -329,19 +336,19 @@ public boolean hasNext() { @Override public Integer next() { //check to see if i is greater than size - if ( i >= size) { + if ( index >= size) { throw new NoSuchElementException("i is now out of bounds"); } - int currentValue = buffer[i]; - i++; + int currentValue = buffer[index]; + index++; return currentValue; } @Override public String toString() { return "IntListIterator{" + - "i=" + i + + "i=" + index + '}'; } } diff --git a/src/lists/LinkedIntList.java b/src/lists/LinkedIntList.java index ac63cfd..2aefddd 100644 --- a/src/lists/LinkedIntList.java +++ b/src/lists/LinkedIntList.java @@ -5,34 +5,56 @@ import interfaces.IntList; +/** + * Class that instantiates a Linked List for Integer types + * + * @author tobygoetz + * @version 1.0 + */ public class LinkedIntList implements IntList { // Fields private Node head; private int size; - // add a constructor to initialize the fields + /** + * Constructor to initialize the fields of LinkedIntList + */ public LinkedIntList() { head = null; size = 0; } - // define what a node is - public class Node { - public Integer data; - public Node next; + // Node Class + private class Node { + private Integer data; + private Node next; - public Node(Integer data) { + /** + * Constutor for Node that accepts on Integer data + * and sets the next to null + * @param data Integer value of Node + */ + private Node(Integer data) { this.data = data; this.next = null; } - public Node(Integer data, Node next) { + /** + * Constutor for Node that accepts on Integer data + * and sets the next to null + * @param data Integer Value of Node + * @param next Points to the next Node in list + */ + private Node(Integer data, Node next) { this.data = data; this.next = next; } - } + public String toString() { + return data + " -> "; + } + } /** * Prepends (inserts) the specified value at the front of the list (at index 0). @@ -43,21 +65,17 @@ public Node(Integer data, Node next) { */ @Override public void addFront(int value) { - - // set up a new node + // new Node to be added Node addedToFront = new Node(value); if (head == null) { // the list is currently empty head = addedToFront; - size++; } else { // the list currently has some nodes in it addedToFront.next = head; head = addedToFront; - - } - + } size++; } /** @@ -73,38 +91,13 @@ public void addBack(int value) { if (head == null) { head = addToBack; } else { - Node current = new Node(head.data, head.next); + Node current = head; while (current.next != null) { current = current.next; } - -// for (int i = 0; i < size; i++) { -// current = current.next; -// } current.next = addToBack; - head = current; - System.out.println(current); - - -// do { -// current = current.next; -// } while (current.next != null); - -// current.next = addToBack; - -// while (current.next != null) { -// current = current.next; -// if (current.next == null) { -// current.next = addToBack; -// } - -// } - } - - size++; - - + } size++; } /** @@ -118,7 +111,30 @@ public void addBack(int value) { */ @Override public void add(int index, int value) { - + int dex = 1; + //if requested index is out of range throw exception + if (index < 0 || index > (size )) { + throw new IndexOutOfBoundsException( + "Index must be in the Range 0-" + (size)); + //else find Node at index + } else { + //check if index is head + if (index == 0) { + addFront(value); + //check if index is at the end + } else if (index == (size)) { + addBack(value); + //remove everywhere else + } else { + Node current = head; + while (dex <= index - 1) { + current = current.next; + dex++; + } + current.next = new Node(value, current.next); + size++; + } + } } /** @@ -128,7 +144,15 @@ public void add(int index, int value) { */ @Override public void removeFront() { - + if (head != null) { + if (head.next != null) { + head = head.next; + size--; + } else { + head = null; + size--; + } + } } /** @@ -138,6 +162,20 @@ public void removeFront() { @Override public void removeBack() { + if (head != null) { + Node current = head; + + if (current.next != null) { + while (current.next.next != null) { + current = current.next; + } + current.next = null; + size--; + } else { + head = null; + size = 0; + } + } } /** @@ -151,7 +189,32 @@ public void removeBack() { */ @Override public int remove(int index) { - return 0; + int dex = 1; + int removedValue = get(index); + + //if requested index is out of range throw exception + if (index < 0 || index > (size - 1)) { + throw new IndexOutOfBoundsException( + "Index must be in the Range 0-" + (size - 1)); + //else find Node at index + } else { + //check if index is head + if (index == 0) { + removeFront(); + //check if index is at the end + } else if (index == (size - 1)) { + removeBack(); + //remove everywhere else + } else { + Node current = head; + while (dex <= index - 1) { + current = current.next; + dex++; + } + current.next = current.next.next; + size--; + } return removedValue; + } } /** @@ -163,7 +226,26 @@ public int remove(int index) { */ @Override public int get(int index) { - return 0; + int dex = 1; + + //if requested index is out of range throw exception + if (index < 0 || index > (size - 1)) { + throw new IndexOutOfBoundsException( + "Index must be in the Range 0-" + (size - 1)); + //else find Node at index + } else { + //head is always at index 0 + if (index == 0) { + return head.data; + } else { + Node current = head; + while(dex <= index) { + current = current.next; + dex++; + } + return dex; + } + } } /** @@ -174,7 +256,20 @@ public int get(int index) { */ @Override public boolean contains(int value) { - return false; + + if (head != null) { + Node current = head; + if (current.data == value) { + return true; + } else { + while (current.next != null) { + current = current.next; + if (current.data == value) { + return true; + } + } + } + } return false; } /** @@ -187,7 +282,16 @@ public boolean contains(int value) { */ @Override public int indexOf(int value) { - return 0; + if (!isEmpty()) { + Node current = head; + int index = 0; + if (contains(value)) { + while(current.data != value) { + current = current.next; + index++; + } return index; + } + } return -1; } /** @@ -197,7 +301,7 @@ public int indexOf(int value) { */ @Override public boolean isEmpty() { - return false; + return size == 0; } /** @@ -207,7 +311,7 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + return size; } /** @@ -216,7 +320,8 @@ public int size() { */ @Override public void clear() { - + head = null; + size = 0; } /** @@ -249,14 +354,6 @@ public SinglyLinkedIterator() { */ @Override public boolean hasNext() { -/* if (current == null) { - return false; - } else { - return true; - }*/ - - // or.... - return current != null; } @@ -275,24 +372,31 @@ public Integer next() { current = current.next; return dataValue; } + + @Override + public String toString() { + return "SinglyLinkedIterator{" + + "Node=" + current + + '}'; + } } @Override public String toString() { - String list = "LinkedIntList{size=" + size + ", list=["; Node current = head; + String list = "LinkedIntList{size=" + size + ", list=["; if (current == null) { list += "]}"; } else { - for (Node i = current; i != null; i = current.next) { - list += i.data; - - if (i.next != null) { + while (current != null) { + list += current.data; + if (current.next != null) { list += ", "; } else { list += "]}"; } + current = current.next; } } return list; } diff --git a/src/tests/tests/ArrayIntListTest.java b/src/tests/tests/ArrayIntListTest.java new file mode 100644 index 0000000..906201e --- /dev/null +++ b/src/tests/tests/ArrayIntListTest.java @@ -0,0 +1,329 @@ +package tests; +import lists.ArrayIntList; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +/** + * Test Class for ArrayIntList + * @author tobygoetz + * @version 1.0 + */ +public class ArrayIntListTest { + public ArrayIntList array = new ArrayIntList(); + public Exception exception; + public static final int ITERATIONS = 15; + + /** + * Test adds Integer values to the front when empty, almost empty, + * not empty and when buffer is larger than intial size of 10 is + * surpassed. + */ + @Test + public void addFrontTest() { + assertEquals(0, array.size()); + for (int i = 0; i <= ITERATIONS; i++) { + array.addFront(i); + // Index 0 changes everytime addFront is called + assertEquals(i, array.get(0)); + } + } + + /** + * Test adds Integer values to the back when empty, almost empty, + * not empty and when buffer is larger than intial size of 10 is + * surpassed. + */ + @Test + public void addBackTest() { + array.clear(); + assertEquals(0, array.size()); + for (int i = 0; i <= ITERATIONS; i++) { + array.addBack(i); + // Index 0 changes everytime addFront is called + assertEquals(i, array.get(array.size() - 1)); + } + } + + /** + * Test adds Integer values at specific index when empty, almost + * empty,not empty and when buffer is larger than intial size of + * 10 is surpassed. + */ + @Test + public void addTest() { + array.clear(); + assertEquals(0, array.size()); + for (int i = 0; i <= ITERATIONS; i++) { + array.add(i, i); + // Index at i incrementing + assertEquals(i, array.get(i)); + } + + for (int i = ITERATIONS; i >= 0; i--) { + array.add(i, i); + // Index at i decrementing + assertEquals(i, array.get(i)); + } + + //IndexOutOfBoundsException is thrown if -1 is called + exception = assertThrows( + IndexOutOfBoundsException.class, () -> { + array.add(-1, ITERATIONS); + }); + + //IndexOutOfBoundsException is thrown if index larger than + // the amount of indices is called + exception = assertThrows( + IndexOutOfBoundsException.class, () -> { + array.add(array.size() + 1, ITERATIONS); + }); + } + + /** + * Test removes Integer values from the front of ArrayIntList when + * empty, almost empty and not empty + */ + @Test + public void removeFrontTest() { + array.clear(); + assertEquals(0, array.size()); + + //test with empty array + array.removeFront(); + exception = assertThrows( + IndexOutOfBoundsException.class, () -> { + array.get(0); + }); + assertTrue(array.isEmpty()); + + //test with 1 value in array + array.addFront(ITERATIONS); + array.removeFront(); + assertTrue(array.isEmpty()); + + for (int i = 0; i <= ITERATIONS; i++) { + array.add(i, i); + } + for (int i = 0; i < ITERATIONS; i++) { + int removedValue = array.get(1); + array.removeFront(); + assertEquals(removedValue, array.get(0)); + } + } + + /** + * Test removes Integer values from the front of ArrayIntList when + * empty, almost empty and not empty + */ + @Test + public void removeBackTest() { + array.clear(); + assertEquals(0, array.size()); + + //test with empty array + array.removeBack(); + exception = assertThrows( + IndexOutOfBoundsException.class, () -> { + array.get(0); + }); + assertTrue(array.isEmpty()); + + //test with 1 value in array + array.addFront(ITERATIONS); + array.removeBack(); + assertTrue(array.isEmpty()); + + //Finish this for removed +// for (int i = 0; i <= ITERATIONS; i++) { +// array.add(i, i); +// } +// for (int i = 0; i < ITERATIONS; i++) { +// int removedValue = array.get(1); +// array.removeFront(); +// assertEquals(removedValue, array.get(0)); +// } + } + + /** + * Test removes Integer values at specific index when empty, almost + * empty,not empty and when buffer is larger than intial size of + * 10 is surpassed. + */ + @Test + public void removeTest() { + array.clear(); + assertEquals(0, array.size()); + + //test with empty array + exception = assertThrows( + IndexOutOfBoundsException.class, () -> { + array.get(0); + }); + assertTrue(array.isEmpty()); + + //test with index higher than size of array + exception = assertThrows( + IndexOutOfBoundsException.class, () -> { + array.get(array.size()); + }); + assertTrue(array.isEmpty()); + + //test with one value in array + array.addFront(0); + array.remove(0); + assertTrue(array.isEmpty()); + assertEquals(0, array.size()); + + for (int i = 0; i <= ITERATIONS; i++) { + array.add(i, i); + } + for (int i = 0; i < ITERATIONS; i++) { + int removedValue = array.get(1); + array.remove(0); + assertEquals(removedValue, array.get(0)); + } + } + + /** + * Test get method returns for empty, almost empty + * and exception throw due to Index out of bounds + */ + @Test + public void getTest() { + array.clear(); + assertEquals(0, array.size()); + + //test with empty array + exception = assertThrows( + IndexOutOfBoundsException.class, () -> { + array.get(0); + }); + assertTrue(array.isEmpty()); + + //test with index greater than size of array + //test with empty array + exception = assertThrows( + IndexOutOfBoundsException.class, () -> { + array.get(1); + }); + assertTrue(array.isEmpty()); + + //reassign values + this.fillArray(array); + + //test the return values of get() + for (int i = 0; i <= ITERATIONS; i++) { + int getValue = array.get(i); + assertEquals(getValue, array.get(i)); + } + } + + /** + * Test contains() for non-existent values, existing values, + * against empty list + */ + @Test + public void containsTest() { + array.clear(); + assertEquals(0, array.size()); + + //Test if empty + assertFalse(array.contains(ITERATIONS)); + + //Test if value 1 exists + array.add(0, 1); + assertTrue(array.contains(1)); + + //test if Iterations exists + this.fillArray(array); + assertTrue(array.contains(ITERATIONS)); + + //test if number does not exist + assertFalse(array.contains(80085)); + } + + /** + * Test IndexOf() for no values, some values, + * against empty list + */ + @Test + public void IndexOfTest() { + //saftey check + array.clear(); + assertEquals(0, array.size()); + + //test bounds of IndexOf() + assertEquals(-1, array.indexOf(-ITERATIONS)); + + //test if indices match value returns of all indices + System.out.println(array); + for (int i = 0; i < ITERATIONS; i++) { + array.addBack(i); + } + for (int i = 0; i < ITERATIONS; i++) { + array.addBack(i); + assertEquals(i, array.indexOf(i)); + } + } + + /** + * Test isEmpty() for no values, some values, + * against empty list + */ + @Test + public void isEmptyTest() { + //saftey check + array.clear(); + assertEquals(0, array.size()); + + //test against non-empty array + this.fillArray(array); + assertFalse(array.isEmpty()); + } + + /** + * Test size() for no values, some values, + * against empty list + */ + @Test + public void sizeTest() { + //saftey check + array.clear(); + assertEquals(0, array.size()); + + //test against non-empty array + for (int i = 0; i < ITERATIONS; i++) { + array.addBack(i); + assertEquals(i + 1, array.size()); + } + } + + /** + * Test clear() for no values, some values, + * against empty list + */ + @Test + public void clearTest() { + //saftey check + array.clear(); + assertEquals(0, array.size()); + + //test against non-empty array + this.fillArray(array); + array.clear(); + assertEquals(0, array.size()); + } + + /** + * Helper method to fill the array in this class + * @param arr Field array + */ + public void fillArray(ArrayIntList arr) { + //reassign values + for (int i = 0; i <= ITERATIONS; i++) { + array.addFront(i); + // Index 0 changes everytime addFront is called + assertEquals(i, array.get(0)); + } + } +} diff --git a/src/tests/tests/DoublyLinkedIntListTest.java b/src/tests/tests/DoublyLinkedIntListTest.java new file mode 100644 index 0000000..0a26037 --- /dev/null +++ b/src/tests/tests/DoublyLinkedIntListTest.java @@ -0,0 +1,17 @@ +package tests; +import lists.ArrayIntList; +import lists.LinkedIntList; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +/** + * Test Class for ArrayIntList + * @author tobygoetz + * @version 1.0 + */ +public class DoublyLinkedIntListTest { + public LinkedIntList list = new LinkedIntList(); + public Exception exception; + public static final int ITERATIONS = 15; + +} \ No newline at end of file diff --git a/src/tests/tests/LinkedIntListTest.java b/src/tests/tests/LinkedIntListTest.java new file mode 100644 index 0000000..532b4d9 --- /dev/null +++ b/src/tests/tests/LinkedIntListTest.java @@ -0,0 +1,330 @@ +package tests; +import lists.ArrayIntList; +import lists.LinkedIntList; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +/** + * Test Class for ArrayIntList + * @author tobygoetz + * @version 1.0 + */ +public class LinkedIntListTest { + public LinkedIntList list = new LinkedIntList(); + public Exception exception; + public static final int ITERATIONS = 15; + + /** + * Test adds Integer values to the front when empty, almost empty, + * not empty and when buffer is larger than intial size of 10 is + * surpassed. + */ + @Test + public void addFrontTest() { + assertEquals(0, list.size()); + for (int i = 0; i <= ITERATIONS; i++) { + list.addFront(i); + // Index 0 changes everytime addFront is called + assertEquals(i, list.get(0)); + } + } + + /** + * Test adds Integer values to the back when empty, almost empty, + * not empty and when buffer is larger than intial size of 10 is + * surpassed. + */ + @Test + public void addBackTest() { + list.clear(); + assertEquals(0, list.size()); + for (int i = 0; i <= ITERATIONS; i++) { + list.addBack(i); + // Index 0 changes everytime addFront is called + assertEquals(i, list.get(list.size() - 1)); + } + } + + /** + * Test adds Integer values at specific index when empty, almost + * empty,not empty and when buffer is larger than intial size of + * 10 is surpassed. + */ + @Test + public void addTest() { + list.clear(); + assertEquals(0, list.size()); + for (int i = 0; i <= ITERATIONS; i++) { + list.add(i, i); + // Index at i incrementing + assertEquals(i, list.get(i)); + } + + for (int i = ITERATIONS; i >= 0; i--) { + list.add(i, i); + // Index at i decrementing + assertEquals(i, list.get(i)); + } + + //IndexOutOfBoundsException is thrown if -1 is called + exception = assertThrows( + IndexOutOfBoundsException.class, () -> { + list.add(-1, ITERATIONS); + }); + + //IndexOutOfBoundsException is thrown if index larger than + // the amount of indices is called + exception = assertThrows( + IndexOutOfBoundsException.class, () -> { + list.add(list.size() + 1, ITERATIONS); + }); + } + + /** + * Test removes Integer values from the front of ArrayIntList when + * empty, almost empty and not empty + */ + @Test + public void removeFrontTest() { + list.clear(); + assertEquals(0, list.size()); + + //test with empty array + list.removeFront(); + exception = assertThrows( + IndexOutOfBoundsException.class, () -> { + list.get(0); + }); + assertTrue(list.isEmpty()); + + //test with 1 value in array + list.addFront(ITERATIONS); + list.removeFront(); + assertTrue(list.isEmpty()); + + for (int i = 0; i <= ITERATIONS; i++) { + list.add(i, i); + } + for (int i = 0; i < ITERATIONS; i++) { + int removedValue = list.get(1); + list.removeFront(); + assertEquals(removedValue, list.get(0)); + } + } + + /** + * Test removes Integer values from the front of ArrayIntList when + * empty, almost empty and not empty + */ + @Test + public void removeBackTest() { + list.clear(); + assertEquals(0, list.size()); + + //test with empty array + list.removeBack(); + exception = assertThrows( + IndexOutOfBoundsException.class, () -> { + list.get(0); + }); + assertTrue(list.isEmpty()); + + //test with 1 value in array + list.addFront(ITERATIONS); + list.removeBack(); + assertTrue(list.isEmpty()); + + //Finish this for removed +// for (int i = 0; i <= ITERATIONS; i++) { +// array.add(i, i); +// } +// for (int i = 0; i < ITERATIONS; i++) { +// int removedValue = array.get(1); +// array.removeFront(); +// assertEquals(removedValue, array.get(0)); +// } + } + + /** + * Test removes Integer values at specific index when empty, almost + * empty,not empty and when buffer is larger than intial size of + * 10 is surpassed. + */ + @Test + public void removeTest() { + list.clear(); + assertEquals(0, list.size()); + + //test with empty array + exception = assertThrows( + IndexOutOfBoundsException.class, () -> { + list.get(0); + }); + assertTrue(list.isEmpty()); + + //test with index higher than size of array + exception = assertThrows( + IndexOutOfBoundsException.class, () -> { + list.get(list.size()); + }); + assertTrue(list.isEmpty()); + + //test with one value in array + list.addFront(0); + list.remove(0); + assertTrue(list.isEmpty()); + assertEquals(0, list.size()); + + for (int i = 0; i <= ITERATIONS; i++) { + list.add(i, i); + } + for (int i = 0; i < ITERATIONS; i++) { + int removedValue = list.get(1); + list.remove(0); + assertEquals(removedValue, list.get(0)); + } + } + + /** + * Test get method returns for empty, almost empty + * and exception throw due to Index out of bounds + */ + @Test + public void getTest() { + list.clear(); + assertEquals(0, list.size()); + + //test with empty array + exception = assertThrows( + IndexOutOfBoundsException.class, () -> { + list.get(0); + }); + assertTrue(list.isEmpty()); + + //test with index greater than size of array + //test with empty array + exception = assertThrows( + IndexOutOfBoundsException.class, () -> { + list.get(1); + }); + assertTrue(list.isEmpty()); + + //reassign values + this.fillArray(list); + + //test the return values of get() + for (int i = 0; i <= ITERATIONS; i++) { + int getValue = list.get(i); + assertEquals(getValue, list.get(i)); + } + } + + /** + * Test contains() for non-existent values, existing values, + * against empty list + */ + @Test + public void containsTest() { + list.clear(); + assertEquals(0, list.size()); + + //Test if empty + assertFalse(list.contains(ITERATIONS)); + + //Test if value 1 exists + list.add(0, 1); + assertTrue(list.contains(1)); + + //test if Iterations exists + this.fillArray(list); + assertTrue(list.contains(ITERATIONS)); + + //test if number does not exist + assertFalse(list.contains(80085)); + } + + /** + * Test IndexOf() for no values, some values, + * against empty list + */ + @Test + public void IndexOfTest() { + //saftey check + list.clear(); + assertEquals(0, list.size()); + + //test bounds of IndexOf() + assertEquals(-1, list.indexOf(-ITERATIONS)); + + //test if indices match value returns of all indices + System.out.println(list); + for (int i = 0; i < ITERATIONS; i++) { + list.addBack(i); + } + for (int i = 0; i < ITERATIONS; i++) { + list.addBack(i); + assertEquals(i, list.indexOf(i)); + } + } + + /** + * Test isEmpty() for no values, some values, + * against empty list + */ + @Test + public void isEmptyTest() { + //saftey check + list.clear(); + assertEquals(0, list.size()); + + //test against non-empty array + this.fillArray(list); + assertFalse(list.isEmpty()); + } + + /** + * Test size() for no values, some values, + * against empty list + */ + @Test + public void sizeTest() { + //saftey check + list.clear(); + assertEquals(0, list.size()); + + //test against non-empty array + for (int i = 0; i < ITERATIONS; i++) { + list.addBack(i); + assertEquals(i + 1, list.size()); + } + } + + /** + * Test clear() for no values, some values, + * against empty list + */ + @Test + public void clearTest() { + //saftey check + list.clear(); + assertEquals(0, list.size()); + + //test against non-empty array + this.fillArray(list); + list.clear(); + assertEquals(0, list.size()); + } + + /** + * Helper method to fill the array in this class + * @param arr Field array + */ + public void fillArray(LinkedIntList arr) { + //reassign values + for (int i = 0; i <= ITERATIONS; i++) { + list.addFront(i); + // Index 0 changes everytime addFront is called + assertEquals(i, list.get(0)); + } + } +} From 280b06a4ffdfd1d9074637d693e2325f386016e1 Mon Sep 17 00:00:00 2001 From: tobygoetz Date: Fri, 26 Jan 2024 22:12:59 -0800 Subject: [PATCH 16/17] Ready for review. Finished jUnit Tests LinkedIntList and correct one error in LinkedIntList get(). --- src/driver/Driver.java | 8 ++ src/lists/LinkedIntList.java | 22 ++--- src/tests/tests/LinkedIntListTest.java | 112 ++++++++++++------------- 3 files changed, 70 insertions(+), 72 deletions(-) diff --git a/src/driver/Driver.java b/src/driver/Driver.java index 846dad7..9f5a292 100644 --- a/src/driver/Driver.java +++ b/src/driver/Driver.java @@ -279,8 +279,16 @@ public static void main(String[] args) { list.addBack(6); System.out.println(list); + System.out.println(list.get(0)); + list.addFront(100); + System.out.println(list); list.clear(); System.out.println(list); + list.addFront(100); + list.addFront(200); + list.addFront(300); + + System.out.println(list); diff --git a/src/lists/LinkedIntList.java b/src/lists/LinkedIntList.java index 2aefddd..02cc00b 100644 --- a/src/lists/LinkedIntList.java +++ b/src/lists/LinkedIntList.java @@ -31,7 +31,7 @@ private class Node { private Node next; /** - * Constutor for Node that accepts on Integer data + * Constuctor for Node that accepts on Integer data * and sets the next to null * @param data Integer value of Node */ @@ -41,7 +41,7 @@ private Node(Integer data) { } /** - * Constutor for Node that accepts on Integer data + * Constuctor for Node that accepts on Integer data * and sets the next to null * @param data Integer Value of Node * @param next Points to the next Node in list @@ -67,15 +67,12 @@ public String toString() { public void addFront(int value) { // new Node to be added Node addedToFront = new Node(value); - - if (head == null) { - // the list is currently empty - head = addedToFront; - } else { - // the list currently has some nodes in it + // the list currently has some nodes in it + if (head != null) { addedToFront.next = head; - head = addedToFront; - } size++; + } + head = addedToFront; + size++; } /** @@ -147,11 +144,10 @@ public void removeFront() { if (head != null) { if (head.next != null) { head = head.next; - size--; } else { head = null; - size--; } + size--; } } @@ -243,7 +239,7 @@ public int get(int index) { current = current.next; dex++; } - return dex; + return current.data; } } } diff --git a/src/tests/tests/LinkedIntListTest.java b/src/tests/tests/LinkedIntListTest.java index 532b4d9..1cdb277 100644 --- a/src/tests/tests/LinkedIntListTest.java +++ b/src/tests/tests/LinkedIntListTest.java @@ -1,5 +1,4 @@ package tests; -import lists.ArrayIntList; import lists.LinkedIntList; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -10,10 +9,19 @@ * @version 1.0 */ public class LinkedIntListTest { - public LinkedIntList list = new LinkedIntList(); - public Exception exception; + private LinkedIntList list = new LinkedIntList(); + private Exception exception; public static final int ITERATIONS = 15; + + protected Exception getException() { + return exception; + } + + protected void setException(Exception exception) { + this.exception = exception; + } + /** * Test adds Integer values to the front when empty, almost empty, * not empty and when buffer is larger than intial size of 10 is @@ -67,17 +75,13 @@ public void addTest() { } //IndexOutOfBoundsException is thrown if -1 is called - exception = assertThrows( - IndexOutOfBoundsException.class, () -> { - list.add(-1, ITERATIONS); - }); + setException(assertThrows( + IndexOutOfBoundsException.class, () -> list.add(-1, ITERATIONS))); //IndexOutOfBoundsException is thrown if index larger than // the amount of indices is called - exception = assertThrows( - IndexOutOfBoundsException.class, () -> { - list.add(list.size() + 1, ITERATIONS); - }); + setException(assertThrows( + IndexOutOfBoundsException.class, () -> list.add(list.size() + 1, ITERATIONS))); } /** @@ -91,10 +95,8 @@ public void removeFrontTest() { //test with empty array list.removeFront(); - exception = assertThrows( - IndexOutOfBoundsException.class, () -> { - list.get(0); - }); + setException(assertThrows( + IndexOutOfBoundsException.class, () -> list.get(0))); assertTrue(list.isEmpty()); //test with 1 value in array @@ -102,13 +104,19 @@ public void removeFrontTest() { list.removeFront(); assertTrue(list.isEmpty()); - for (int i = 0; i <= ITERATIONS; i++) { - list.add(i, i); - } + /* Check that next index value is now equal to index 0 + after the first index is removed */ + + fillArray(); for (int i = 0; i < ITERATIONS; i++) { - int removedValue = list.get(1); - list.removeFront(); - assertEquals(removedValue, list.get(0)); + if (list.size() >= 1) { + int nextIndex = list.get(1); + list.removeFront(); + assertEquals(nextIndex, list.get(0)); + } else { + list.removeFront(); + assertTrue(list.isEmpty()); + } } } @@ -123,10 +131,8 @@ public void removeBackTest() { //test with empty array list.removeBack(); - exception = assertThrows( - IndexOutOfBoundsException.class, () -> { - list.get(0); - }); + setException(assertThrows( + IndexOutOfBoundsException.class, () -> list.get(0))); assertTrue(list.isEmpty()); //test with 1 value in array @@ -134,15 +140,14 @@ public void removeBackTest() { list.removeBack(); assertTrue(list.isEmpty()); - //Finish this for removed -// for (int i = 0; i <= ITERATIONS; i++) { -// array.add(i, i); -// } -// for (int i = 0; i < ITERATIONS; i++) { -// int removedValue = array.get(1); -// array.removeFront(); -// assertEquals(removedValue, array.get(0)); -// } + /* Check that next index value is now equal to index 0 + after the first index is removed */ + fillArray(); + for (int i = 0; i < ITERATIONS; i++) { + int removedValue = list.get(1); + list.removeFront(); + assertEquals(removedValue, list.get(0)); + } } /** @@ -156,17 +161,13 @@ public void removeTest() { assertEquals(0, list.size()); //test with empty array - exception = assertThrows( - IndexOutOfBoundsException.class, () -> { - list.get(0); - }); + setException(assertThrows( + IndexOutOfBoundsException.class, () -> list.get(0))); assertTrue(list.isEmpty()); //test with index higher than size of array - exception = assertThrows( - IndexOutOfBoundsException.class, () -> { - list.get(list.size()); - }); + setException(assertThrows( + IndexOutOfBoundsException.class, () -> list.get(list.size()))); assertTrue(list.isEmpty()); //test with one value in array @@ -175,9 +176,7 @@ public void removeTest() { assertTrue(list.isEmpty()); assertEquals(0, list.size()); - for (int i = 0; i <= ITERATIONS; i++) { - list.add(i, i); - } + fillArray(); for (int i = 0; i < ITERATIONS; i++) { int removedValue = list.get(1); list.remove(0); @@ -195,22 +194,18 @@ public void getTest() { assertEquals(0, list.size()); //test with empty array - exception = assertThrows( - IndexOutOfBoundsException.class, () -> { - list.get(0); - }); + setException(assertThrows( + IndexOutOfBoundsException.class, () -> list.get(0))); assertTrue(list.isEmpty()); //test with index greater than size of array //test with empty array - exception = assertThrows( - IndexOutOfBoundsException.class, () -> { - list.get(1); - }); + setException(assertThrows( + IndexOutOfBoundsException.class, () -> list.get(1))); assertTrue(list.isEmpty()); //reassign values - this.fillArray(list); + this.fillArray(); //test the return values of get() for (int i = 0; i <= ITERATIONS; i++) { @@ -236,7 +231,7 @@ public void containsTest() { assertTrue(list.contains(1)); //test if Iterations exists - this.fillArray(list); + this.fillArray(); assertTrue(list.contains(ITERATIONS)); //test if number does not exist @@ -278,7 +273,7 @@ public void isEmptyTest() { assertEquals(0, list.size()); //test against non-empty array - this.fillArray(list); + this.fillArray(); assertFalse(list.isEmpty()); } @@ -310,16 +305,15 @@ public void clearTest() { assertEquals(0, list.size()); //test against non-empty array - this.fillArray(list); + this.fillArray(); list.clear(); assertEquals(0, list.size()); } /** * Helper method to fill the array in this class - * @param arr Field array */ - public void fillArray(LinkedIntList arr) { + public void fillArray() { //reassign values for (int i = 0; i <= ITERATIONS; i++) { list.addFront(i); From 6d57c849a5362e18b37202488fe58c8e4be9bec8 Mon Sep 17 00:00:00 2001 From: tobygoetz Date: Fri, 26 Jan 2024 23:45:30 -0800 Subject: [PATCH 17/17] Ready for review. Finished jUnit Tests for Doubly LinkedIntList and fixed toString() and size miss count within remove(). --- src/lists/DoublyLinkedIntList.java | 117 ++++--- src/tests/tests/DoublyLinkedIntListTest.java | 313 ++++++++++++++++++- 2 files changed, 362 insertions(+), 68 deletions(-) diff --git a/src/lists/DoublyLinkedIntList.java b/src/lists/DoublyLinkedIntList.java index 64a280c..2809689 100644 --- a/src/lists/DoublyLinkedIntList.java +++ b/src/lists/DoublyLinkedIntList.java @@ -26,7 +26,6 @@ public DoublyLinkedIntList() { front.next = back; back.prev = front; size = 0; - } private class Node { @@ -110,18 +109,21 @@ public void addBack(int value) { public void add(int index, int value) { // Counter - int i = 0; + int dex = 0; // Create new Node Node nodeAtIndex = new Node(value); Node current = front.next; - if (i <= size) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException( + "Index must be in the Range 0-" + (size - 1)); + } else { // Conditional: If index is size of List skip loop and add to back if (index == size) { this.addBack(value); } else { while (current != back) { - if (index == i) { + if (index == dex) { // Assign the new node's prev and next locators nodeAtIndex.prev = current.prev; nodeAtIndex.next = current; @@ -129,10 +131,9 @@ public void add(int index, int value) { current.prev.next = nodeAtIndex; current.prev = nodeAtIndex; } - i++; + dex++; current = current.next; - } - size++; + } size++; } } } @@ -189,47 +190,38 @@ public void removeBack() { */ @Override public int remove(int index) { - // Counter - int i = 0; - // Position + + int dex = 0; Node current = front.next; - if (index < size && index >= 0) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException( + "Index must be in the Range 0-" + (size - 1)); + } else { // Conditional to skip looping if index is first node if (index == 0) { int removedValue = current.data; removeFront(); - size--; return removedValue; // Conditional to skip looping if index is last node } else if (index == size - 1) { int removedValue = back.prev.data; removeBack(); - size--; return removedValue; + // Loop to find the node at index position } else { - // Loop to find the node at index position while (current != back) { - if (index == i) { + if (index == dex) { int removedValue = current.data; current.prev.next = current.next; current.next.prev = current.prev; size--; - // Index found so skip looping return removedValue; } else { current = current.next; - i++; + dex++; } - // Decrement size of list } -// size--; - } - } else { - if (index < 0) { - throw new IndexOutOfBoundsException("Index must be 0 or greater..."); - } else { - throw new IndexOutOfBoundsException("List does not have enough indices..."); } } return 0; } @@ -243,34 +235,32 @@ public int remove(int index) { */ @Override public int get(int index) { + // Counter - int i = 0; + int dex = 0; // Position Node current = front.next; - //Skip looping if index is at front of list - if (index == 0) { - return current.data; - // Skip looping if index is at back of list - } else if (index == size - 1) { - return back.prev.data; - // If index is in acceptable range loop to find Node at index value - }else if (index < size && index >= 0){ - while (current != back) { - if (index == i) { - return current.data; - } else { - i++; - current = current.next; - } - } - return current.data; - // Throw IndexOutOfBoundsException if index is not in range + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException( + "Index must be in the Range 0-" + (size - 1)); } else { - if (index < 0) { - throw new IndexOutOfBoundsException("Index must be 0 or greater..."); + //Skip looping if index is at front of list + if (index == 0) { + return current.data; + // Skip looping if index is at back of list + } else if (index == size - 1) { + return back.prev.data; + // If index is in acceptable range loop to find Node at index value } else { - throw new IndexOutOfBoundsException("List does not have enough indices..."); + while (current != back) { + if (index == dex) { + return current.data; + } else { + dex++; + current = current.next; + } + } return current.data; } } } @@ -284,7 +274,6 @@ public int get(int index) { @Override public boolean contains(int value) { - // Position Node current = front.next; while (current != back) { @@ -307,34 +296,28 @@ public boolean contains(int value) { @Override public int indexOf(int value) { - int i = 0; + int dex = 0; Node current = front.next; if (contains(value)) { while (current.data != value) { - i++; + dex++; current = current.next; - } return i; + } return dex; } return -1; } /** * Returns true if this list contains no values. - * * @return true if this list contains no values */ @Override public boolean isEmpty() { - - if (front.next == back && back.prev == front) { - return true; - } - return false; + return front.next == back && back.prev == front; } /** * Returns the number of values in this list. - * * @return the number of values in this list */ @Override @@ -355,7 +338,6 @@ public void clear() { /** * Returns an iterator over elements of type {@code T}. - * * @return an Iterator. */ @Override @@ -385,10 +367,17 @@ public void printList() @Override public String toString() { - return "DoublyLinkedIntList{" + - "front=" + front + - ", back=" + back + - ", size=" + size + - '}'; + Node current = this.front.next; + String list = ("DoublyLinkedIntList{Size: " + size + ", list ["); + + while (current.next != null) { + if (current.next != back) { + list += (current.data + " -> "); + } else { + list += (current.data); + } + current = current.next; + } + return list + "]}"; } } diff --git a/src/tests/tests/DoublyLinkedIntListTest.java b/src/tests/tests/DoublyLinkedIntListTest.java index 0a26037..42d7bee 100644 --- a/src/tests/tests/DoublyLinkedIntListTest.java +++ b/src/tests/tests/DoublyLinkedIntListTest.java @@ -1,6 +1,5 @@ package tests; -import lists.ArrayIntList; -import lists.LinkedIntList; +import lists.DoublyLinkedIntList; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -10,8 +9,314 @@ * @version 1.0 */ public class DoublyLinkedIntListTest { - public LinkedIntList list = new LinkedIntList(); - public Exception exception; + private DoublyLinkedIntList list = new DoublyLinkedIntList(); + private Exception exception; public static final int ITERATIONS = 15; + + protected Exception getException() { + return exception; + } + + protected void setException(Exception exception) { + this.exception = exception; + } + + /** + * Test adds Integer values to the front when empty, almost empty, + * not empty and when buffer is larger than intial size of 10 is + * surpassed. + */ + @Test + public void addFrontTest() { + assertEquals(0, list.size()); + for (int i = 0; i <= ITERATIONS; i++) { + list.addFront(i); + // Index 0 changes everytime addFront is called + assertEquals(i, list.get(0)); + } + } + + /** + * Test adds Integer values to the back when empty, almost empty, + * not empty and when buffer is larger than intial size of 10 is + * surpassed. + */ + @Test + public void addBackTest() { + list.clear(); + assertEquals(0, list.size()); + for (int i = 0; i <= ITERATIONS; i++) { + list.addBack(i); + // Index 0 changes everytime addFront is called + assertEquals(i, list.get(list.size() - 1)); + } + } + + /** + * Test adds Integer values at specific index when empty, almost + * empty,not empty and when buffer is larger than intial size of + * 10 is surpassed. + */ + @Test + public void addTest() { + list.clear(); + assertEquals(0, list.size()); + for (int i = 0; i <= ITERATIONS; i++) { + list.add(i, i); + // Index at i incrementing + assertEquals(i, list.get(i)); + } + + for (int i = ITERATIONS; i >= 0; i--) { + list.add(i, i); + // Index at i decrementing + assertEquals(i, list.get(i)); + } + + //IndexOutOfBoundsException is thrown if -1 is called + setException(assertThrows( + IndexOutOfBoundsException.class, () -> list.add(-1, ITERATIONS))); + + //IndexOutOfBoundsException is thrown if index larger than + // the amount of indices is called + setException(assertThrows( + IndexOutOfBoundsException.class, () -> list.add(list.size() + 1, ITERATIONS))); + } + + /** + * Test removes Integer values from the front of ArrayIntList when + * empty, almost empty and not empty + */ + @Test + public void removeFrontTest() { + list.clear(); + assertEquals(0, list.size()); + + //test with empty array + list.removeFront(); + + //test with 1 value in array + list.addFront(ITERATIONS); + list.removeFront(); + assertTrue(list.isEmpty()); + + /* Check that next index value is now equal to index 0 + after the first index is removed */ + + fillArray(); + for (int i = 0; i < ITERATIONS; i++) { + if (list.size() >= 1) { + int nextIndex = list.get(1); + list.removeFront(); + assertEquals(nextIndex, list.get(0)); + } else { + list.removeFront(); + assertTrue(list.isEmpty()); + } + } + } + + /** + * Test removes Integer values from the front of ArrayIntList when + * empty, almost empty and not empty + */ + @Test + public void removeBackTest() { + list.clear(); + assertEquals(0, list.size()); + + //test with empty array + list.removeBack(); + setException(assertThrows( + IndexOutOfBoundsException.class, () -> list.get(0))); + assertTrue(list.isEmpty()); + + //test with 1 value in array + list.addFront(ITERATIONS); + list.removeBack(); + assertTrue(list.isEmpty()); + + /* Check that next index value is now equal to index 0 + after the first index is removed */ + fillArray(); + for (int i = 0; i < ITERATIONS; i++) { + int removedValue = list.get(1); + list.removeFront(); + assertEquals(removedValue, list.get(0)); + } + } + + /** + * Test removes Integer values at specific index when empty, almost + * empty,not empty and when buffer is larger than intial size of + * 10 is surpassed. + */ + @Test + public void removeTest() { + list.clear(); + assertEquals(0, list.size()); + + //test with empty array + setException(assertThrows( + IndexOutOfBoundsException.class, () -> list.get(0))); + assertTrue(list.isEmpty()); + + //test with index higher than size of array + setException(assertThrows( + IndexOutOfBoundsException.class, () -> list.get(list.size()))); + assertTrue(list.isEmpty()); + + //test with one value in array + list.addFront(0); + list.remove(0); + assertTrue(list.isEmpty()); + assertEquals(0, list.size()); + + fillArray(); + for (int i = 0; i < ITERATIONS; i++) { + int removedValue = list.get(1); + list.remove(0); + assertEquals(removedValue, list.get(0)); + } + } + + /** + * Test get method returns for empty, almost empty + * and exception throw due to Index out of bounds + */ + @Test + public void getTest() { + list.clear(); + assertEquals(0, list.size()); + + //test with empty array + setException(assertThrows( + IndexOutOfBoundsException.class, () -> list.get(-1))); + assertTrue(list.isEmpty()); + + //test with index greater than size of array + //test with empty array + setException(assertThrows( + IndexOutOfBoundsException.class, () -> list.get(1))); + assertTrue(list.isEmpty()); + + //reassign values + this.fillArray(); + + //test the return values of get() + for (int i = 0; i <= ITERATIONS; i++) { + int getValue = list.get(i); + assertEquals(getValue, list.get(i)); + } + } + + /** + * Test contains() for non-existent values, existing values, + * against empty list + */ + @Test + public void containsTest() { + list.clear(); + assertEquals(0, list.size()); + + //Test if empty + assertFalse(list.contains(ITERATIONS)); + + //Test if value 1 exists + list.add(0, 1); + assertTrue(list.contains(1)); + + //test if Iterations exists + this.fillArray(); + assertTrue(list.contains(ITERATIONS)); + + //test if number does not exist + assertFalse(list.contains(80085)); + } + + /** + * Test IndexOf() for no values, some values, + * against empty list + */ + @Test + public void IndexOfTest() { + //saftey check + list.clear(); + assertEquals(0, list.size()); + + //test bounds of IndexOf() + assertEquals(-1, list.indexOf(-ITERATIONS)); + + //test if indices match value returns of all indices + System.out.println(list); + for (int i = 0; i < ITERATIONS; i++) { + list.addBack(i); + } + for (int i = 0; i < ITERATIONS; i++) { + list.addBack(i); + assertEquals(i, list.indexOf(i)); + } + } + + /** + * Test isEmpty() for no values, some values, + * against empty list + */ + @Test + public void isEmptyTest() { + //saftey check + list.clear(); + assertEquals(0, list.size()); + + //test against non-empty array + this.fillArray(); + assertFalse(list.isEmpty()); + } + + /** + * Test size() for no values, some values, + * against empty list + */ + @Test + public void sizeTest() { + //saftey check + list.clear(); + assertEquals(0, list.size()); + + //test against non-empty array + for (int i = 0; i < ITERATIONS; i++) { + list.addBack(i); + assertEquals(i + 1, list.size()); + } + } + + /** + * Test clear() for no values, some values, + * against empty list + */ + @Test + public void clearTest() { + //saftey check + list.clear(); + assertEquals(0, list.size()); + + //test against non-empty array + this.fillArray(); + list.clear(); + assertEquals(0, list.size()); + } + + /** + * Helper method to fill the array in this class + */ + public void fillArray() { + //reassign values + for (int i = 0; i <= ITERATIONS; i++) { + list.addFront(i); + // Index 0 changes everytime addFront is called + assertEquals(i, list.get(0)); + } + } + } \ No newline at end of file