From edb7ad87d8d3f13e4fc03e0c75e93aed20097efa Mon Sep 17 00:00:00 2001 From: William Castillo Date: Tue, 9 Jan 2024 13:18:49 -0800 Subject: [PATCH 01/32] Added starter code, finished some of the easy methods. --- src/ArrayIntList.java | 194 ++++++++++++++++++++++++++++++++++++++++++ src/Main.java | 26 ++++-- 2 files changed, 215 insertions(+), 5 deletions(-) create mode 100644 src/ArrayIntList.java diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java new file mode 100644 index 0000000..c0b9bd9 --- /dev/null +++ b/src/ArrayIntList.java @@ -0,0 +1,194 @@ +import java.util.Iterator; + +public class ArrayIntList implements IntList +{ + private int size; + private int[] buffer; + + public ArrayIntList() + { + //initialize my 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) + { + // start at 0, stop at the last element, increase by one + for (int i = 0; i <= size; i++) + { + //shift over to the right + buffer[i + 1] = buffer[i]; + } + buffer[4] = buffer[3]; + buffer[3] = buffer[2]; + buffer[2] = buffer[1]; + buffer[1] = buffer[0]; + + // 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 += 1; + } + + /** + * 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) + { + buffer[index] = 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() + { + // for loop to shift elements to the left + // buffer[0] = buffer[1] + // buffer[1] = buffer[2] + for (int i = 0; i < size - 1; i++) + { + buffer[i] = buffer[i + 1]; + } + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() + { + buffer[size - 1] = 0; + } + + /** + * 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 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 size; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() + { + for (int i = 0; i < size; i++) + { + buffer[i] = 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..9cfea34 100644 --- a/src/Main.java +++ b/src/Main.java @@ -4,12 +4,28 @@ public class Main { public static void main(String[] args) { //TIP Press with your caret at the highlighted text // to see how IntelliJ IDEA suggests fixing it. - System.out.printf("Hello and welcome!"); +// System.out.printf("Hello and welcome!"); +// +// //you cannot create an object of an interface, +// //but you can create a class that implements +// //the methods from the interface to create an object +// IntList firstList; +// +// ArrayIntList secondList = new ArrayIntList(); +// +// IntList thirdList = 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 . - System.out.println("i = " + i); + ArrayIntList yes = new ArrayIntList(); + for (int each : yes) + { + System.out.println(each); } + +// 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); +// } + } } \ No newline at end of file From b691f68e67585d7f8f62101d3f4567cae003e1f0 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Tue, 9 Jan 2024 13:46:47 -0800 Subject: [PATCH 02/32] Completed the remove(index) method. --- src/ArrayIntList.java | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java index c0b9bd9..58cc8cd 100644 --- a/src/ArrayIntList.java +++ b/src/ArrayIntList.java @@ -104,7 +104,31 @@ 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 = index; index <= size - 1; index++) + { + buffer[i] = buffer[i + 1]; + } + + //reassign the last value of the size to 0 + buffer[size - 1] = 0; + + //don't forget to decrement size + size--; + + return copyOfRemovedValue; } /** From 147d448355dce46059ec9f87cfb3284fe5c47ad6 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Tue, 9 Jan 2024 14:19:01 -0800 Subject: [PATCH 03/32] Completed addBack() and created resize() --- src/ArrayIntList.java | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java index 58cc8cd..b6ab4d2 100644 --- a/src/ArrayIntList.java +++ b/src/ArrayIntList.java @@ -46,6 +46,11 @@ public void addFront(int value) 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 += 1; } @@ -62,7 +67,10 @@ public void addBack(int value) @Override public void add(int index, int value) { - buffer[index] = value; + if (size == buffer.length) + { + resize(size * 2); + } } /** @@ -203,6 +211,28 @@ public void clear() { buffer[i] = 0; } + //another way to do this + + //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 buffer space is no longer "pointed to" and will eventually + //be cleaned up by the garbage collector } /** From 2dd7c7161ce9eb3f2cd5ec1ebd8cfbb9fa77dd7c Mon Sep 17 00:00:00 2001 From: William Castillo Date: Tue, 9 Jan 2024 14:34:21 -0800 Subject: [PATCH 04/32] Created a private helper Iterator class, contains implements hasNext and next methods. --- src/ArrayIntList.java | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java index b6ab4d2..e93c836 100644 --- a/src/ArrayIntList.java +++ b/src/ArrayIntList.java @@ -1,4 +1,5 @@ import java.util.Iterator; +import java.util.NoSuchElementException; public class ArrayIntList implements IntList { @@ -245,4 +246,49 @@ public Iterator iterator() { return null; } + + //create a private helper Iterator class + private class IntListIterator implements Iterator { + //private fields: + private int i; + + public 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() + { + if (i >= size) + { + throw new NoSuchElementException("i is now out of bounds"); + } + int currentValue = buffer[i]; + i++; + + return currentValue; + } + } + + // iterators are what enables main/client to use a for-each loop + // on my IntList } From 4c9ea4f45396155fc34144fd502d8b67417b5a20 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Tue, 9 Jan 2024 14:35:37 -0800 Subject: [PATCH 05/32] Created thirdList IntList and printed/iterated through lists. --- src/Main.java | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/Main.java b/src/Main.java index 9cfea34..cc05de6 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,26 +1,40 @@ +import java.util.Iterator; + //TIP To Run code, press or // click the icon in the gutter. public class Main { public static void main(String[] args) { //TIP Press with your caret at the highlighted text // to see how IntelliJ IDEA suggests fixing it. -// System.out.printf("Hello and welcome!"); -// -// //you cannot create an object of an interface, -// //but you can create a class that implements -// //the methods from the interface to create an object -// IntList firstList; -// -// ArrayIntList secondList = new ArrayIntList(); -// -// IntList thirdList = new ArrayIntList(); - - ArrayIntList yes = new ArrayIntList(); - for (int each : yes) + System.out.printf("Hello and welcome!"); + + //you cannot create an object of an interface, + //but you can create a class that implements + //the methods from the interface to create an object + IntList firstList; + + ArrayIntList secondList = new ArrayIntList(); + + IntList thirdList = new ArrayIntList(); + thirdList.addFront(15); + thirdList.addFront(12); + thirdList.addBack(8); + + + // where an iterator gets used: + for (int value : thirdList) { - System.out.println(each); + 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); } + // 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 aafd12ebb8bf43b5f6ad741572a63538f9d0fe3a Mon Sep 17 00:00:00 2001 From: William Castillo Date: Tue, 16 Jan 2024 14:17:37 -0800 Subject: [PATCH 06/32] Created DoublyLinkedIntList class, with methods from IntList Interface --- src/DoublyLinkedIntList.java | 202 +++++++++++++++++++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 src/DoublyLinkedIntList.java diff --git a/src/DoublyLinkedIntList.java b/src/DoublyLinkedIntList.java new file mode 100644 index 0000000..017b1f0 --- /dev/null +++ b/src/DoublyLinkedIntList.java @@ -0,0 +1,202 @@ +import java.util.Iterator; + +public class DoublyLinkedIntList implements IntList +{ + //private fields + int size; + + private class Node { + int data; + Node next; //address of the node "after" this one in line + Node prev; //address of the node "before" this one in line + + public Node() { + next = null; + prev = null; + } + } + + private Node head; + private Node tail; + + public DoublyLinkedIntList() { + // an empty list has 3 sentinel (dummy) nodes that serve as bookends + head = new Node(); + tail = new Node(); + head.next = tail; + tail.prev = head; + 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 theLastOne = tail.prev; + + // set up my new node and fill it out (data, prev, next) + Node theNewOne = new Node(); + theNewOne.data = value; + theNewOne.next = tail; + theNewOne.prev = theLastOne; + + //go to the end of the list's sentinel, and update its prev + tail.prev = theNewOne; + + //go to the node before the new one, and update its next + theLastOne.next = theNewOne; + + 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) + { + + } + + /** + * 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 bfa86ecd49216eefa5ef6fa715903c895bf04d63 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Tue, 16 Jan 2024 14:36:23 -0800 Subject: [PATCH 07/32] Started DoublyLinkedIntList, filled in private fields, constructor, addBack, removeBack --- src/DoublyLinkedIntList.java | 25 +++-- src/LinkedIntList.java | 192 +++++++++++++++++++++++++++++++++++ 2 files changed, 211 insertions(+), 6 deletions(-) create mode 100644 src/LinkedIntList.java diff --git a/src/DoublyLinkedIntList.java b/src/DoublyLinkedIntList.java index 017b1f0..cf1c8fa 100644 --- a/src/DoublyLinkedIntList.java +++ b/src/DoublyLinkedIntList.java @@ -8,11 +8,11 @@ public class DoublyLinkedIntList implements IntList private class Node { int data; Node next; //address of the node "after" this one in line - Node prev; //address of the node "before" this one in line + Node previous; //address of the node "before" this one in line public Node() { next = null; - prev = null; + previous = null; } } @@ -24,7 +24,7 @@ public DoublyLinkedIntList() { head = new Node(); tail = new Node(); head.next = tail; - tail.prev = head; + tail.previous = head; size = 0; } @@ -39,16 +39,16 @@ public DoublyLinkedIntList() { @Override public void addFront(int value) { - Node theLastOne = tail.prev; + Node theLastOne = tail.previous; // set up my new node and fill it out (data, prev, next) Node theNewOne = new Node(); theNewOne.data = value; theNewOne.next = tail; - theNewOne.prev = theLastOne; + theNewOne.previous = theLastOne; //go to the end of the list's sentinel, and update its prev - tail.prev = theNewOne; + tail.previous = theNewOne; //go to the node before the new one, and update its next theLastOne.next = theNewOne; @@ -100,7 +100,20 @@ public void removeFront() @Override public void removeBack() { + if (size > 0) { + //set up a temporary variable for convenience + Node theOneToRemove = tail.previous; + theOneToRemove.previous.next = tail; + tail.previous = theOneToRemove.previous; + + //optional, but strongly recommended to clean up + theOneToRemove.next = null; + theOneToRemove.previous = null; + theOneToRemove.data = 0; + + size--; + } } /** diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java new file mode 100644 index 0000000..1d414a9 --- /dev/null +++ b/src/LinkedIntList.java @@ -0,0 +1,192 @@ +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 field + private Node head; + + // set up the size field + private int size; + + //add a constructor to initialize + 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 currently 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 33df7d2eed84939508f4d6b7775f08e6639a2ad5 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Mon, 22 Jan 2024 11:03:46 -0600 Subject: [PATCH 08/32] Completed removeBack, indexOf, isEmpty --- src/ArrayIntList.java | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java index e93c836..aca26bf 100644 --- a/src/ArrayIntList.java +++ b/src/ArrayIntList.java @@ -3,6 +3,7 @@ public class ArrayIntList implements IntList { + private int size; private int[] buffer; @@ -82,13 +83,22 @@ 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]; + } + + //optional, but a good idea - since we shifted everything to the left by 1 + // we want to clear out the right-most value to be zero + buffer[size - 1] = 0; + + size--; + } // for loop to shift elements to the left // buffer[0] = buffer[1] // buffer[1] = buffer[2] - for (int i = 0; i < size - 1; i++) - { - buffer[i] = buffer[i + 1]; - } + } /** @@ -98,7 +108,10 @@ public void removeFront() @Override public void removeBack() { - buffer[size - 1] = 0; + if (!isEmpty()) { + buffer[size - 1] = 0; + size--; + } } /** @@ -176,7 +189,17 @@ public boolean contains(int value) @Override public int indexOf(int value) { - return 0; + //run through the array using a for loop + for (int i = 0; i < size; i++) + { + //if the buffer[] == value, return the index + if (buffer[i] == value) + { + return i; + } + } + //otherwise, return a -1 + return -1; } /** @@ -187,7 +210,7 @@ public int indexOf(int value) @Override public boolean isEmpty() { - return false; + return size == 0; } /** From 81d404cff2232074360c11854170e39372b00cbb Mon Sep 17 00:00:00 2001 From: William Castillo Date: Mon, 22 Jan 2024 11:08:49 -0600 Subject: [PATCH 09/32] Completed contains() --- src/ArrayIntList.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java index aca26bf..926256d 100644 --- a/src/ArrayIntList.java +++ b/src/ArrayIntList.java @@ -175,6 +175,13 @@ 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; } @@ -189,6 +196,11 @@ public boolean contains(int value) @Override public int indexOf(int value) { + if (isEmpty()) + { + return -1; + } + //run through the array using a for loop for (int i = 0; i < size; i++) { From 4ffaa3ee85163f98b9314b7eaaad95837fc164a3 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Mon, 22 Jan 2024 12:52:09 -0600 Subject: [PATCH 10/32] Revision: addFront and add methods, Completed iterator method --- src/ArrayIntList.java | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java index 926256d..de31b33 100644 --- a/src/ArrayIntList.java +++ b/src/ArrayIntList.java @@ -3,7 +3,6 @@ public class ArrayIntList implements IntList { - private int size; private int[] buffer; @@ -24,15 +23,15 @@ public ArrayIntList() public void addFront(int value) { // start at 0, stop at the last element, increase by one - for (int i = 0; i <= size; i++) + for (int i = size; i >= 0; i--) { //shift over to the right buffer[i + 1] = buffer[i]; } - buffer[4] = buffer[3]; - buffer[3] = buffer[2]; - buffer[2] = buffer[1]; - buffer[1] = buffer[0]; +// buffer[4] = buffer[3]; +// buffer[3] = buffer[2]; +// buffer[2] = buffer[1]; +// buffer[1] = buffer[0]; // put the value at the front of the array at position 0 buffer[0] = value; @@ -73,6 +72,8 @@ public void add(int index, int value) { resize(size * 2); } + buffer[index] = value; + size++; } /** @@ -84,7 +85,7 @@ public void add(int index, int value) public void removeFront() { if (!isEmpty()) { - for (int i = 0; i < size - 2; i++) + for (int i = 0; i < size; i++) { buffer[i] = buffer[i + 1]; } @@ -253,7 +254,7 @@ public void clear() //size = 0; } - private void resize(int newSize) + public void resize(int newSize) { //create new space, separate from the old space (buffer) int[] newBuffer = new int[newSize]; @@ -279,7 +280,8 @@ private void resize(int newSize) @Override public Iterator iterator() { - return null; + IntListIterator theIterator = new IntListIterator(); + return theIterator; } //create a private helper Iterator class From 68bc8143eeb5962cb388e8f468f8889866499a78 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Mon, 22 Jan 2024 12:52:36 -0600 Subject: [PATCH 11/32] Implemented Test Cases --- src/Main.java | 114 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 95 insertions(+), 19 deletions(-) diff --git a/src/Main.java b/src/Main.java index cc05de6..e73521d 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,3 +1,4 @@ +import java.util.Arrays; import java.util.Iterator; //TIP To Run code, press or @@ -6,33 +7,97 @@ public class Main { public static void main(String[] args) { //TIP Press with your caret at the highlighted text // to see how IntelliJ IDEA suggests fixing it. - System.out.printf("Hello and welcome!"); + System.out.println("Hello and welcome!"); - //you cannot create an object of an interface, - //but you can create a class that implements - //the methods from the interface to create an object - IntList firstList; + // create ArrayIntList + ArrayIntList testList = new ArrayIntList(); - ArrayIntList secondList = new ArrayIntList(); + //add methods + testList.add(0, 1); + testList.add(1, 3); + testList.add(2, 5); + Iterator itr = testList.iterator(); + System.out.println("add():"); + printArray(testList, itr); + System.out.println(); - IntList thirdList = new ArrayIntList(); - thirdList.addFront(15); - thirdList.addFront(12); - thirdList.addBack(8); + System.out.println("addFront()"); + testList.addFront(6); + System.out.println("Front: " + testList.get(0)); + printArray(testList, itr); + System.out.println(); + System.out.println("addBack()"); + testList.addBack(50); + printArray(testList, itr); - // where an iterator gets used: - for (int value : thirdList) + System.out.println(); + System.out.println("removeFront()"); + testList.removeFront(); + printArray(testList, itr); + + System.out.println(); + System.out.println("removeBack()"); + testList.removeBack(); + printArray(testList, itr); + + System.out.println(); + System.out.println("remove()"); + testList.remove(1); + printArray(testList, itr); + + System.out.println(); + System.out.println("get()"); + System.out.println(testList.get(1)); + + System.out.println(); + System.out.println("contains()"); + System.out.println("Contains 10: " + testList.contains(10)); + System.out.println("Contains 1: " + testList.contains(1)); + + System.out.println(); + System.out.println("indexOf()"); + System.out.println("Index of 1: " + testList.indexOf(1)); + System.out.println("Index of 5: " + testList.indexOf(5)); + System.out.println("Index of 3: " + testList.indexOf(3)); + + System.out.println(); + System.out.println("isEmpty()"); + ArrayIntList emptyList = new ArrayIntList(); + System.out.println("emptyList: " + emptyList.isEmpty()); + System.out.println("testList: " + testList.isEmpty()); + + System.out.println(); + System.out.println("size()"); + System.out.println("Size of testList: " + testList.size()); + + System.out.println(); + System.out.println("resize()"); + System.out.println("currentList: " + testList.size()); + testList.resize(20); + for (int i = 0; i <= 20; i++) { - System.out.println(value); + testList.add(i, 4); } + System.out.println(testList.size()); + + + + +// // where an iterator gets used: +// 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); +// } + - //alternate way to use an iterator - Iterator itr = thirdList.iterator(); - while (itr.hasNext()) { - int value = itr.next(); - System.out.println(value); - } // for (int i = 1; i <= 5; i++) { @@ -42,4 +107,15 @@ public static void main(String[] args) { // } } + + public static void printArray(ArrayIntList list, Iterator iterator) + { + System.out.print("testList = {"); + iterator = list.iterator(); + while (iterator.hasNext()) + { + System.out.print(iterator.next() + ", "); + } + System.out.println("}"); + } } \ No newline at end of file From 46f985a589aff46b4b8610d7d04a7ab9e3a3eeb5 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Mon, 22 Jan 2024 14:27:28 -0600 Subject: [PATCH 12/32] Finished addFront, addBack, add, removeFront, removeBack, and remove methods --- src/LinkedIntList.java | 125 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 120 insertions(+), 5 deletions(-) diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java index 1d414a9..1cdeb9f 100644 --- a/src/LinkedIntList.java +++ b/src/LinkedIntList.java @@ -1,4 +1,5 @@ import java.util.Iterator; +import java.util.NoSuchElementException; public class LinkedIntList implements IntList { @@ -10,6 +11,7 @@ private class Node { // set up the head field private Node head; + private Node current; // set up the size field private int size; @@ -19,6 +21,7 @@ public LinkedIntList() { head = null; size = 0; + current = null; } /** @@ -37,12 +40,15 @@ public void addFront(int value) if (head == null) { // the list is currently empty head = theNewOne; + head.data = value; size++; } else { // the list currently has some nodes in it theNewOne.next = head; head = theNewOne; + head.data = value; + size++; } } @@ -54,7 +60,24 @@ public void addFront(int value) @Override public void addBack(int value) { + //create a current to traverse list + //create new node + Node current = head; + Node node = new Node(); + + node.data = value; + //if the next doesn't equals null, + //continue + //otherwise, assign the current.next to the new value + if (current.next != null) + { + current = current.next; + } + else + { + current.next = node; + } } /** @@ -69,7 +92,32 @@ public void addBack(int value) @Override public void add(int index, int value) { + //keep track of current + Node current = head; + int currentIndex = 0; + Node newNode = new Node(); + newNode.data = value; + //invalid index + if (index < 0) + { + throw new IndexOutOfBoundsException("Index is out of range"); + } + + //if head is null + if (head == null) + { + head.data = value; + } + else + { + while (currentIndex != index) + { + current = current.next; + currentIndex++; + } + current = newNode; + } } /** @@ -80,7 +128,11 @@ public void add(int index, int value) @Override public void removeFront() { - + if (head == null) + { + throw new NoSuchElementException("The list is empty"); + } + head = head.next; } /** @@ -90,7 +142,12 @@ public void removeFront() @Override public void removeBack() { - + Node current = head; + while (current.next != null) + { + current = current.next; + } + current = null; } /** @@ -105,7 +162,17 @@ public void removeBack() @Override public int remove(int index) { - return 0; + int currentIndex = 0; + Node current = head; + Node previous = current; + + while (currentIndex != index) + { + previous = current; + current = current.next; + } + previous = current.next; + } /** @@ -118,7 +185,7 @@ public int remove(int index) @Override public int get(int index) { - return 0; + } /** @@ -187,6 +254,54 @@ public void clear() @Override public Iterator iterator() { - return null; + SinglyLinkedIterator theIterator = new SinglyLinkedIterator(); + return theIterator; +// 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() + { + //return current != null; <----- another way to write this + if (current == 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 no next one to go to!!"); + } + int item = current.data; + current = current.next; + return item; + } } } From 805389d2cf5b7f6128275e85927bbdff8d5918c9 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Mon, 22 Jan 2024 14:48:52 -0600 Subject: [PATCH 13/32] Finished remaining methods: get, contains, indexOf, isEmpty, size, and clear --- src/LinkedIntList.java | 68 ++++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java index 1cdeb9f..94da877 100644 --- a/src/LinkedIntList.java +++ b/src/LinkedIntList.java @@ -11,7 +11,6 @@ private class Node { // set up the head field private Node head; - private Node current; // set up the size field private int size; @@ -21,7 +20,6 @@ public LinkedIntList() { head = null; size = 0; - current = null; } /** @@ -34,19 +32,21 @@ public LinkedIntList() @Override public void addFront(int value) { + //considerations: empty list + // set up a new node - Node theNewOne = new Node(); + Node newNode = new Node(); if (head == null) { // the list is currently empty - head = theNewOne; + head = newNode; head.data = value; size++; } else { // the list currently has some nodes in it - theNewOne.next = head; - head = theNewOne; + newNode.next = head; + head = newNode; head.data = value; size++; } @@ -60,14 +60,18 @@ public void addFront(int value) @Override public void addBack(int value) { + //considerations: empty list //create a current to traverse list //create new node Node current = head; Node node = new Node(); - node.data = value; - //if the next doesn't equals null, - //continue + + if (head == null) { + // the list is currently empty + head.data = node.data; + size++; + } //otherwise, assign the current.next to the new value if (current.next != null) @@ -92,9 +96,12 @@ public void addBack(int value) @Override public void add(int index, int value) { + //considerations: empty list or invalid index + //keep track of current Node current = head; int currentIndex = 0; + Node newNode = new Node(); newNode.data = value; @@ -116,7 +123,7 @@ public void add(int index, int value) current = current.next; currentIndex++; } - current = newNode; + current.data = newNode.data; } } @@ -128,11 +135,13 @@ public void add(int index, int value) @Override public void removeFront() { + Node current = head; + //consider: empty list if (head == null) { throw new NoSuchElementException("The list is empty"); } - head = head.next; + head = current.next; } /** @@ -170,6 +179,7 @@ public int remove(int index) { previous = current; current = current.next; + currentIndex++; } previous = current.next; @@ -185,7 +195,15 @@ public int remove(int index) @Override public int get(int index) { + Node current = head; + int currentIndex = 0; + while (currentIndex != index) + { + current = current.next; + currentIndex++; + } + return current.data; } /** @@ -197,7 +215,13 @@ public int get(int index) @Override public boolean contains(int value) { - return false; + Node current = head; + while (current.data != value && current.next != null) + { + current = current.next; + } + if (current.next == null) return false; + else return true; } /** @@ -211,7 +235,14 @@ public boolean contains(int value) @Override public int indexOf(int value) { - return 0; + Node current = head; + int currentIndex = 0; + while (current.data != value) + { + current = current.next; + currentIndex++; + } + return currentIndex; } /** @@ -222,7 +253,7 @@ public int indexOf(int value) @Override public boolean isEmpty() { - return false; + return head.next == null; } /** @@ -233,7 +264,12 @@ public boolean isEmpty() @Override public int size() { - return 0; + int size = 0; + while (iterator().hasNext()) + { + size++; + } + return size; } /** @@ -243,7 +279,7 @@ public int size() @Override public void clear() { - + head = null; } /** From bb511b03287252611f6520e31892296c5f830213 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Mon, 22 Jan 2024 14:50:15 -0600 Subject: [PATCH 14/32] Created Test class for LinkedIntList.java --- tests/LinkedIntListTest.java | 67 ++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tests/LinkedIntListTest.java diff --git a/tests/LinkedIntListTest.java b/tests/LinkedIntListTest.java new file mode 100644 index 0000000..4412332 --- /dev/null +++ b/tests/LinkedIntListTest.java @@ -0,0 +1,67 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class LinkedIntListTest +{ + + @Test + void addFront() + { + } + + @Test + void addBack() + { + } + + @Test + void add() + { + } + + @Test + void removeFront() + { + } + + @Test + void removeBack() + { + } + + @Test + void remove() + { + } + + @Test + void get() + { + } + + @Test + void contains() + { + } + + @Test + void indexOf() + { + } + + @Test + void isEmpty() + { + } + + @Test + void size() + { + } + + @Test + void clear() + { + } +} \ No newline at end of file From b967066fac3127c772a70ff6308ad65823381508 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Mon, 22 Jan 2024 15:52:26 -0600 Subject: [PATCH 15/32] Revised add method --- src/LinkedIntList.java | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java index 94da877..16b83b4 100644 --- a/src/LinkedIntList.java +++ b/src/LinkedIntList.java @@ -111,19 +111,26 @@ public void add(int index, int value) throw new IndexOutOfBoundsException("Index is out of range"); } - //if head is null + //if head is null - index 0 if (head == null) { head.data = value; + size++; } - else + //if index 1 is null, assign new node + else if (current.next == null) { - while (currentIndex != index) - { - current = current.next; - currentIndex++; - } - current.data = newNode.data; + current.next = newNode; + } + else //if head && current.next are not null + { + //move through until you get to the last + while (currentIndex != index && current.next != null) + { + current = current.next; + currentIndex++; + } + current.next = newNode; } } @@ -182,7 +189,7 @@ public int remove(int index) currentIndex++; } previous = current.next; - + return current.data; } /** @@ -264,8 +271,8 @@ public boolean isEmpty() @Override public int size() { - int size = 0; - while (iterator().hasNext()) + Iterator + while (LinkedIntList.hasNext()) { size++; } From 65ee54a3349c77639a3c53037daef4db2f2d9c8e Mon Sep 17 00:00:00 2001 From: William Castillo Date: Mon, 22 Jan 2024 16:04:03 -0600 Subject: [PATCH 16/32] Fixed addBack() --- src/LinkedIntList.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java index 16b83b4..858e9e6 100644 --- a/src/LinkedIntList.java +++ b/src/LinkedIntList.java @@ -74,14 +74,11 @@ public void addBack(int value) } //otherwise, assign the current.next to the new value - if (current.next != null) + while (current.next != null) { current = current.next; } - else - { - current.next = node; - } + current.next = node; } /** @@ -205,7 +202,7 @@ public int get(int index) Node current = head; int currentIndex = 0; - while (currentIndex != index) + while (currentIndex != index && current.next != null) { current = current.next; currentIndex++; @@ -271,12 +268,13 @@ public boolean isEmpty() @Override public int size() { - Iterator - while (LinkedIntList.hasNext()) + Node current = head; + while (current != null) { size++; + current = current.next; } - return size; + return size - 1; } /** From 26f392c3f0876a96007a6ede00d12df9d89e93b2 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Mon, 22 Jan 2024 16:50:36 -0600 Subject: [PATCH 17/32] Fixed add() --- src/LinkedIntList.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java index 858e9e6..853186e 100644 --- a/src/LinkedIntList.java +++ b/src/LinkedIntList.java @@ -121,13 +121,18 @@ else if (current.next == null) } else //if head && current.next are not null { + Node previousNode = current; + //move through until you get to the last while (currentIndex != index && current.next != null) { + previousNode = current; current = current.next; currentIndex++; } - current.next = newNode; + previousNode.next = newNode; + newNode.next = current.next; + //shift everything over by reassigning } } @@ -224,8 +229,7 @@ public boolean contains(int value) { current = current.next; } - if (current.next == null) return false; - else return true; + return true; } /** @@ -257,7 +261,7 @@ public int indexOf(int value) @Override public boolean isEmpty() { - return head.next == null; + return head == null; } /** @@ -297,7 +301,6 @@ public Iterator iterator() { SinglyLinkedIterator theIterator = new SinglyLinkedIterator(); return theIterator; -// return new SinglyLinkedIterator(); } //helper class/type that defines how the iterator works From 64438b270f1c576e3cb7dbaa6098fe08354deac3 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Mon, 22 Jan 2024 16:51:27 -0600 Subject: [PATCH 18/32] Completed tests for LinkedListTest.java --- tests/LinkedIntListTest.java | 58 ++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/LinkedIntListTest.java b/tests/LinkedIntListTest.java index 4412332..1dab0c7 100644 --- a/tests/LinkedIntListTest.java +++ b/tests/LinkedIntListTest.java @@ -1,67 +1,125 @@ import org.junit.jupiter.api.Test; +import org.junit.platform.engine.support.hierarchical.Node; + +import java.util.Random; import static org.junit.jupiter.api.Assertions.*; class LinkedIntListTest { + public LinkedIntList createList() + { + int sizeOfList = 5; + LinkedIntList list = new LinkedIntList(); + list.addFront(1); + + for (int i = 2; i < sizeOfList; i++) + { + Random rand = new Random(); + int randomNum = rand.nextInt(100); + list.add(i, randomNum); + } + return list; + } @Test void addFront() { + LinkedIntList linkedList = createList(); + linkedList.addFront(1); + assertEquals(1, linkedList.get(0)); } @Test void addBack() { + LinkedIntList linkedIntList = createList(); + linkedIntList.addBack(2); + assertEquals(2, linkedIntList.get(linkedIntList.size())); } @Test void add() { + LinkedIntList linkedIntList = createList(); + linkedIntList.add(3, 5); + assertEquals(5, linkedIntList.get(3)); } @Test void removeFront() { + LinkedIntList linkedIntList = createList(); + int newFront = linkedIntList.get(1); + linkedIntList.removeFront(); + assertEquals(linkedIntList.get(0), newFront); } @Test void removeBack() { + LinkedIntList linkedIntList = createList(); + int newLastNode = linkedIntList.get(linkedIntList.size() - 1); + linkedIntList.removeBack(); + assertEquals(linkedIntList.get(linkedIntList.size() - 1), newLastNode); } @Test void remove() { + LinkedIntList linkedIntList = createList(); + int removedNode = linkedIntList.get(2); + assertEquals(removedNode, linkedIntList.remove(2)); } @Test void get() { + LinkedIntList linkedIntList = createList(); + int retrievedData = linkedIntList.get(3); + assertEquals(retrievedData, linkedIntList.get(3)); } @Test void contains() { + LinkedIntList linkedIntList = createList(); + linkedIntList.add(linkedIntList.size(), 3); + assertEquals(true, linkedIntList.contains(3)); } @Test void indexOf() { + LinkedIntList fixedList = new LinkedIntList(); + fixedList.add(0, 1); + fixedList.add(1, 2); + fixedList.add(2, 3); + + fixedList.addBack(6); + assertEquals(fixedList.size() - 1, fixedList.indexOf(6)); } @Test void isEmpty() { + LinkedIntList emptyList = new LinkedIntList(); + assertEquals(true, emptyList.isEmpty()); } @Test void size() { + LinkedIntList linkedIntList = createList(); + assertEquals(4, linkedIntList.size()); } @Test void clear() { + LinkedIntList linkedIntList = createList(); + int size = linkedIntList.size(); + linkedIntList.clear(); + assertEquals(true, linkedIntList.isEmpty()); } } \ No newline at end of file From 472cbc3e0c5b6b319c00340451207f72f377fde2 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Mon, 22 Jan 2024 16:59:34 -0600 Subject: [PATCH 19/32] Completed Driver Test for ArrayIntList class --- IntListReview.iml | 17 ++++++++++++ src/{Main.java => ArrayIntListDriver.java} | 30 ++-------------------- src/DoublyLinkedIntList.java | 28 ++++++++++---------- src/IntList.java | 12 ++++++++- 4 files changed, 44 insertions(+), 43 deletions(-) rename src/{Main.java => ArrayIntListDriver.java} (79%) diff --git a/IntListReview.iml b/IntListReview.iml index c90834f..0f07f6c 100644 --- a/IntListReview.iml +++ b/IntListReview.iml @@ -4,8 +4,25 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/ArrayIntListDriver.java similarity index 79% rename from src/Main.java rename to src/ArrayIntListDriver.java index e73521d..cd81ded 100644 --- a/src/Main.java +++ b/src/ArrayIntListDriver.java @@ -1,9 +1,9 @@ -import java.util.Arrays; import java.util.Iterator; //TIP To Run code, press or // click the icon in the gutter. -public class Main { +public class ArrayIntListDriver +{ public static void main(String[] args) { //TIP Press with your caret at the highlighted text // to see how IntelliJ IDEA suggests fixing it. @@ -80,32 +80,6 @@ public static void main(String[] args) { testList.add(i, 4); } System.out.println(testList.size()); - - - - -// // where an iterator gets used: -// 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); -// } - - - - -// 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); -// } - } public static void printArray(ArrayIntList list, Iterator iterator) diff --git a/src/DoublyLinkedIntList.java b/src/DoublyLinkedIntList.java index cf1c8fa..74d739b 100644 --- a/src/DoublyLinkedIntList.java +++ b/src/DoublyLinkedIntList.java @@ -8,11 +8,11 @@ public class DoublyLinkedIntList implements IntList private class Node { int data; Node next; //address of the node "after" this one in line - Node previous; //address of the node "before" this one in line + Node last; //address of the node "before" this one in line public Node() { next = null; - previous = null; + last = null; } } @@ -24,7 +24,7 @@ public DoublyLinkedIntList() { head = new Node(); tail = new Node(); head.next = tail; - tail.previous = head; + tail.last = head; size = 0; } @@ -39,19 +39,19 @@ public DoublyLinkedIntList() { @Override public void addFront(int value) { - Node theLastOne = tail.previous; + Node previousNode = tail.last; // set up my new node and fill it out (data, prev, next) - Node theNewOne = new Node(); - theNewOne.data = value; - theNewOne.next = tail; - theNewOne.previous = theLastOne; + Node newNode = new Node(); + newNode.data = value; + newNode.next = tail; + newNode.last = previousNode; //go to the end of the list's sentinel, and update its prev - tail.previous = theNewOne; + tail.last = newNode; //go to the node before the new one, and update its next - theLastOne.next = theNewOne; + previousNode.next = newNode; size++; } @@ -102,14 +102,14 @@ public void removeBack() { if (size > 0) { //set up a temporary variable for convenience - Node theOneToRemove = tail.previous; + Node theOneToRemove = tail.last; - theOneToRemove.previous.next = tail; - tail.previous = theOneToRemove.previous; + theOneToRemove.last.next = tail; + tail.last = theOneToRemove.last; //optional, but strongly recommended to clean up theOneToRemove.next = null; - theOneToRemove.previous = null; + theOneToRemove.last = null; theOneToRemove.data = 0; size--; diff --git a/src/IntList.java b/src/IntList.java index 398c27b..0ffc8c3 100644 --- a/src/IntList.java +++ b/src/IntList.java @@ -3,18 +3,21 @@ * for an ordered (indexed) collection of ints, which * in mathematics is known as a sequence. */ -public interface IntList extends Iterable { +public interface IntList extends Iterable +{ /** * 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 */ 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 */ void addBack(int value); @@ -23,6 +26,7 @@ public interface IntList extends Iterable { * 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 @@ -46,6 +50,7 @@ public interface IntList extends Iterable { * 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 @@ -54,6 +59,7 @@ public interface IntList extends Iterable { /** * 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 @@ -62,6 +68,7 @@ public interface IntList extends Iterable { /** * 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 */ @@ -70,6 +77,7 @@ public interface IntList extends Iterable { /** * 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 @@ -78,12 +86,14 @@ public interface IntList extends Iterable { /** * Returns true if this list contains no values. + * * @return true if this list contains no values */ boolean isEmpty(); /** * Returns the number of values in this list. + * * @return the number of values in this list */ int size(); From 04b15b7d0f73ec3eb5514e60783e72c85d325f40 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Wed, 24 Jan 2024 12:57:20 -0800 Subject: [PATCH 20/32] Completed clear, size, and isEmpty methods --- src/DoublyLinkedIntList.java | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/DoublyLinkedIntList.java b/src/DoublyLinkedIntList.java index 74d739b..f9537ec 100644 --- a/src/DoublyLinkedIntList.java +++ b/src/DoublyLinkedIntList.java @@ -1,4 +1,5 @@ import java.util.Iterator; +import java.util.NoSuchElementException; public class DoublyLinkedIntList implements IntList { @@ -178,7 +179,7 @@ public int indexOf(int value) @Override public boolean isEmpty() { - return false; + return head == null; } /** @@ -189,7 +190,24 @@ public boolean isEmpty() @Override public int size() { - return 0; + //check if the list is empty + if (head == null) + { + throw new NoSuchElementException("The list is empty"); + } + + //use current to traverse linked list + //and keep count + Node current = head; + int count = 0; + + //move through until you get to the end + while (current != null) + { + current = current.next; + count++; + } + return count; } /** @@ -199,7 +217,7 @@ public int size() @Override public void clear() { - + head = null; } /** From 79731d3fa8b985c5d1b0921e660aa06817e5631f Mon Sep 17 00:00:00 2001 From: William Castillo Date: Wed, 24 Jan 2024 13:10:32 -0800 Subject: [PATCH 21/32] Completed get and contains methods --- src/DoublyLinkedIntList.java | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/DoublyLinkedIntList.java b/src/DoublyLinkedIntList.java index f9537ec..a8fe6bd 100644 --- a/src/DoublyLinkedIntList.java +++ b/src/DoublyLinkedIntList.java @@ -142,7 +142,26 @@ public int remove(int index) @Override public int get(int index) { - return 0; + //check if the index is out of bounds + //get the size and check the index + if (index > size || index < size) + { + throw new IndexOutOfBoundsException("The index is out of range"); + } + + //traverse list with current starting at the head + //and current index + Node current = head; + int currentIndex = 0; + + // while loop that traverses list and + // increments current index + while (index != currentIndex) + { + current = current.next; + currentIndex++; + } + return current.data; } /** @@ -154,7 +173,20 @@ public int get(int index) @Override public boolean contains(int value) { - return false; + // create current node + Node current = head; + + // also check if the next node is null + // run through list until you hit the value + while (current.data != value) + { + if (current.next == null) + { + return false; + } + current = current.next; + } + return true; } /** From dbd1bb947a68756fb3b4a423783481093d513f0f Mon Sep 17 00:00:00 2001 From: William Castillo Date: Wed, 24 Jan 2024 13:42:23 -0800 Subject: [PATCH 22/32] Completed addBack, add, indexOf methods --- src/DoublyLinkedIntList.java | 49 ++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/DoublyLinkedIntList.java b/src/DoublyLinkedIntList.java index a8fe6bd..09557b8 100644 --- a/src/DoublyLinkedIntList.java +++ b/src/DoublyLinkedIntList.java @@ -65,7 +65,16 @@ public void addFront(int value) @Override public void addBack(int value) { + //create node with new value + Node newNode = new Node(); + newNode.data = value; + //assign the next node of tail to new node + //reassign the tail to the new node + //increase size + tail.next = newNode; + tail = tail.next; + size++; } /** @@ -80,7 +89,32 @@ public void addBack(int value) @Override public void add(int index, int value) { + //throw out of bounds exception if out of range + if (index < size || index > size) + { + throw new IndexOutOfBoundsException("The given index is out of range"); + } + //keep track of index, current node, previous node, and create new node + int currentIndex = 0; + Node current = head; + Node newNode = new Node(); + newNode.data = value; + Node previousNode = current.last; + + //traverse list - while currentIndex does not equal the index + while (currentIndex != index) + { + previousNode = current; + current = current.next; + } + //if found, add the node by connecting the previous.next, next.previous + //to the new node + previousNode.next = newNode; + newNode.last = previousNode; + newNode.next = current.next; + current.next.last = newNode; + size++; } /** @@ -176,10 +210,11 @@ public boolean contains(int value) // create current node Node current = head; - // also check if the next node is null // run through list until you hit the value while (current.data != value) { + // check if the next node is null (this means we've hit the + // end of the list and the value does not exist if (current.next == null) { return false; @@ -200,7 +235,17 @@ public boolean contains(int value) @Override public int indexOf(int value) { - return 0; + // create current node and currentIndex + Node current = head; + int currentIndex = 0; + + // run through list until you hit the value + while (current.data != value) + { + current = current.next; + currentIndex++; + } + return currentIndex; } /** From fa03827a64d81b7e0c65e046ec49d1b85d9106c3 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Wed, 24 Jan 2024 13:54:17 -0800 Subject: [PATCH 23/32] Completed removeFront and remove methods --- src/DoublyLinkedIntList.java | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/DoublyLinkedIntList.java b/src/DoublyLinkedIntList.java index 09557b8..a81e9d1 100644 --- a/src/DoublyLinkedIntList.java +++ b/src/DoublyLinkedIntList.java @@ -125,7 +125,11 @@ public void add(int index, int value) @Override public void removeFront() { - + //change the head to head.next + //reassign the new heads last to node + head = head.next; + head.last = null; + size--; } /** @@ -163,7 +167,34 @@ public void removeBack() @Override public int remove(int index) { - return 0; + //index out of bounds + if (index > size || index < size) + { + throw new IndexOutOfBoundsException("Index is out of range"); + } + + //current index and current node + Node current = head; + int currentIndex = 0; + int returnValue = 0; + + //traverse through list and compare + while (currentIndex != index) + { + current = current.next; + currentIndex++; + } + //remove the node by reassign/rearranging the nodes + //previous.next to next node, + //next node to previous + returnValue = current.data; + current.last = current.next; + current.next.last = current.last; + + //reduce the size and return value + size--; + + return returnValue; } /** From 228fd301f744eca3d2e1067f02844146cca34095 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Wed, 24 Jan 2024 13:58:09 -0800 Subject: [PATCH 24/32] Completed DoublyLinkedIntList class - Test class needed --- src/DoublyLinkedIntList.java | 48 +++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/DoublyLinkedIntList.java b/src/DoublyLinkedIntList.java index a81e9d1..6cd139d 100644 --- a/src/DoublyLinkedIntList.java +++ b/src/DoublyLinkedIntList.java @@ -336,6 +336,52 @@ public void clear() @Override public Iterator iterator() { - return null; + DoublyLinkedIterator theIterator = new DoublyLinkedIterator(); + return theIterator; + } + + private class DoublyLinkedIterator implements Iterator{ + + private Node current; + + public DoublyLinkedIterator() { + 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() + { + //return current != null; <----- another way to write this + if (current == 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 no next one to go to!!"); + } + int item = current.data; + current = current.next; + return item; + } } } From d0b7f95fdf7bf7da3ff0c87bd6dc3dee7e189067 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Wed, 24 Jan 2024 13:59:51 -0800 Subject: [PATCH 25/32] Created Test class for DoublyLinkedIntList.java --- tests/DoublyLinkedIntListTest.java | 67 ++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tests/DoublyLinkedIntListTest.java diff --git a/tests/DoublyLinkedIntListTest.java b/tests/DoublyLinkedIntListTest.java new file mode 100644 index 0000000..a92c112 --- /dev/null +++ b/tests/DoublyLinkedIntListTest.java @@ -0,0 +1,67 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class DoublyLinkedIntListTest +{ + + @Test + void addFront() + { + } + + @Test + void addBack() + { + } + + @Test + void add() + { + } + + @Test + void removeFront() + { + } + + @Test + void removeBack() + { + } + + @Test + void remove() + { + } + + @Test + void get() + { + } + + @Test + void contains() + { + } + + @Test + void indexOf() + { + } + + @Test + void isEmpty() + { + } + + @Test + void size() + { + } + + @Test + void clear() + { + } +} \ No newline at end of file From 6c77ce0bf9557d31a31e939c8402cf15d047facf Mon Sep 17 00:00:00 2001 From: William Castillo Date: Wed, 24 Jan 2024 15:44:14 -0800 Subject: [PATCH 26/32] Revised and fixed addBack, add, and get methods --- tests/DoublyLinkedIntListTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/DoublyLinkedIntListTest.java b/tests/DoublyLinkedIntListTest.java index a92c112..1ddedcc 100644 --- a/tests/DoublyLinkedIntListTest.java +++ b/tests/DoublyLinkedIntListTest.java @@ -4,10 +4,24 @@ class DoublyLinkedIntListTest { + public DoublyLinkedIntList createList() + { + DoublyLinkedIntList list = new DoublyLinkedIntList(); + list.addFront(5); + list.add(1, 10); + list.add(2, 15); + list.add(3, 20); + list.addBack(25); + + return list; + } @Test void addFront() { + DoublyLinkedIntList list = createList(); + list.addFront(50); + assertEquals(50, list.get(0)); } @Test From 282092e74bcbde8a823bbfe8d5ea6ca689baddfd Mon Sep 17 00:00:00 2001 From: William Castillo Date: Wed, 24 Jan 2024 15:44:44 -0800 Subject: [PATCH 27/32] Revised and fixed addBack, add, and get methods --- src/DoublyLinkedIntList.java | 113 +++++++++++++++++++++++++---------- 1 file changed, 82 insertions(+), 31 deletions(-) diff --git a/src/DoublyLinkedIntList.java b/src/DoublyLinkedIntList.java index 6cd139d..c81b56b 100644 --- a/src/DoublyLinkedIntList.java +++ b/src/DoublyLinkedIntList.java @@ -40,21 +40,31 @@ public DoublyLinkedIntList() { @Override public void addFront(int value) { - Node previousNode = tail.last; - - // set up my new node and fill it out (data, prev, next) Node newNode = new Node(); + Node tempNode = head; newNode.data = value; - newNode.next = tail; - newNode.last = previousNode; - - //go to the end of the list's sentinel, and update its prev - tail.last = newNode; - //go to the node before the new one, and update its next - previousNode.next = newNode; - - size++; + //assign head to new value if no value is there + if (head.data == 0) + { + //assign the new node as the head + head = newNode; + //head as tail + tail = head; + //reference each other + size++; + } + // if there is, then assign + // head.last to the new node + // assign the new node to next + // and assign the head to new node + else + { + head.last = newNode; + head = newNode; + head.next = tempNode; + size++; + } } /** @@ -72,8 +82,13 @@ public void addBack(int value) //assign the next node of tail to new node //reassign the tail to the new node //increase size + + //current tail tail.next = newNode; - tail = tail.next; + + //new tail + newNode.last = tail; + tail = newNode; size++; } @@ -102,19 +117,48 @@ public void add(int index, int value) newNode.data = value; Node previousNode = current.last; - //traverse list - while currentIndex does not equal the index - while (currentIndex != index) + //if there's only one node + if (head == tail) { - previousNode = current; - current = current.next; + //reassign all nodes + //head reassignments + head.next = newNode; + + //new node reassignments + newNode.last = head; + newNode.next = tail; + + //tail + tail = newNode; + tail.last = head; + tail.next = new Node(); + size++; + } + else + { + //traverse list - while currentIndex does not equal the index + while (currentIndex != index) + { + previousNode = current; + current = current.next; + currentIndex++; + } + //if found, add the node by connecting all nodes to each other + //previous node + previousNode.next = newNode; + + //new node + newNode.last = previousNode; + if (current == null) + { + tail = newNode; + size++; + } + else { + newNode.next = current.next; + size++; + } } - //if found, add the node by connecting the previous.next, next.previous - //to the new node - previousNode.next = newNode; - newNode.last = previousNode; - newNode.next = current.next; - current.next.last = newNode; - size++; } /** @@ -209,7 +253,7 @@ public int get(int index) { //check if the index is out of bounds //get the size and check the index - if (index > size || index < size) + if (index > size) { throw new IndexOutOfBoundsException("The index is out of range"); } @@ -219,14 +263,21 @@ public int get(int index) Node current = head; int currentIndex = 0; - // while loop that traverses list and - // increments current index - while (index != currentIndex) + if (index == 0) { - current = current.next; - currentIndex++; + return head.data; + } + else + { + // while loop that traverses list and + // increments current index + while (index != currentIndex) + { + current = current.next; + currentIndex++; + } + return current.data; } - return current.data; } /** From 5ed4adffd01354f5e08fa72318fa41cfd2ed49f4 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Wed, 24 Jan 2024 15:47:30 -0800 Subject: [PATCH 28/32] Added addFront and addBack methods --- tests/DoublyLinkedIntListTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/DoublyLinkedIntListTest.java b/tests/DoublyLinkedIntListTest.java index 1ddedcc..6ae6bfa 100644 --- a/tests/DoublyLinkedIntListTest.java +++ b/tests/DoublyLinkedIntListTest.java @@ -27,6 +27,9 @@ void addFront() @Test void addBack() { + DoublyLinkedIntList list = createList(); + list.addBack(30); + assertEquals(30, list.get(list.size)); } @Test From b8e70e4e82166b44d25b0b8e26261aef5fa980ba Mon Sep 17 00:00:00 2001 From: William Castillo Date: Wed, 24 Jan 2024 15:47:52 -0800 Subject: [PATCH 29/32] Revised get method --- src/DoublyLinkedIntList.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/DoublyLinkedIntList.java b/src/DoublyLinkedIntList.java index c81b56b..7e7801d 100644 --- a/src/DoublyLinkedIntList.java +++ b/src/DoublyLinkedIntList.java @@ -267,6 +267,10 @@ public int get(int index) { return head.data; } + else if (index == size) + { + return tail.data; + } else { // while loop that traverses list and From 098c392a771480977854c169da4ad4026a11fe8b Mon Sep 17 00:00:00 2001 From: William Castillo Date: Wed, 24 Jan 2024 16:05:56 -0800 Subject: [PATCH 30/32] Completed DoublyLinkedList Tests. Next task: Adding multiple test cases --- tests/DoublyLinkedIntListTest.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/DoublyLinkedIntListTest.java b/tests/DoublyLinkedIntListTest.java index 6ae6bfa..92110e0 100644 --- a/tests/DoublyLinkedIntListTest.java +++ b/tests/DoublyLinkedIntListTest.java @@ -35,50 +35,76 @@ void addBack() @Test void add() { + DoublyLinkedIntList list = createList(); + list.add(5, 21); + assertEquals(21, list.get(5)); } @Test void removeFront() { + DoublyLinkedIntList list = createList(); + int newHead = list.get(1); + list.removeFront(); + assertEquals(newHead, list.get(0)); } @Test void removeBack() { + DoublyLinkedIntList list = createList(); + int newTail = list.get(list.size - 1); + list.removeBack(); + assertEquals(newTail, list.get(list.size)); } @Test void remove() { + DoublyLinkedIntList list = createList(); + assertEquals(list.get(2), list.remove(2)); } @Test void get() { + DoublyLinkedIntList list = createList(); + assertEquals(20, list.get(3)); } @Test void contains() { + DoublyLinkedIntList list = createList(); + assertEquals(true, list.contains(20)); } @Test void indexOf() { + DoublyLinkedIntList list = createList(); + assertEquals(4, list.indexOf(25)); } @Test void isEmpty() { + DoublyLinkedIntList list = createList(); + assertEquals(false, list.isEmpty()); } @Test void size() { + DoublyLinkedIntList list = createList(); + assertEquals(5, list.size()); } @Test void clear() { + DoublyLinkedIntList list = createList(); + list.clear(); + assertEquals(true, list.isEmpty()); } } \ No newline at end of file From 12a15a38443628925d281fb7e15cde5745ba94d3 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Wed, 24 Jan 2024 16:53:57 -0800 Subject: [PATCH 31/32] Completed Tests --- tests/DoublyLinkedIntListTest.java | 200 +++++++++++++++++++++++------ 1 file changed, 158 insertions(+), 42 deletions(-) diff --git a/tests/DoublyLinkedIntListTest.java b/tests/DoublyLinkedIntListTest.java index 92110e0..d7635de 100644 --- a/tests/DoublyLinkedIntListTest.java +++ b/tests/DoublyLinkedIntListTest.java @@ -4,107 +4,223 @@ class DoublyLinkedIntListTest { - public DoublyLinkedIntList createList() + private DoublyLinkedIntList emptyList; + private DoublyLinkedIntList fullList; + private DoublyLinkedIntList oneItemList; + private DoublyLinkedIntList twoItemList; + + public void createLists() { - DoublyLinkedIntList list = new DoublyLinkedIntList(); - list.addFront(5); - list.add(1, 10); - list.add(2, 15); - list.add(3, 20); - list.addBack(25); - - return list; + //empty list + emptyList = new DoublyLinkedIntList(); + + //full list + fullList = new DoublyLinkedIntList(); + fullList.addFront(5); + fullList.add(1, 10); + fullList.add(2, 15); + fullList.add(3, 20); + fullList.addBack(25); + + //one item list + oneItemList = new DoublyLinkedIntList(); + oneItemList.addFront(5); + + //two items + twoItemList = new DoublyLinkedIntList(); + twoItemList.addFront(5); + twoItemList.addBack(25); } @Test void addFront() { - DoublyLinkedIntList list = createList(); - list.addFront(50); - assertEquals(50, list.get(0)); + createLists(); + emptyList.addFront(7); + fullList.addFront(7); + oneItemList.addFront(7); + twoItemList.addFront(7); + + assertEquals(7, emptyList.get(0)); + assertEquals(7, fullList.get(0)); + assertEquals(7, oneItemList.get(0)); + assertEquals(7, twoItemList.get(0)); +// DoublyLinkedIntList list = createList(); +// list.addFront(50); +// assertEquals(50, list.get(0)); } @Test void addBack() { - DoublyLinkedIntList list = createList(); - list.addBack(30); - assertEquals(30, list.get(list.size)); + createLists(); + emptyList.addBack(1); + fullList.addBack(1); + oneItemList.addBack(1); + twoItemList.addBack(1); + + assertEquals(1, emptyList.get(emptyList.size)); + assertEquals(1, fullList.get(fullList.size)); + assertEquals(1, oneItemList.get(oneItemList.size)); + assertEquals(1, twoItemList.get(twoItemList.size)); + +// DoublyLinkedIntList list = createList(); +// list.addBack(30); +// assertEquals(30, list.get(list.size)); } @Test void add() { - DoublyLinkedIntList list = createList(); - list.add(5, 21); - assertEquals(21, list.get(5)); + createLists(); + +// emptyList.add(1, 3); -> throws IndexOutOfBoundException + fullList.add(1, 3); + oneItemList.add(1, 3); + twoItemList.add(1, 3); + + assertEquals(3, fullList.get(1)); + assertEquals(3, oneItemList.get(1)); + assertEquals(3, twoItemList.get(1)); + +// DoublyLinkedIntList list = createList(); +// list.add(5, 21); +// assertEquals(21, list.get(5)); } @Test void removeFront() { - DoublyLinkedIntList list = createList(); - int newHead = list.get(1); - list.removeFront(); - assertEquals(newHead, list.get(0)); + createLists(); + + int removedElementOne = fullList.get(0); + int removedElementTwo = oneItemList.get(0); + int removedElementThree = twoItemList.get(0); + int removedElement = 5; + +// assertEquals(emptyList); -> Throws exception + fullList.removeFront(); + oneItemList.removeFront(); + twoItemList.removeFront(); + + assertEquals(removedElement, removedElementOne); + assertEquals(removedElement, removedElementTwo); + assertEquals(removedElement, removedElementThree); + +// DoublyLinkedIntList list = createList(); +// int newHead = list.get(1); +// list.removeFront(); +// assertEquals(newHead, list.get(0)); } @Test void removeBack() { - DoublyLinkedIntList list = createList(); - int newTail = list.get(list.size - 1); - list.removeBack(); - assertEquals(newTail, list.get(list.size)); + createLists(); + +// emptyList.removeFront(); + fullList.removeBack(); + oneItemList.removeBack(); + twoItemList.removeBack(); + +// assertEquals(); +// DoublyLinkedIntList list = createList(); +// int newTail = list.get(list.size - 1); +// list.removeBack(); +// assertEquals(newTail, list.get(list.size)); } @Test void remove() { - DoublyLinkedIntList list = createList(); - assertEquals(list.get(2), list.remove(2)); + createLists(); + + assertEquals(5, fullList.remove(0)); + assertEquals(5, oneItemList.remove(0)); + assertEquals(5, twoItemList.remove(0)); +// DoublyLinkedIntList list = createList(); +// assertEquals(list.get(2), list.remove(2)); } @Test void get() { - DoublyLinkedIntList list = createList(); - assertEquals(20, list.get(3)); - } + createLists(); +// assertEquals(5, fullList.indexOf(0)); + assertEquals(5, fullList.get(0)); + assertEquals(5, oneItemList.get(0)); + assertEquals(5, twoItemList.get(0)); + } +// @Test void contains() { - DoublyLinkedIntList list = createList(); - assertEquals(true, list.contains(20)); + createLists(); + + assertEquals(true, fullList.contains(5)); + assertEquals(true, oneItemList.contains(5)); + assertEquals(true, twoItemList.contains(5)); + + +// DoublyLinkedIntList list = createList(); +// assertEquals(true, list.contains(20)); } @Test void indexOf() { - DoublyLinkedIntList list = createList(); - assertEquals(4, list.indexOf(25)); + createLists(); + +// assertEquals(1, emptyList); + assertEquals(0, fullList.indexOf(0)); + assertEquals(0, oneItemList.indexOf(0)); + assertEquals(0, twoItemList.indexOf(0)); + +// DoublyLinkedIntList list = createList(); +// assertEquals(4, list.indexOf(25)); } @Test void isEmpty() { - DoublyLinkedIntList list = createList(); - assertEquals(false, list.isEmpty()); + createLists(); + + assertEquals(true, emptyList); + assertEquals(false, fullList); + assertEquals(false, oneItemList); + assertEquals(false, twoItemList); +// DoublyLinkedIntList list = createList(); +// assertEquals(false, list.isEmpty()); } @Test void size() { - DoublyLinkedIntList list = createList(); - assertEquals(5, list.size()); + createLists(); + + assertEquals(0, emptyList.size()); + assertEquals(5, fullList.size()); + assertEquals(1, oneItemList.size()); + assertEquals(2, twoItemList.size()); +// DoublyLinkedIntList list = createList(); +// assertEquals(5, list.size()); } @Test void clear() { - DoublyLinkedIntList list = createList(); - list.clear(); - assertEquals(true, list.isEmpty()); + createLists(); + + emptyList.clear(); + fullList.clear(); + oneItemList.clear(); + twoItemList.clear(); + + assertEquals(true, emptyList.isEmpty()); + assertEquals(true, emptyList.isEmpty()); + assertEquals(true, emptyList.isEmpty()); + assertEquals(true, emptyList.isEmpty()); + } } \ No newline at end of file From 0a1769bf1cea8105f4ac2c62483e7be2eef51a32 Mon Sep 17 00:00:00 2001 From: William Castillo Date: Wed, 24 Jan 2024 16:54:06 -0800 Subject: [PATCH 32/32] Revised methods and completed class --- src/DoublyLinkedIntList.java | 44 ++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/src/DoublyLinkedIntList.java b/src/DoublyLinkedIntList.java index 7e7801d..69c63d3 100644 --- a/src/DoublyLinkedIntList.java +++ b/src/DoublyLinkedIntList.java @@ -45,7 +45,11 @@ public void addFront(int value) newNode.data = value; //assign head to new value if no value is there - if (head.data == 0) + if (head == null) + { + head = newNode; + } + else if (head.data == 0) { //assign the new node as the head head = newNode; @@ -79,6 +83,12 @@ public void addBack(int value) Node newNode = new Node(); newNode.data = value; + if (head == null && tail == null) + { + head = newNode; + tail = newNode; + } + //assign the next node of tail to new node //reassign the tail to the new node //increase size @@ -105,7 +115,7 @@ public void addBack(int value) public void add(int index, int value) { //throw out of bounds exception if out of range - if (index < size || index > size) + if (index > size || index == 0) { throw new IndexOutOfBoundsException("The given index is out of range"); } @@ -169,11 +179,23 @@ public void add(int index, int value) @Override public void removeFront() { - //change the head to head.next - //reassign the new heads last to node - head = head.next; - head.last = null; - size--; + if (head == null) + { + throw new NoSuchElementException("The list is empty"); + } + + if (size == 1) + { + head = null; + } + else + { + //change the head to head.next + //reassign the new heads last to node + head = head.next; + head.last = null; + size--; + } } /** @@ -183,7 +205,11 @@ public void removeFront() @Override public void removeBack() { - if (size > 0) { + if (head == null && tail == null) + { + throw new NoSuchElementException("The list is empty"); + } + else if (size > 0) { //set up a temporary variable for convenience Node theOneToRemove = tail.last; @@ -212,7 +238,7 @@ public void removeBack() public int remove(int index) { //index out of bounds - if (index > size || index < size) + if (index > size) { throw new IndexOutOfBoundsException("Index is out of range"); }