From 52e579a6660d8f53aacc3c54b24d246cceb95b31 Mon Sep 17 00:00:00 2001 From: Mason Date: Tue, 13 Feb 2024 10:12:09 -0800 Subject: [PATCH 1/5] Created ArrayIntList and LinkedIntList. Added addBack, toString to ArrayIntList --- src/ArrayIntList.java | 177 +++++++++++++++++++++++++++++++++++++++++ src/LinkedIntList.java | 147 ++++++++++++++++++++++++++++++++++ src/Main.java | 24 +++--- 3 files changed, 337 insertions(+), 11 deletions(-) create mode 100644 src/ArrayIntList.java create mode 100644 src/LinkedIntList.java diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java new file mode 100644 index 0000000..93073aa --- /dev/null +++ b/src/ArrayIntList.java @@ -0,0 +1,177 @@ +import java.util.Iterator; + +public class ArrayIntList implements IntList{ + //internal (private) representation + private int[] buffer; + private int size; // number of "spots used" in the int buffer + private final int INITIAL_CAPACITY = 10; + public ArrayIntList() { + buffer = new int[INITIAL_CAPACITY]; + 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) { + + } + + /** + * 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 still have room (capacity) + + buffer[size] = value; + size++; + } + + /** + * Inserts the specified value at the specified position in this list. + * Shifts the value currently at that position (if any) and any subsequent + * values to the right. + * + * @param index index at which the specified value is to be inserted + * @param value value to be inserted + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public void add(int index, int value) { + + } + + /** + * Removes the value located at the front of the list + * (at index 0), if it is present. + * Shifts any subsequent values to the left. + */ + @Override + public void removeFront() { + + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() { + + } + + /** + * Removes the value at the specified position in this list. + * Shifts any subsequent values to the left. Returns the value + * that was removed from the list. + * + * @param index the index of the value to be removed + * @return the value previously at the specified position + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int remove(int index) { + return 0; + } + + /** + * Returns the value at the specified position in the list. + * + * @param index index of the value to return + * @return the value at the specified position in this list + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int get(int index) { + 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; + } + + @Override + public String toString() { + if (size == 0) { + return "[]"; + } + + StringBuilder sb = new StringBuilder(); + sb.append("["); + + for (int i = 0; i < size; i++) { + sb.append(buffer[i]); + sb.append(i == size-1 ? "" : ", "); + } + + sb.append("]"); + return sb.toString(); + } +} diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java new file mode 100644 index 0000000..4a20492 --- /dev/null +++ b/src/LinkedIntList.java @@ -0,0 +1,147 @@ +import java.util.Iterator; + +public class LinkedIntList implements IntList{ + /** + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) { + + } + + /** + * Appends (inserts) the specified value at the back of the list (at index size()-1). + * + * @param value value to be inserted + */ + @Override + public void addBack(int value) { + + } + + /** + * 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; + } +} diff --git a/src/Main.java b/src/Main.java index 930198c..5742c17 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,15 +1,17 @@ -//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!"); - - 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); - } + System.out.println("Hello and welcome!"); + + int[] arrayOfNumbers = new int[10]; + + IntList list1 = new ArrayIntList(); + IntList list2 = new LinkedIntList(); + + //adds 3 ints to the back of the list + list1.addBack(42); + list1.addBack(82); + list1.addBack(97); + + System.out.println(list1); } } \ No newline at end of file From d855333adc061da3268ce1da63655428043c9e87 Mon Sep 17 00:00:00 2001 From: Mason Date: Tue, 13 Feb 2024 10:53:28 -0800 Subject: [PATCH 2/5] Added removeBack() and addFront() methods --- src/ArrayIntList.java | 40 ++++++++++++++++++++++++++++++++++++++-- src/Main.java | 5 +++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java index 93073aa..94ab393 100644 --- a/src/ArrayIntList.java +++ b/src/ArrayIntList.java @@ -19,7 +19,33 @@ public ArrayIntList() { */ @Override public void addFront(int value) { + // check if full + if (size == buffer.length) { + resize(2*buffer.length); + } + + // open a spot at index 0 where value will be saved + // shift everything over to the right by 1 position + for (int i = size; i >= 1; i--) { + buffer[i] = buffer[i-1]; + } + + //put value in position[0] + buffer[0] = value; + size++; + } + + private void resize(int newSize) { + // create a new array that is of the new size + int[] temp = new int[newSize]; + // copy over values from the existing buffer + for (int i = 0; i < size; i++) { + temp[i] = buffer[i]; + } + + // make the switchover + buffer = temp; } /** @@ -29,7 +55,14 @@ public void addFront(int value) { */ @Override public void addBack(int value) { - // TODO: check to see if we still have room (capacity) + if (size == buffer.length) { + // if the size matches the capacity, then I know I'm "full" + // and I need to resize (create a new larger buffer and copy + // the values over from the older smaller buffer) + + // make the resize twice the existing capacity + resize(2*buffer.length); + } buffer[size] = value; size++; @@ -65,7 +98,10 @@ public void removeFront() { */ @Override public void removeBack() { - + if(size==0) { + throw new IllegalStateException("ArrayList is empty!"); + } + buffer[--size] = 0; } /** diff --git a/src/Main.java b/src/Main.java index 5742c17..057cd0d 100644 --- a/src/Main.java +++ b/src/Main.java @@ -13,5 +13,10 @@ public static void main(String[] args) { list1.addBack(97); System.out.println(list1); + + list1.removeBack(); + list1.addFront(32); + + System.out.println(list1); } } \ No newline at end of file From 9b3aa4e82595b26d26c3bc969a36ccf8c1578aef Mon Sep 17 00:00:00 2001 From: Mason Date: Tue, 27 Feb 2024 12:18:57 -0800 Subject: [PATCH 3/5] Added most methods and tests --- IntListReview.iml | 32 +++++ src/ArrayIntList.java | 77 +++++++++++- src/ArrayIntListTest.java | 124 +++++++++++++++++++ src/LinkedIntList.java | 236 +++++++++++++++++++++++++++++++++++-- src/LinkedIntListTest.java | 94 +++++++++++++++ src/Main.java | 68 +++++++++++ 6 files changed, 617 insertions(+), 14 deletions(-) create mode 100644 src/ArrayIntListTest.java create mode 100644 src/LinkedIntListTest.java diff --git a/IntListReview.iml b/IntListReview.iml index c90834f..ff79d70 100644 --- a/IntListReview.iml +++ b/IntListReview.iml @@ -7,5 +7,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java index 94ab393..7e8f606 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{ //internal (private) representation @@ -14,6 +15,8 @@ public ArrayIntList() { * 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. + *

+ * Slow, linear time O(size) - shifts size items right * * @param value value to be inserted */ @@ -35,6 +38,7 @@ public void addFront(int value) { size++; } + // slow method, linear time O(n) private void resize(int newSize) { // create a new array that is of the new size int[] temp = new int[newSize]; @@ -50,6 +54,9 @@ private void resize(int newSize) { /** * Appends (inserts) the specified value at the back of the list (at index size()-1). + *

+ * Fast, constant time if no resize, O(1) + * can be slow if resize is needed * * @param value value to be inserted */ @@ -72,6 +79,10 @@ 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. + *

+ * worst case add at index and resize - O(n) - linear

+ * best case add at index size - - - - O(1) - constant

+ * average case - - - - - - - - - - - - O(1/2n) * * @param index index at which the specified value is to be inserted * @param value value to be inserted @@ -85,16 +96,26 @@ 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. + * slow, constant time, O(size)/O(n) */ @Override public void removeFront() { + // shift everything to the left by 1 + for (int i = 0; i < size; i++) { + buffer[i] = buffer[i+1]; + } + // lower size + size--; } /** * Removes the value located at the back of the list * (at index size()-1), if it is present. + *

+ * fast, constant time, O(1) */ @Override public void removeBack() { @@ -108,6 +129,10 @@ 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. + *

+ * worst case remove at index 0 and resize - O(n) - linear

+ * best case remove at index size - - - - - O(1) - constant

+ * average case - - - - - - - - - - - - - - O(1/2n) * * @param index the index of the value to be removed * @return the value previously at the specified position @@ -120,6 +145,7 @@ public int remove(int index) { /** * Returns the value at the specified position in the list. + * This method is fast, constant time or also known as, O(1). * * @param index index of the value to return * @return the value at the specified position in this list @@ -127,11 +153,13 @@ public int remove(int index) { */ @Override public int get(int index) { - return 0; + return buffer[index]; } /** * Returns true if this list contains the specified value. + *

+ * W.C. when return false or at end of list - linear time O(n) * * @param value value whose presence in this list is to be searched for * @return true if this list contains the specified value @@ -144,6 +172,8 @@ public boolean contains(int value) { /** * Returns the index of the first occurrence of the specified value * in this list, or -1 if this list does not contain the value. + *

+ * Fast, constant time, O(1) * * @param value value to search for * @return the index of the first occurrence of the specified value in this list @@ -156,22 +186,26 @@ public int indexOf(int value) { /** * Returns true if this list contains no values. + *

+ * Fast, constant time, O(1) * * @return true if this list contains no values */ @Override public boolean isEmpty() { - return false; + return size != 0; } /** * Returns the number of values in this list. + *

+ * This is a fast method, constant time, O(1) * * @return the number of values in this list */ @Override public int size() { - return 0; + return size; } /** @@ -190,9 +224,16 @@ public void clear() { */ @Override public Iterator iterator() { - return null; + return new ArrayIntListIterator(); } + /** + * Transfers the array into a string value + *

+ * O(n) linear time b/c visits every item + * + * @return string + */ @Override public String toString() { if (size == 0) { @@ -210,4 +251,30 @@ public String toString() { sb.append("]"); return sb.toString(); } -} + + // nested or inner class (helper class) + public class ArrayIntListIterator implements Iterator{ + private int currenPosition; + public ArrayIntListIterator() { + currenPosition = 0; + } + + @Override + public boolean hasNext() { + return currenPosition < size(); + } + + @Override + public Integer next() { + //just to be safe - make sure there is a next + if (!hasNext()) { + throw new NoSuchElementException(); + } + + int value = get(currenPosition); + currenPosition++; + return value; + } + } + +} // end of ArrayIntList diff --git a/src/ArrayIntListTest.java b/src/ArrayIntListTest.java new file mode 100644 index 0000000..791e169 --- /dev/null +++ b/src/ArrayIntListTest.java @@ -0,0 +1,124 @@ +import java.lang.reflect.Array; + +import static org.junit.jupiter.api.Assertions.*; + +class ArrayIntListTest { + + @org.junit.jupiter.api.Test + void addFront() { + } + + @org.junit.jupiter.api.Test + void addBackWithEmptyList() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(42); + assertEquals(theList.get(0), 42); + + } + @org.junit.jupiter.api.Test + void addBackWithNonEmptyList() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(1); + theList.addBack(2); + theList.addBack(3); + theList.addBack(42); + //assertEquals(theList.get(theList.size()-1), 42); + String out = theList.toString(); + assertEquals(out, "[1, 2, 3, 42]"); + } + @org.junit.jupiter.api.Test + void addBackWithResizedList() { + ArrayIntList theList = new ArrayIntList(); + // fill the list with INITIAL_CAPACITY or 10 items + for (int i = 0; i < 10; i++) { + theList.addBack(i); + } + // now add item 11 + theList.addBack(42); + assertEquals(theList.get(theList.size()-1), 42); + } + @org.junit.jupiter.api.Test + void addBackWithMultipleResizes() { + ArrayIntList theList = new ArrayIntList(); + // fill the list with INITIAL_CAPACITY or 10 items + for (int i = 0; i < 1000000; i++) { + theList.addBack(i); + } + // now add item 11 + theList.addBack(42); + assertEquals(theList.get(theList.size()-1), 42); + } + + @org.junit.jupiter.api.Test + void add() { + } + + @org.junit.jupiter.api.Test + void removeFront() { + } + + @org.junit.jupiter.api.Test + void removeBackFromEmptyList() { + ArrayIntList theList = new ArrayIntList(); + + assertThrowsExactly(IllegalStateException.class, + () -> {theList.removeBack();}); + } + @org.junit.jupiter.api.Test + void removeBackFromListOf10() { + ArrayIntList theList = new ArrayIntList(); + + //fill list with 10 items (0,1,2,3,4,5,6,7,8,9) + for (int i = 0; i < 10; i++) { + theList.addBack(i); + } + theList.removeBack(); + + String out = theList.toString(); + assertEquals(out, "[0, 1, 2, 3, 4, 5, 6, 7, 8]"); + } + @org.junit.jupiter.api.Test + void removeBackFromOneItemList() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(42); + theList.removeBack(); + assertEquals(0, theList.size()); + } + + + @org.junit.jupiter.api.Test + void remove() { + } + + @org.junit.jupiter.api.Test + void get() { + } + + @org.junit.jupiter.api.Test + void contains() { + } + + @org.junit.jupiter.api.Test + void indexOf() { + } + + @org.junit.jupiter.api.Test + void isEmpty() { + } + + @org.junit.jupiter.api.Test + void size() { + } + + @org.junit.jupiter.api.Test + void clear() { + } + + @org.junit.jupiter.api.Test + void iterator() { + } + + @org.junit.jupiter.api.Test + void testToString() { + } +} \ No newline at end of file diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java index 4a20492..3242149 100644 --- a/src/LinkedIntList.java +++ b/src/LinkedIntList.java @@ -1,6 +1,32 @@ import java.util.Iterator; +import java.util.NoSuchElementException; public class LinkedIntList implements IntList{ + // helper inner/nested class + public class Node { + int data; // holds the data value + Node next; // holds address of next node + + public Node() { + data = 0; + next = null; + } + + public Node (int data, Node next) { + this.data = data; + this.next = next; + } + } // end of class Node + + // fields for LinkedIntList class + private Node head; //address of first node in list + private int size; // number of nodes/items in list + + 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 @@ -10,7 +36,19 @@ public class LinkedIntList implements IntList{ */ @Override public void addFront(int value) { - + /* + This does the same, in more depth: + // if the list is empty + if (head == null) { + head = new Node(value, null); + } + else { + // if the list is not empty + head = new Node(value, head); + } + */ + head = new Node(value, head); + size++; } /** @@ -20,7 +58,26 @@ public void addFront(int value) { */ @Override public void addBack(int value) { + // if list is empty + if (head == null) { + head = new Node(value, null); + + } else { + + // if list is not empty + Node current = head; + // loop and stop on last node in list + // (but not all the way to null) + while (current.next != null) { + // move current forward + current = current.next; + } + + // at end of list - current is referencing last node + current.next = new Node(value, null); + } + size++; } /** @@ -44,7 +101,12 @@ public void add(int index, int value) { */ @Override public void removeFront() { - + // if head empty, exit method + if (head == null) { + return; + } + head = head.next; + size--; } /** @@ -53,7 +115,23 @@ public void removeFront() { */ @Override public void removeBack() { + if (head == null) { + return; + } + if (head.next == null) { + head = null; + } else { + Node current = head; + + // move to second last node + while (current.next.next != null) { + current = current.next; + } + // make last node null + current.next = null; + } + size--; } /** @@ -67,7 +145,43 @@ public void removeBack() { */ @Override public int remove(int index) { - return 0; + if (head == null) { + throw new IndexOutOfBoundsException("Nothing in list!"); + } if (index == 0) { + // saving value + int data = head.data; + if (head.next != null) { + // can make it past head.next because it exists + head = head.next; + + } else { + // cannot make it past head.next because doesn't exist + head = null; + } + return data; + } + // start at 1, needs to have access to the index after current one + int i = 1; + Node current = head; + while (current.next != null) { + if (i == index) { + // save value, using i since we won't be moving anymore + i = current.next.data; + + if (current.next.next == null) { + // checking if we can skip over current.next, if not, make next = null + current.next = null; + + } else { + // if we can skip over current.next, skip + current.next = current.next.next; + } + return i; + } + current = current.next; + i++; + } + throw new IndexOutOfBoundsException("Index out of range"); } /** @@ -79,7 +193,19 @@ public int remove(int index) { */ @Override public int get(int index) { - return 0; + if (head == null) { + throw new IndexOutOfBoundsException("Nothing in list!"); + } + int i = 0; + Node current = head; + while (current != null) { + if (i == index) { + return current.data; + } + current = current.next; + i++; + } + throw new IndexOutOfBoundsException("Index out of range"); } /** @@ -90,6 +216,16 @@ public int get(int index) { */ @Override public boolean contains(int value) { + if (head == null) { + return false; + } + Node current = head; + while (current != null) { + if (current.data == value) { + return true; + } + current = current.next; + } return false; } @@ -103,7 +239,19 @@ public boolean contains(int value) { */ @Override public int indexOf(int value) { - return 0; + if (head == null) { + return -1; + } + int i = 0; + Node current = head; + while (current != null) { + if (current.data == value) { + return i; + } + current = current.next; + i++; + } + return -1; } /** @@ -113,7 +261,7 @@ public int indexOf(int value) { */ @Override public boolean isEmpty() { - return false; + return head == null; } /** @@ -123,7 +271,7 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + return size; } /** @@ -132,7 +280,8 @@ public int size() { */ @Override public void clear() { - + head = null; + size = 0; } /** @@ -142,6 +291,75 @@ public void clear() { */ @Override public Iterator iterator() { - return null; + return new LinkedIterator(); + } + + // helper method (not required, but nice example to reference) + public void print() { + // create a temp variable (almost like an index i) + // copy in the address from head and save it + Node current = head; + + while (current != null) { + // print the value inside the node + System.out.println(current.data); + + //go to the next node + current = current.next; + } + } + + @Override + public String toString() { + if (head == null) { + // if list is empty, indicate with [] + return "[]"; + } + + // list not empty... + StringBuilder sb = new StringBuilder(); + sb.append("["); + + // stop one before the last node + Node current = head; + while (current.next != null) { + sb.append(current.data); + sb.append(", "); + + current = current.next; + } + + // add in the last node + sb.append(current.data); + + sb.append("]"); + return sb.toString(); + } + + // helper class + public class LinkedIterator implements Iterator { + // keep track of current position + private Node current; // holds address of current node + + public LinkedIterator() { + // start the current position at the first node in list + current = head; + } + + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public Integer next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + + int result = current.data; + current = current.next; + return result; + } } } diff --git a/src/LinkedIntListTest.java b/src/LinkedIntListTest.java new file mode 100644 index 0000000..afadef7 --- /dev/null +++ b/src/LinkedIntListTest.java @@ -0,0 +1,94 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class LinkedIntListTest { + + @Test + void addFrontWithEmptyList() { + IntList testList = new LinkedIntList(); + testList.addFront(2); + + String out = testList.toString(); + assertEquals(out, "[2]"); + } + @Test + void addFrontWithNonEmptyList() { + IntList testList = new LinkedIntList(); + testList.addFront(1); + testList.addFront(2); + testList.addFront(3); + testList.addFront(4); + + String out = testList.toString(); + assertEquals(out, "[4, 3, 2, 1]"); + } + + @Test + void addBackWithEmptyList() { + IntList testList = new LinkedIntList(); + testList.addBack(2); + + String out = testList.toString(); + assertEquals(out, "[2]"); + } + @Test + void addBackWithNonEmptyList() { + IntList testList = new LinkedIntList(); + testList.addBack(1); + testList.addBack(2); + testList.addBack(3); + testList.addBack(4); + + String out = testList.toString(); + assertEquals(out, "[1, 2, 3, 4]"); + } + + @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() { + } + + @Test + void iterator() { + } + + @Test + void testToString() { + } +} \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 057cd0d..272f47a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,3 +1,5 @@ +import java.util.Iterator; + public class Main { public static void main(String[] args) { System.out.println("Hello and welcome!"); @@ -18,5 +20,71 @@ public static void main(String[] args) { list1.addFront(32); System.out.println(list1); + + // iterator short way + for(int value : list1) { + System.out.println(value); + } + + list1.removeFront(); + + System.out.println(list1); + + + // -------------------- + // Linked List short test + // -------------------- + + System.out.println("----------------------------"); + + list2.addFront(9); + list2.addFront(7); + list2.addFront(4); + list2.addBack(18); + + System.out.println(list2); + + // iterator long way + Iterator itr = list2.iterator(); + while (itr.hasNext()) { + System.out.println(itr.next()); + } + + System.out.println("Contains 9?: " + list2.contains(9)); + System.out.println("Contains 22?: " + list2.contains(22)); + + System.out.println("Index of 9: " + list2.indexOf(9)); + System.out.println("Index of 22: " + list2.indexOf(22)); + + System.out.println("Get value of index 2: " + list2.get(2)); + System.out.println("Get value of index 0: " + list2.get(0)); + + System.out.println("Show me value of removed index 2: " + list2.remove(2)); + System.out.println(list2); + + System.out.println(list2.remove(0)); + System.out.println(list2); + + list2.removeFront(); + System.out.println(list2); + + list2.removeBack(); + System.out.println(list2); + + list2.removeFront(); + System.out.println(list2); + + list2.removeFront(); + System.out.println(list2); + + System.out.println(list2.size()); + System.out.println(list2.isEmpty()); + + + list2.clear(); + + System.out.println(list2); + System.out.println(list2.isEmpty()); + } } \ No newline at end of file From 9f39cbe344092549fb5b02f0c253587efabda90a Mon Sep 17 00:00:00 2001 From: Mason Date: Tue, 19 Mar 2024 16:50:49 -0700 Subject: [PATCH 4/5] Added all methods and all types of add methods for ArrayIntList --- src/ArrayIntList.java | 50 +++++++++++++++++++++++-- src/ArrayIntListTest.java | 78 +++++++++++++++++++++++++++++++++++++-- src/LinkedIntList.java | 31 ++++++++++++++++ src/Main.java | 14 +++++++ 4 files changed, 167 insertions(+), 6 deletions(-) diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java index 7e8f606..2d16c6d 100644 --- a/src/ArrayIntList.java +++ b/src/ArrayIntList.java @@ -90,7 +90,27 @@ public void addBack(int value) { */ @Override public void add(int index, int value) { + // check if index is out of range + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index out of range!"); + } + // adding last item? + else if (size == index){ + addBack(value); + } + else { + // shifts all items over to right (if needed) + for (int i = size; i > index; i--) { + // resize + if (size == buffer.length) { + resize(2*buffer.length); + } + buffer[i] = buffer[i-1]; + } + buffer[index] = value; + } + size++; } /** @@ -140,7 +160,19 @@ public void removeBack() { */ @Override public int remove(int index) { - return 0; + if(size==0) { + throw new IllegalStateException("ArrayList is empty!"); + } else if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Out of index range!"); + } + + int savedValue = buffer[index]; + // shifts all other units to the left + for (int i = index; i < size; i++) { + buffer[i] = buffer[i+1]; + } + size--; + return savedValue; } /** @@ -166,6 +198,11 @@ 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; } @@ -181,7 +218,12 @@ public boolean contains(int value) { */ @Override public int indexOf(int value) { - return 0; + for (int i = 0; i < size; i++) { + if(buffer[i] == value) { + return i; + } + } + return -1; } /** @@ -211,10 +253,12 @@ public int size() { /** * Removes all the values from this list. * The list will be empty after this call returns. + * Fast method, constant time, O(1) */ @Override public void clear() { - + buffer = new int[INITIAL_CAPACITY]; + size = 0; } /** diff --git a/src/ArrayIntListTest.java b/src/ArrayIntListTest.java index 791e169..8dfd5a9 100644 --- a/src/ArrayIntListTest.java +++ b/src/ArrayIntListTest.java @@ -1,13 +1,38 @@ -import java.lang.reflect.Array; +import java.sql.Array; import static org.junit.jupiter.api.Assertions.*; class ArrayIntListTest { + // addFront() -------------------------------------------- @org.junit.jupiter.api.Test - void addFront() { + void addFrontWithEmptyList() { + ArrayIntList theList = new ArrayIntList(); + theList.addFront(42); + assertEquals(theList.get(0), 42); + } + @org.junit.jupiter.api.Test + void addFrontWithNonEmptyList() { + ArrayIntList theList = new ArrayIntList(); + theList.addFront(1); + theList.addFront(2); + theList.addFront(3); + theList.addFront(4); + theList.addFront(42); + String out = theList.toString(); + assertEquals(out, "[42, 4, 3, 2, 1]"); + } + @org.junit.jupiter.api.Test + void addFrontWithResizedList() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 0; i < 10; i++) { + theList.addFront(i); + } + theList.addFront(42); + assertEquals(theList.get(0), 42); } + // addBack() -------------------------------------------- @org.junit.jupiter.api.Test void addBackWithEmptyList() { ArrayIntList theList = new ArrayIntList(); @@ -49,14 +74,61 @@ void addBackWithMultipleResizes() { assertEquals(theList.get(theList.size()-1), 42); } + // add() ---------------------------------- @org.junit.jupiter.api.Test - void add() { + void addWithEmptyList() { + ArrayIntList theList = new ArrayIntList(); + theList.add(0,42); + assertEquals(theList.get(0), 42); + } + + @org.junit.jupiter.api.Test + void addAt10() { + ArrayIntList theList = new ArrayIntList(); + assertThrowsExactly(IndexOutOfBoundsException.class, + () -> {theList.add(10,42);}); + + } + + @org.junit.jupiter.api.Test + void addWithFullList() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(10); + theList.addBack(11); + theList.addBack(12); + theList.addBack(13); + theList.addBack(14); + theList.addBack(15); + theList.addBack(16); + theList.addBack(17); + theList.addBack(18); + theList.addBack(19); + theList.add(5,42); + assertEquals(theList.toString(), "[10, 11, 12, 13, 14, 42, 15, 16, 17, 18, 19]"); + } + + @org.junit.jupiter.api.Test + void addFirstWithFullList() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(10); + theList.addBack(11); + theList.addBack(12); + theList.addBack(13); + theList.addBack(14); + theList.addBack(15); + theList.addBack(16); + theList.addBack(17); + theList.addBack(18); + theList.addBack(19); + theList.add(0,42); + assertEquals(theList.toString(), "[42, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]"); } @org.junit.jupiter.api.Test void removeFront() { } + // removeBack() ------------------------------------------- @org.junit.jupiter.api.Test void removeBackFromEmptyList() { ArrayIntList theList = new ArrayIntList(); diff --git a/src/LinkedIntList.java b/src/LinkedIntList.java index 3242149..51c78c2 100644 --- a/src/LinkedIntList.java +++ b/src/LinkedIntList.java @@ -12,6 +12,7 @@ public Node() { next = null; } + // T = 2 is O(1) constant public Node (int data, Node next) { this.data = data; this.next = next; @@ -22,6 +23,9 @@ public Node (int data, Node next) { private Node head; //address of first node in list private int size; // number of nodes/items in list + /** + * T = 2 is O(1) constant + */ public LinkedIntList() { head = null; size = 0; @@ -32,6 +36,8 @@ public LinkedIntList() { * Shifts the value currently at the front of the list (if any) and any * subsequent values to the right. * + * T = 6 is O(1) constant + * * @param value value to be inserted */ @Override @@ -54,6 +60,10 @@ public void addFront(int value) { /** * Appends (inserts) the specified value at the back of the list (at index size()-1). * + * If list is empty (go into if). T = 7 which is O(1) constant time + * If list not empty (go into else). T = 2 * size + 8 which is O(n) linear time + * T = 2n + 8 + * * @param value value to be inserted */ @Override @@ -91,7 +101,23 @@ public void addBack(int value) { */ @Override public void add(int index, int value) { + if(head == null) { + throw new IndexOutOfBoundsException("Nothing in list!"); + } + else if (index > size) { + throw new IndexOutOfBoundsException("Index out of bounds!"); + } + else { + Node current = head; + // move before to the index node + for (int i = 1; i < index; i++) { + current = current.next; + } + // put in the node between the nodes + Node newNode = new Node(value, current.next); + current.next = newNode; + } } /** @@ -158,6 +184,7 @@ public int remove(int index) { // cannot make it past head.next because doesn't exist head = null; } + size--; return data; } // start at 1, needs to have access to the index after current one @@ -176,6 +203,7 @@ public int remove(int index) { // if we can skip over current.next, skip current.next = current.next.next; } + size--; return i; } current = current.next; @@ -187,6 +215,9 @@ public int remove(int index) { /** * Returns the value at the specified position in the list. * + * O(n) linear - to get an item at an index, I have to start at the + * head and walk up to the size positions over + * * @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 diff --git a/src/Main.java b/src/Main.java index 272f47a..62c46d5 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,3 +1,4 @@ +import java.util.Arrays; import java.util.Iterator; public class Main { @@ -9,11 +10,15 @@ public static void main(String[] args) { IntList list1 = new ArrayIntList(); IntList list2 = new LinkedIntList(); + System.out.println(Arrays.toString(arrayOfNumbers)); //adds 3 ints to the back of the list list1.addBack(42); list1.addBack(82); list1.addBack(97); + System.out.println(list1); + list1.remove(2); + System.out.println(list1); list1.removeBack(); @@ -44,6 +49,10 @@ public static void main(String[] args) { System.out.println(list2); + list2.add(2, 42); + + System.out.println(list2); + // iterator long way Iterator itr = list2.iterator(); while (itr.hasNext()) { @@ -87,4 +96,9 @@ public static void main(String[] args) { System.out.println(list2.isEmpty()); } + + public static void tryout(int one, int two, int three, int four, int five) { + int[] array = new int[5]; + array.toString(); + } } \ No newline at end of file From b110d0fd898e2fb50fff4088aa89971dfea9c4cf Mon Sep 17 00:00:00 2001 From: Mason Date: Tue, 19 Mar 2024 17:39:27 -0700 Subject: [PATCH 5/5] Finished all tests, fixed a few methods with the bugs found --- src/ArrayIntList.java | 21 +++- src/ArrayIntListTest.java | 200 +++++++++++++++++++++++++++++++++++--- 2 files changed, 202 insertions(+), 19 deletions(-) diff --git a/src/ArrayIntList.java b/src/ArrayIntList.java index 2d16c6d..945ac7e 100644 --- a/src/ArrayIntList.java +++ b/src/ArrayIntList.java @@ -124,7 +124,12 @@ else if (size == index){ public void removeFront() { // shift everything to the left by 1 for (int i = 0; i < size; i++) { - buffer[i] = buffer[i+1]; + // if the size is as large as buffer length, do something special + if(i == (buffer.length-1) && size == buffer.length) { + buffer[i] = 0; + } else { + buffer[i] = buffer[i + 1]; + } } // lower size @@ -162,14 +167,19 @@ public void removeBack() { public int remove(int index) { if(size==0) { throw new IllegalStateException("ArrayList is empty!"); - } else if (index < 0 || index > size) { + } else if (index < 0 || index > size-1) { throw new IndexOutOfBoundsException("Out of index range!"); } int savedValue = buffer[index]; // shifts all other units to the left for (int i = index; i < size; i++) { - buffer[i] = buffer[i+1]; + // if the size is as large as buffer length, do something special + if(i == (buffer.length-1) && size == buffer.length) { + buffer[i] = 0; + } else { + buffer[i] = buffer[i + 1]; + } } size--; return savedValue; @@ -185,6 +195,9 @@ public int remove(int index) { */ @Override public int get(int index) { + if (index < 0 || index > size-1 || size == 0) { + throw new IndexOutOfBoundsException("Out of index range!"); + } return buffer[index]; } @@ -235,7 +248,7 @@ public int indexOf(int value) { */ @Override public boolean isEmpty() { - return size != 0; + return size == 0; } /** diff --git a/src/ArrayIntListTest.java b/src/ArrayIntListTest.java index 8dfd5a9..095132a 100644 --- a/src/ArrayIntListTest.java +++ b/src/ArrayIntListTest.java @@ -1,4 +1,5 @@ import java.sql.Array; +import java.util.Iterator; import static org.junit.jupiter.api.Assertions.*; @@ -124,8 +125,37 @@ void addFirstWithFullList() { assertEquals(theList.toString(), "[42, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]"); } + // removeFront() ------------------------------------------ @org.junit.jupiter.api.Test - void removeFront() { + void removeFrontFromEmptyList() { + ArrayIntList theList = new ArrayIntList(); + + theList.removeFront(); + String out = theList.toString(); + assertEquals(out, "[]"); + } + @org.junit.jupiter.api.Test + void removeFrontFromListOf10() { + ArrayIntList theList = new ArrayIntList(); + + // fill list + for (int i = 0; i < 10; i++) { + theList.addFront(i); + } + theList.removeFront(); + + String out = theList.toString(); + assertEquals(out, "[8, 7, 6, 5, 4, 3, 2, 1, 0]"); + } + @org.junit.jupiter.api.Test + void removeFrontSizeTest() { + ArrayIntList theList = new ArrayIntList(); + + theList.addFront(42); + theList.removeFront(); + + String out = theList.toString(); + assertEquals(0, theList.size()); } // removeBack() ------------------------------------------- @@ -157,40 +187,180 @@ void removeBackFromOneItemList() { assertEquals(0, theList.size()); } - + // remove() ------------------------------------------------ @org.junit.jupiter.api.Test - void remove() { + void removeFromEmptyList() { + ArrayIntList theList = new ArrayIntList(); + assertThrowsExactly(IllegalStateException.class, + () -> {theList.remove(0);}); } - @org.junit.jupiter.api.Test - void get() { + void removeOutOfBoundsPositive() { + ArrayIntList theList = new ArrayIntList(); + theList.addFront(1); + assertThrowsExactly(IndexOutOfBoundsException.class, + () -> {theList.remove(1);}); } - @org.junit.jupiter.api.Test - void contains() { + void removeOutOfBoundsNegative() { + ArrayIntList theList = new ArrayIntList(); + theList.addFront(1); + assertThrowsExactly(IndexOutOfBoundsException.class, + () -> {theList.remove(-1);}); } - @org.junit.jupiter.api.Test - void indexOf() { + void removeFromListOf10() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 0; i < 10; i++) { + theList.addBack(i); + } + theList.remove(4); + String out = theList.toString(); + assertEquals(out, "[0, 1, 2, 3, 5, 6, 7, 8, 9]"); } - @org.junit.jupiter.api.Test - void isEmpty() { + void removeLastItemFromListOf10() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 0; i < 10; i++) { + theList.addBack(i); + } + theList.remove(9); + String out = theList.toString(); + assertEquals(out, "[0, 1, 2, 3, 4, 5, 6, 7, 8]"); } - + // get() --------------------------------------------------- @org.junit.jupiter.api.Test - void size() { + void getFromEmptyList() { + ArrayIntList theList = new ArrayIntList(); + assertThrowsExactly(IndexOutOfBoundsException.class, + () -> {theList.get(0);}); } - @org.junit.jupiter.api.Test - void clear() { + void getNegativeOutOfBounds() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(42); + assertThrowsExactly(IndexOutOfBoundsException.class, + () -> {theList.get(-1);}); + } + @org.junit.jupiter.api.Test + void getPositiveOutOfBounds() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(42); + assertThrowsExactly(IndexOutOfBoundsException.class, + () -> {theList.get(1);}); + } + @org.junit.jupiter.api.Test + void getMiddleOfList() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(1); + theList.addBack(2); + theList.addBack(42); + theList.addBack(4); + assertEquals(theList.get(2), 42); + } + // contains() --------------------------------------------- + @org.junit.jupiter.api.Test + void containsNotThere() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 0; i < 10; i++) { + theList.addBack(i); + } + assertFalse(theList.contains(42)); + } + @org.junit.jupiter.api.Test + void containsThere() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 0; i < 10; i++) { + theList.addBack(i); + } + assertTrue(theList.contains(4)); + } + // indexOf() -------------------------------------------- + @org.junit.jupiter.api.Test + void indexOfNotThere() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 0; i < 10; i++) { + theList.addBack(i); + } + assertEquals(theList.indexOf(42), -1); + } + @org.junit.jupiter.api.Test + void indexOfThere() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 0; i < 10; i++) { + theList.addBack(i); + } + assertEquals(theList.indexOf(2), 2); + } + // isEmpty() ------------------------------------------------ + @org.junit.jupiter.api.Test + void isEmptyEmpty() { + ArrayIntList theList = new ArrayIntList(); + assertTrue(theList.isEmpty()); + } + @org.junit.jupiter.api.Test + void isEmptyNotEmpty() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 0; i < 10; i++) { + theList.addBack(i); + } + assertFalse(theList.isEmpty()); + } + // size() ------------------------------------------- + @org.junit.jupiter.api.Test + void sizeEmpty() { + ArrayIntList theList = new ArrayIntList(); + assertEquals(theList.size(), 0); + } + @org.junit.jupiter.api.Test + void sizeTen() { + ArrayIntList theList = new ArrayIntList(); + for (int i = 0; i < 10; i++) { + theList.addBack(i); + } + assertEquals(theList.size(), 10); + } + // clear() --------------------------------------------------- + @org.junit.jupiter.api.Test + void clearSizeTest() { + ArrayIntList theList = new ArrayIntList(); + theList.clear(); + assertEquals(theList.size(), 0); + } + @org.junit.jupiter.api.Test + void clearListTest() { + ArrayIntList theList = new ArrayIntList(); + theList.clear(); + String out = theList.toString(); + assertEquals(out, "[]"); } @org.junit.jupiter.api.Test void iterator() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(1); + theList.addBack(2); + theList.addBack(3); + theList.addBack(4); + theList.addBack(5); + int total = 0; + Iterator itr = theList.iterator(); + while (itr.hasNext()) { + total = total+itr.next(); + } + assertEquals(total, 15); } @org.junit.jupiter.api.Test void testToString() { + ArrayIntList theList = new ArrayIntList(); + theList.addBack(1); + theList.addBack(2); + theList.addBack(3); + theList.addBack(4); + theList.addBack(5); + theList.removeFront(); + String out = theList.toString(); + assertEquals(out, "[2, 3, 4, 5]"); } } \ No newline at end of file