From c0a6542744751aa8ad982927fdad1c41753272a5 Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Tue, 9 Jan 2024 13:17:02 -0800 Subject: [PATCH 1/5] finished all methods, haven't gone and cleaned everything up yet! --- .idea/misc.xml | 1 - src/ArrayList.java | 217 +++++++++++++++++++++++++++++++++++++++++++++ src/Main.java | 10 ++- 3 files changed, 223 insertions(+), 5 deletions(-) create mode 100644 src/ArrayList.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 6f29fee..5af9c98 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/src/ArrayList.java b/src/ArrayList.java new file mode 100644 index 0000000..d0547d4 --- /dev/null +++ b/src/ArrayList.java @@ -0,0 +1,217 @@ +import java.util.Iterator; + +public class ArrayList implements IntList{ + + private int size; + + private int[] buffer; + + public ArrayList(){ + 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]; + buffer[0] = value; + size++; + // or + + // for (int i = size - 1; i>=0; i--){ + // buffer[i+1] = buffer[i]; + // } + + } + } + + /** + * 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) { + 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) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("index not in range"); // throw exception. get used to this!! + } + + if (size == buffer.length) { + int[] largeBuffer = new int[buffer.length * 2]; //create a bigger array if array isn't of size, (I needed to look this up) + System.arraycopy(buffer, 0, largeBuffer, 0, size); + buffer = largeBuffer; + } + + for (int i = size; i > index; i--) { + buffer[i] = buffer[i - 1]; // shift elements to the right. used in alot of methods + } + + buffer[index] = value; //insert new value. should always be the final step!!! + size++; + } + + /** + * 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() { + if (size < 0){ + for (int i = 0; i < 0; i--) { + buffer[i] = buffer[i + 1]; + } + size--; + } + } + + /** + * 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) { + 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) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("number not in array"); + } + + int numberRemoved = buffer[index]; + + // Shift elements to the left (taken from remove front) + for (int i = index; i < size - 1; i++) { + buffer[i] = buffer[i + 1]; + } + + size--; + return numberRemoved; //return the number removed from the position + } + + + /** + * 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 < 0 || index >= size) { + throw new IndexOutOfBoundsException("number not in array"); + } + + return buffer[index]; // return given # + } + + /** + * 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) { + for (int i = 0; i < size; i++) { + if (buffer[i] == value) { + return true; + } + } + 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) { + for (int i = 0; i < size; i++) { + if (buffer[i] == value) { + return i; // Value found, return its index + } + } + return -1; // Value not found in the list + } + + /** + * 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() { + size = 0; + } + + /** + * 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..34367cc 100644 --- a/src/Main.java +++ b/src/Main.java @@ -6,10 +6,12 @@ public static void main(String[] args) { // to see how IntelliJ IDEA suggests fixing it. System.out.printf("Hello and welcome!"); - 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 . - System.out.println("i = " + i); + IntList firstList; + + ArrayList secondList = new ArrayList(); + + IntList thirdList = new ArrayList(); + } } } \ No newline at end of file From 210ec5a3b1c8c61415b51f7067e470136b1b4c01 Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Tue, 9 Jan 2024 13:22:03 -0800 Subject: [PATCH 2/5] finished all methods, haven't gone and cleaned everything up yet! --- src/Main.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Main.java b/src/Main.java index 34367cc..9396099 100644 --- a/src/Main.java +++ b/src/Main.java @@ -13,5 +13,4 @@ public static void main(String[] args) { IntList thirdList = new ArrayList(); } - } -} \ No newline at end of file + } \ No newline at end of file From 3cca56a202aaec802fb7fdd578e278b7a9eff8e4 Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Mon, 22 Jan 2024 00:10:11 -0800 Subject: [PATCH 3/5] Completed is most of the ArrayList Class and it's methods. Also completed are almost all the tests for the classes. I am having trouble with the tests for the iterator and resizing the ArrayList --- IntListReview.iml | 90 ++++++++++++++++++++++++++ src/ArrayList.java | 98 +++++++++++++++++++--------- src/ArrayListTest.java | 139 ++++++++++++++++++++++++++++++++++++++++ src/ArrayListTests.java | 1 + src/IntList.java | 2 + src/Main.java | 1 + 6 files changed, 300 insertions(+), 31 deletions(-) create mode 100644 src/ArrayListTest.java create mode 100644 src/ArrayListTests.java diff --git a/IntListReview.iml b/IntListReview.iml index c90834f..a89274b 100644 --- a/IntListReview.iml +++ b/IntListReview.iml @@ -7,5 +7,95 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ArrayList.java b/src/ArrayList.java index d0547d4..c4662b2 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -19,19 +19,12 @@ public ArrayList(){ */ @Override public void addFront(int value) { - - for (int i = size; i <= 0; i--){ - buffer[i] = buffer[i - 1]; - buffer[0] = value; - size++; - // or - - // for (int i = size - 1; i>=0; i--){ - // buffer[i+1] = buffer[i]; - // } - + for (int i = size - 1; i>=0; i--){ // shift elements + buffer[i+1] = buffer[i]; + } + buffer[0] = value; + size++; } - } /** * Appends (inserts) the specified value at the back of the list (at index size()-1). @@ -40,6 +33,9 @@ public void addFront(int value) { */ @Override public void addBack(int value) { + if (size == buffer.length) { + resize(size * 2); + } buffer[size] = value; size++; } @@ -58,18 +54,13 @@ public void add(int index, int value) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException("index not in range"); // throw exception. get used to this!! } - - if (size == buffer.length) { - int[] largeBuffer = new int[buffer.length * 2]; //create a bigger array if array isn't of size, (I needed to look this up) - System.arraycopy(buffer, 0, largeBuffer, 0, size); - buffer = largeBuffer; + if (size == buffer.length) { //double array size to assure space for new element + resize(size * 2); } - - for (int i = size; i > index; i--) { - buffer[i] = buffer[i - 1]; // shift elements to the right. used in alot of methods + for (int i = size - 1; i >= 0; i--) { // shift elements to the right + buffer[i + 1] = buffer[i]; } - - buffer[index] = value; //insert new value. should always be the final step!!! + buffer[index] = value; //insert the new value at the given index size++; } @@ -80,8 +71,8 @@ public void add(int index, int value) { */ @Override public void removeFront() { - if (size < 0){ - for (int i = 0; i < 0; i--) { + if (size > 0){ + for (int i = 0; i <= size - 2; i++) { buffer[i] = buffer[i + 1]; } size--; @@ -94,7 +85,10 @@ public void removeFront() { */ @Override public void removeBack() { - if (size < 0) { + if (isEmpty()) { + throw new IndexOutOfBoundsException("Cannot remove from an empty list"); + }else{ + buffer[size - 1] = 0; size--; } } @@ -110,17 +104,15 @@ public void removeBack() { */ @Override public int remove(int index) { - if (index < 0 || index >= size) { + if (index < 0 || index >= size) { //or do an if else instead of on "or" so I can be more specific with my error messages throw new IndexOutOfBoundsException("number not in array"); } - int numberRemoved = buffer[index]; - // Shift elements to the left (taken from remove front) for (int i = index; i < size - 1; i++) { buffer[i] = buffer[i + 1]; } - + buffer[size -1] = 0; size--; return numberRemoved; //return the number removed from the position } @@ -193,7 +185,7 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + return size; } /** @@ -202,7 +194,8 @@ public int size() { */ @Override public void clear() { - size = 0; + buffer = new int[10]; + size = 0; } /** @@ -214,4 +207,47 @@ public void clear() { public Iterator iterator() { return null; } + //create a private helper iterator class + private class IntListIterator implements Iterator{ + 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() { + int currentValue = buffer[i]; + i++; + return null; + } + } + //iterators enables main/client to use a for-each loop + // on my intlist + + @Override + public void resize(int newSize){ + int[] newBuffer = new int[newSize]; //create a new array that will be larger + for (int i = 0; i < buffer.length; i++){ + newBuffer[i] = buffer[i]; + } + buffer = newBuffer; + // this overwrites the previous array with our new larger array + } } diff --git a/src/ArrayListTest.java b/src/ArrayListTest.java new file mode 100644 index 0000000..28890bc --- /dev/null +++ b/src/ArrayListTest.java @@ -0,0 +1,139 @@ +import static org.junit.jupiter.api.Assertions.*; + +class ArrayListTest { + + @org.junit.jupiter.api.Test + void testAddFront() { + ArrayList myArray = new ArrayList(); + myArray.addFront(3); + assertEquals(3, myArray.get(0)); + } + + @org.junit.jupiter.api.Test + void addBack() { + ArrayList myArray = new ArrayList(); + myArray.addBack(1); + assertEquals(1, myArray.get(0)); + } + + @org.junit.jupiter.api.Test + void testAdd() { + ArrayList list = new ArrayList(); + list.add(0, 12); + list.add(1, 4); + list.add(1, 6); + assertEquals(12, list.get(0)); + assertEquals(6, list.get(1)); + assertEquals(4, list.get(2)); + } + + @org.junit.jupiter.api.Test + void removeFront() { + ArrayList myArray = new ArrayList(); + myArray.addFront(1); + myArray.addFront(2); + myArray.addFront(3); + myArray.removeFront(); + assertEquals(2, myArray.get(0)); + assertEquals(1, myArray.get(1)); + myArray.removeFront(); + assertEquals(1,myArray.get(0)); + } + + @org.junit.jupiter.api.Test + void removeBack() { + //had to look this test up. super confusing but works for an empty array list. + //how would I change this to work for checking if just the back number is removed?? + ArrayList myArray = new ArrayList(); + assertThrows(IndexOutOfBoundsException.class, myArray::removeBack); + } + + @org.junit.jupiter.api.Test + void remove() { + ArrayList myList = new ArrayList(); + myList.addFront(1); + myList.addFront(2); + myList.addFront(3); + myList.remove(1); + assertEquals(3, myList.get(0)); + assertEquals(1, myList.get(1)); + } + + @org.junit.jupiter.api.Test + void get() { + ArrayList myList = new ArrayList(); + myList.addFront(1); + myList.addFront(2); + myList.addFront(3); + assertEquals(3, myList.get(0)); + assertEquals(2, myList.get(1)); + assertEquals(1, myList.get(2)); + } + + @org.junit.jupiter.api.Test + void contains() { + ArrayList myArray = new ArrayList(); + myArray.addFront(1); + myArray.addFront(2); + myArray.addFront(3); + assertFalse(myArray.contains(5)); + assertTrue(myArray.contains(1)); + } + + @org.junit.jupiter.api.Test + void indexOf() { + ArrayList myArray = new ArrayList(); + myArray.addFront(1); + myArray.addFront(2); + myArray.addFront(3); + assertEquals(2, myArray.indexOf(1)); + assertEquals(-1, myArray.indexOf(10)); + } + + @org.junit.jupiter.api.Test + void isEmpty() { + ArrayList myArray = new ArrayList(); + assertTrue(!myArray.isEmpty()); + myArray.addFront(1); + assertFalse(myArray.isEmpty()); + } + + @org.junit.jupiter.api.Test + void size() { + ArrayList myArray = new ArrayList(); + assertEquals(0, myArray.size()); + myArray.addFront(1); + myArray.addFront(2); + myArray.addFront(3); + assertEquals(3, myArray.size()); + } + + @org.junit.jupiter.api.Test + void clear() { + ArrayList myArray = new ArrayList(); + myArray.size(); + myArray.addFront(1); + myArray.addFront(2); + myArray.addFront(3); + myArray.size(); + myArray.clear(); + myArray.size(); + } + + @org.junit.jupiter.api.Test + void iterator() { + + } + + @org.junit.jupiter.api.Test + void resize() { + ArrayList myArray = new ArrayList(); + myArray.size(); + myArray.addFront(1); + myArray.addFront(2); + myArray.addFront(3); + myArray.size(); + myArray.resize(6); + myArray.size(); + } +} \ No newline at end of file diff --git a/src/ArrayListTests.java b/src/ArrayListTests.java new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/ArrayListTests.java @@ -0,0 +1 @@ + diff --git a/src/IntList.java b/src/IntList.java index 398c27b..e1926b8 100644 --- a/src/IntList.java +++ b/src/IntList.java @@ -93,4 +93,6 @@ public interface IntList extends Iterable { * The list will be empty after this call returns. */ void clear(); + + void resize(int newSize); } diff --git a/src/Main.java b/src/Main.java index 9396099..fb8c2d9 100644 --- a/src/Main.java +++ b/src/Main.java @@ -13,4 +13,5 @@ public static void main(String[] args) { IntList thirdList = new ArrayList(); } + } \ No newline at end of file From a4799fdc58e81e3c6ddfcfd70a16cbd78e9e7387 Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Thu, 25 Jan 2024 16:26:33 -0800 Subject: [PATCH 4/5] Completed is most of the LinkedList Class and it's methods. Also completed are almost all the tests for the classes. I am having trouble with the tests for the iterator for the LinkedList. Updated JavaDocs as well --- src/ArrayListTest.java | 5 +- src/LinkedList.java | 318 ++++++++++++++++++++++++++++++++++++++++ src/LinkedListTest.java | 134 +++++++++++++++++ 3 files changed, 456 insertions(+), 1 deletion(-) create mode 100644 src/LinkedList.java create mode 100644 src/LinkedListTest.java diff --git a/src/ArrayListTest.java b/src/ArrayListTest.java index 28890bc..cdff909 100644 --- a/src/ArrayListTest.java +++ b/src/ArrayListTest.java @@ -1,5 +1,8 @@ import static org.junit.jupiter.api.Assertions.*; - +/* +* Ryder Dettloff +* Tests for IntList Review; ArrayList methods +*/ class ArrayListTest { @org.junit.jupiter.api.Test diff --git a/src/LinkedList.java b/src/LinkedList.java new file mode 100644 index 0000000..a0c5768 --- /dev/null +++ b/src/LinkedList.java @@ -0,0 +1,318 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class LinkedList implements IntList { + + //define what a node is + private static class Node{ + int data; + Node nextNode; + + Node(int data){ + this.data = data; + this.nextNode = null; + + } + } + //set up the head + private Node head; + //set up the size field + private int size; + + //add constructor to initialize the data + public LinkedList(){ + 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) { + Node theNewNode = new Node(value); + if (head == null){ + head = theNewNode; + size++; + //linkedList is empty + + }else{ + theNewNode.nextNode = head; + head = theNewNode; + //LinkedList has nodes in it + } + + } + + /** + * 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 theNewNode = new Node(value); + if (head == null){ + head = theNewNode; //check if node is empty, if it is, assign the new node to the head + } else { + Node currentNode = head; //create new variable current node + while (currentNode.nextNode != null) { //iterate through the list until a null node value is found + currentNode = currentNode.nextNode; //currentNode keeps track of position in the list + } + currentNode.nextNode = theNewNode; //once a null value is found, assign it to that value + } + size++; //increase size of list + } + + /** + * 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) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index is out of bounds!"); + } + if (index == 0) { + addFront(value); + } else { + Node theNewNode = new Node(value); + Node currentNode = head; + int filter = 0; + // Move currentNode to the node just before the index where we want to insert + while (filter < index - 1) { + currentNode = currentNode.nextNode; + filter++; + } + // Link the new node to the rest of the list + theNewNode.nextNode = currentNode.nextNode; + currentNode.nextNode = theNewNode; + size++; + } + } + + /** + * 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() { + if (head != null){ + head = head.nextNode; + size--; + } + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + * very similar to addback method + */ + @Override + public void removeBack() { + if (head == null) { + throw new IndexOutOfBoundsException("index is out of bounds!"); + } + if (size == 1) { + head = null; + }else{ + Node currentNode = head; //create new variable current node + while (currentNode.nextNode.nextNode != null) { //iterate through the list until a null node value is found + currentNode = currentNode.nextNode; //currentNode keeps track of position in the list, + // it keeps track of the next node's next node so we know which nodes next value is null + } + currentNode.nextNode = null; //assign that node to null essentially removing node + } + size--; //subtract 1 from the 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) { + int removedData = 0; + if(index < 0) { + throw new IndexOutOfBoundsException("Index is out of bounds!"); + } + if (index == 0){ //if list is empty + head = null; + } else { //if list has data + Node currentNode = head; //assign head to current node to track value + int filter = 0; //use filter as an iterator count + while (filter < index - 1) { //while filter is less than the last index value + currentNode = currentNode.nextNode; + filter++; + } + removedData = currentNode.nextNode.data; //when the value is found, assign removed data to it + currentNode.nextNode = currentNode.nextNode.nextNode; + } + + size--; //subtract the size + return removedData; //return removed data + } + + /** + * 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 < 0) { + throw new IndexOutOfBoundsException("Index is out of bounds"); + } + Node currentNode = head; //assign currentNode to head to track value + for (int filter = 0; filter < index; filter++) { //create value filter to iterate through the list + currentNode = currentNode.nextNode; //when value is found, assign it + } + + return currentNode.data; //return the data + } + + /** + * 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) { + Node currentNode = head; //assign current node to track value + while (currentNode != null) { + if (currentNode.data == value) { + return true; + } + currentNode = currentNode.nextNode; + } + 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) { + Node currentNode = head; + for (int filter = 0; filter < size; filter++) { //filter counts the index + if (currentNode.data == value) { + return filter; //when data is found, it returns the index + } + currentNode = currentNode.nextNode; + } + return -1; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + return head == null; + } + + /** + * Returns the number of values in this list. + * + * @return the number of values in this list + */ + @Override + public int size() { + return size; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() { + size = 0; //clear by assigning size to 0 + head = null; //assigning head to null will assure clearance + } + + @Override + public void resize(int newSize) { + throw new UnsupportedOperationException("resizing is not allowed"); + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return new singlyLinkedIterator(); // this just inlines the variable so its 1 line of code instead of 2. + /*ORRR + singlyLinkedIterator myIterator = new singlyLinkedIterator(); + return myIterator; //using two lines makes it more readable + */ + } + //helper class 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 || current.nextNode == null) { + return false; + } else { + return true; + } + } + + /** + * 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 not next node to go to!"); + } + int item = current.data; + current = current.nextNode; + return item; + } + } +} diff --git a/src/LinkedListTest.java b/src/LinkedListTest.java new file mode 100644 index 0000000..f0d5fb3 --- /dev/null +++ b/src/LinkedListTest.java @@ -0,0 +1,134 @@ +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +/* + * Ryder Dettloff + * Tests for IntList Review; LinkedList methods + */ +class LinkedListTest { + + + @Test + void AddFront() { + LinkedList myList = new LinkedList(); + myList.addFront(1); + myList.addFront(5); + myList.addFront(10); + assertEquals(10, myList.get(0)); + assertEquals(5, myList.get(1)); + assertEquals(1, myList.get(2)); + } + + @Test + void AddBack() { + LinkedList myList = new LinkedList(); + myList.addBack(1); + myList.addBack(5); + myList.addBack(10); + assertEquals(1, myList.get(0)); + assertEquals(5, myList.get(1)); + assertEquals(10, myList.get(2)); + } + + @Test + void Add() { + LinkedList myList = new LinkedList(); + myList.addFront(1); + myList.addFront(5); + myList.add(1, 10); + assertEquals(5, myList.get(0)); + assertEquals(10, myList.get(1)); + assertEquals(1, myList.get(2)); + } + + @Test + void RemoveFront() { + LinkedList myList = new LinkedList(); + myList.addFront(1); + myList.addFront(5); + myList.removeFront(); + assertEquals(1, myList.get(0)); + assertEquals(0, myList.size()); + } + + @Test + void RemoveBack() { + LinkedList myList = new LinkedList(); + myList.addBack(1); + myList.addBack(5); + myList.addBack(10); + myList.removeBack(); + assertEquals(1, myList.get(0)); + assertEquals(5, myList.get(1)); + assertEquals(2, myList.size()); + } + + @Test + void Remove() { + LinkedList mylist = new LinkedList(); + mylist.addBack(1); + mylist.addBack(5); + mylist.addBack(10); + mylist.remove(1); + assertEquals(1, mylist.get(0)); + assertEquals(10, mylist.get(1)); + assertEquals(2, mylist.size()); + } + + @Test + void Get() { + LinkedList myList = new LinkedList(); + myList.addFront(1); + myList.addFront(5); + myList.addFront(10); + assertEquals(5, myList.get(1)); + } + + @Test + void Contains() { + LinkedList myList = new LinkedList(); + myList.addFront(1); + myList.addFront(5); + myList.addFront(10); + assertTrue(myList.contains(5)); + assertFalse(myList.contains(6)); + } + + @Test + void IndexOf() { + LinkedList myList = new LinkedList(); + myList.addBack(1); + myList.addBack(5); + myList.addBack(10); + assertEquals(1, myList.indexOf(5)); + assertEquals(-1, myList.indexOf(6)); + } + + @Test + void IsEmpty() { + LinkedList myList = new LinkedList(); + myList.addBack(1); + assertFalse(myList.isEmpty()); + myList.clear(); + assertTrue(myList.isEmpty()); + } + + @Test + void Size() { + LinkedList myList = new LinkedList(); + assertEquals(0, myList.size()); + myList.addBack(1); + myList.addBack(5); + myList.addBack(10); + assertEquals(3, myList.size()); + } + + @Test + void Clear() { + LinkedList myList = new LinkedList(); + assertEquals(0, myList.size()); + myList.addBack(1); + myList.addBack(5); + myList.clear(); + assertTrue(myList.isEmpty()); + } +} \ No newline at end of file From 256099ff50da20d254da83fcae3bedc4fe5869d5 Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Mon, 29 Jan 2024 00:33:43 -0800 Subject: [PATCH 5/5] I still need to do some work, debug, and make sure my tests work! updated linkedlist test too. --- src/DoublyLinkedList.java | 309 ++++++++++++++++++++++++++++++++++ src/DoublyLinkedListTest.java | 160 ++++++++++++++++++ src/LinkedList.java | 12 +- src/LinkedListTest.java | 1 + 4 files changed, 474 insertions(+), 8 deletions(-) create mode 100644 src/DoublyLinkedList.java create mode 100644 src/DoublyLinkedListTest.java diff --git a/src/DoublyLinkedList.java b/src/DoublyLinkedList.java new file mode 100644 index 0000000..691f679 --- /dev/null +++ b/src/DoublyLinkedList.java @@ -0,0 +1,309 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class DoublyLinkedList implements IntList { + //define node + private static class Node { + int data; + Node nextNode; + Node previousNode; + + Node(int data) { + this.data = data; + this.nextNode = null; + this.previousNode = null; + } + } +//set up size field + //set up head and tail nodes + private Node head; + + private Node tail; + private int size; + +//constructor + public DoublyLinkedList() { + size = 0; + head = null; + tail = 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) { + Node theNewNode = new Node(value); + if (head != null) { //if list isn't empty, add node head and shift + theNewNode.nextNode = head; + head.previousNode = theNewNode; + head = theNewNode; + } else { + head = theNewNode; + } + 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) { + Node theNewNode = new Node(value); + if (head != null) { + tail.nextNode = theNewNode; + theNewNode.previousNode = tail; + tail = theNewNode; + } else { + head = theNewNode; // Assign head when the list is empty + tail = theNewNode; + } + } + + /** + * 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) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index is out of bounds!"); + } + if (index != 0 && index != size - 1) { //if added node isn't the head or tail index + Node newNode = new Node(value); // replace the node and shift to where previous node points + Node currentNode = head; + for (int filter = 0; filter < index; filter++) { + currentNode = currentNode.nextNode; + } + newNode.previousNode = currentNode.previousNode; + newNode.nextNode = currentNode; + currentNode.previousNode.nextNode = newNode; + currentNode.previousNode = newNode; + } else if (index == 0) { + addFront(value); + } else { + addBack(value); + } + size++; + } + + /** + * 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() { + if (head != null) { //if list isn't assign next node to head and set + head = head.nextNode; // the node to null + head.previousNode = null; + } else { + tail = null; + } + size--; + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() { + if (tail != null) { + if (tail.previousNode != null) { + tail = tail.previousNode; + tail.nextNode = null; + } else { + // If tail is the only node in the list + tail = null; + head = null; + } + } + 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) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index is out of bounds!"); + } + int removedData; //works similar to add method + if (index != 0 && index != size - 1) { // if it isn't head or tail, remove the data + Node currentNode = head; //and point the previous node to the next node of that node + for (int filter = 0; filter < index; filter++) { + currentNode = currentNode.nextNode; + } + removedData = currentNode.data; + currentNode.previousNode.nextNode = currentNode.nextNode; + currentNode.nextNode.previousNode = currentNode.previousNode; + } else if (index == 0) { + removedData = head.data; + removeFront(); + } else { + removedData = tail.data; + removeBack(); + + } + size--; + return removedData; + } + + /** + * 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 < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index is out of bounds"); + } + + Node currentNode = head; + for (int filter = 0; filter < index; filter++) { + currentNode = currentNode.nextNode; + } + + return currentNode.data; + } + + /** + * 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) { + Node currentNode = head; + while (currentNode != null) { + if (currentNode.data == value) { + return true; + } + currentNode = currentNode.nextNode; + } + 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) { + Node currentNode = head; + for (int filter = 0; filter < size; filter++) { //filter counts the index + if (currentNode.data == value) { + return filter; //when data is found, it returns the index + } + currentNode = currentNode.nextNode; + } + return -1; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + return size == 0; + } + + /** + * Returns the number of values in this list. + * + * @return the number of values in this list + */ + @Override + public int size() { + return size; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() { + size = 0; + head = null; + tail = null; + } + + @Override + public void resize(int newSize) { + + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return new doublyLinkedIterator(); + } + + private class doublyLinkedIterator implements Iterator { + private Node current; + + public doublyLinkedIterator() { + current = head; + } + + /** + * Returns {@code true} if the iteration has more elements. + * + * @return {@code true} if the iteration has more elements + */ + @Override + public boolean hasNext() { + 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("No next element available"); + } + int thisNode = current.data; + current = current.nextNode; + return thisNode; + } + } +} diff --git a/src/DoublyLinkedListTest.java b/src/DoublyLinkedListTest.java new file mode 100644 index 0000000..d666072 --- /dev/null +++ b/src/DoublyLinkedListTest.java @@ -0,0 +1,160 @@ +import org.junit.jupiter.api.Test; + +import java.util.Iterator; + +import static org.junit.jupiter.api.Assertions.*; +/* + * Ryder Dettloff + * Tests for IntList Review; DoublyLinkedList methods + * I have work to do to get this to work )): + */ +class DoublyLinkedListTest { + + + @Test + void AddFront() { + DoublyLinkedList myList = new DoublyLinkedList(); + myList.addFront(1); + myList.addFront(5); + myList.addFront(10); + assertEquals(10, myList.get(0)); + assertEquals(5, myList.get(1)); + assertEquals(1, myList.get(2)); + } + + @Test + void AddBack() { + // Create a new instance of DoublyLinkedList + DoublyLinkedList myList = new DoublyLinkedList(); + myList.addBack(1); + myList.addBack(5); + myList.addBack(10); + assertEquals(1, myList.get(0)); + assertEquals(5, myList.get(1)); + assertEquals(10, myList.get(2)); + } + + @Test + void Add() { + DoublyLinkedList myList = new DoublyLinkedList(); + myList.addFront(1); + myList.addFront(5); + myList.add(1, 10); + assertEquals(5, myList.get(0)); + assertEquals(10, myList.get(1)); + assertEquals(1, myList.get(2)); + } + + @Test + void RemoveFront() { + DoublyLinkedList myList = new DoublyLinkedList(); + myList.addFront(1); + myList.addFront(5); + myList.removeFront(); + assertEquals(1, myList.get(0)); + assertEquals(0, myList.size()); + } + + @Test + void RemoveBack() { + DoublyLinkedList myList = new DoublyLinkedList(); + myList.addBack(1); + myList.addBack(5); + myList.addBack(10); + myList.removeBack(); + assertEquals(5, myList.get(myList.size() - 1)); + assertEquals(2, myList.size()); + } + + @Test + void Remove() { + DoublyLinkedList mylist = new DoublyLinkedList(); + mylist.addFront(1); + mylist.addFront(5); + mylist.addFront(10); + mylist.remove(1); + assertEquals(10, mylist.get(0)); + assertEquals(1, mylist.get(1)); + assertEquals(2, mylist.size()); + } + + @Test + void Get() { + DoublyLinkedList myList = new DoublyLinkedList(); + myList.addFront(1); + myList.addFront(5); + myList.addFront(10); + assertEquals(5, myList.get(1)); + } + + @Test + void Contains() { + DoublyLinkedList myList = new DoublyLinkedList(); + myList.addFront(1); + myList.addFront(5); + myList.addFront(10); + assertTrue(myList.contains(5)); + assertFalse(myList.contains(6)); + } + + @Test + void IndexOf() { + DoublyLinkedList myList = new DoublyLinkedList(); + myList.addBack(1); + myList.addBack(5); + myList.addBack(10); + assertEquals(1, myList.indexOf(5)); + assertEquals(-1, myList.indexOf(6)); + } + + @Test + void IsEmpty() { + DoublyLinkedList myList = new DoublyLinkedList(); + myList.addBack(1); + assertFalse(myList.isEmpty()); + myList.clear(); + assertTrue(myList.isEmpty()); + } + + @Test + void Size() { + DoublyLinkedList myList = new DoublyLinkedList(); + assertEquals(0, myList.size()); + myList.addBack(1); + myList.addBack(5); + myList.addBack(10); + assertEquals(3, myList.size()); + } + + @Test + void Clear() { + DoublyLinkedList myList = new DoublyLinkedList(); + assertEquals(0, myList.size()); + myList.addBack(1); + myList.addBack(5); + myList.clear(); + assertTrue(myList.isEmpty()); + } + + /* + I had trouble with this test but finally got it working + it tests the itertators and its methods (hasNext) and next + */ + @Test + public void testIterator() { + DoublyLinkedList mylist = new DoublyLinkedList(); + mylist.addFront(1); + mylist.addFront(5); + mylist.addFront(10); + + Iterator iterator = mylist.iterator(); + + assertTrue(iterator.hasNext()); + assertEquals(10, iterator.next()); + assertTrue(iterator.hasNext()); + assertEquals(5, iterator.next()); + assertTrue(iterator.hasNext()); + assertEquals(1, iterator.next()); + assertFalse(iterator.hasNext()); + } +} \ No newline at end of file diff --git a/src/LinkedList.java b/src/LinkedList.java index a0c5768..91e5d16 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -292,11 +292,7 @@ public singlyLinkedIterator(){ */ @Override public boolean hasNext() { - if (current == null || current.nextNode == null) { - return false; - } else { - return true; - } + return current != null; } /** @@ -307,12 +303,12 @@ public boolean hasNext() { */ @Override public Integer next() { - if(current == null){ + if(current.nextNode == null){ throw new NoSuchElementException("there is not next node to go to!"); } - int item = current.data; + int thisNode = current.data; current = current.nextNode; - return item; + return thisNode; } } } diff --git a/src/LinkedListTest.java b/src/LinkedListTest.java index f0d5fb3..9ec0cda 100644 --- a/src/LinkedListTest.java +++ b/src/LinkedListTest.java @@ -131,4 +131,5 @@ void Clear() { myList.clear(); assertTrue(myList.isEmpty()); } + } \ No newline at end of file