From 7058215c0664b007e227adcf435eacb9439cc0b0 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Sat, 10 Feb 2024 16:22:37 -0800 Subject: [PATCH 01/36] Implemented Method Headers/ --- src/ArrayList.java | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/ArrayList.java diff --git a/src/ArrayList.java b/src/ArrayList.java new file mode 100644 index 0000000..21151db --- /dev/null +++ b/src/ArrayList.java @@ -0,0 +1,66 @@ +public class ArrayList implements List{ + @java.lang.Override + public void addFront(E item) { + + } + + @java.lang.Override + public void addBack(E item) { + + } + + @java.lang.Override + public void add(int i, E item) { + + } + + @java.lang.Override + public E get(int i) { + return null; + } + + @java.lang.Override + public void set(int i, E item) { + + } + + @java.lang.Override + public E removeFront() { + return null; + } + + @java.lang.Override + public E removeBack() { + return null; + } + + @java.lang.Override + public void remove(E item) { + + } + + @java.lang.Override + public E remove(int i) { + return null; + } + + @java.lang.Override + public boolean contains(E item) { + return false; + } + + @java.lang.Override + public boolean isEmpty() { + return false; + } + + @java.lang.Override + public int size() { + return 0; + } + + @java.lang.Override + public java.util.Iterator iterator() { + return null; + } +} From 46506d4b2996bd732bd0e5b4bba2affbd4b4a06f Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Mon, 12 Feb 2024 13:26:34 -0800 Subject: [PATCH 02/36] Completed isEmpty() and size() methods --- src/ArrayList.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index 21151db..2059729 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -1,4 +1,6 @@ -public class ArrayList implements List{ +public class ArrayList implements List{ + + public int size; @java.lang.Override public void addFront(E item) { @@ -51,16 +53,16 @@ public boolean contains(E item) { @java.lang.Override public boolean isEmpty() { - return false; + return size == 0; } @java.lang.Override public int size() { - return 0; + return size; } @java.lang.Override - public java.util.Iterator iterator() { + public java.util.Iterator iterator() { return null; } } From de4050420029b0f537ac55c21719842bea826f84 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Mon, 12 Feb 2024 13:44:12 -0800 Subject: [PATCH 03/36] Completed addFront() method, ArrayList constructor, added fields --- src/ArrayList.java | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index 2059729..7c6e42f 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -1,9 +1,28 @@ public class ArrayList implements List{ - public int size; + // fields + private int size; + private E[] buffer; + + public ArrayList() + { + //initialize my fields + size = 0; + buffer = (E[]) new Object[10]; + } @java.lang.Override public void addFront(E item) { - + if(size == buffer.length) + { + resize(size * 2); + } + + for(int i = size; i >= 1; i--) + { + buffer[i] = buffer[i-1]; + } + buffer[0] = item; + size++; } @java.lang.Override From eeb879476a653740a9f45da3da70160a005a9d47 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Mon, 12 Feb 2024 13:49:46 -0800 Subject: [PATCH 04/36] Added resize() method --- src/ArrayList.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/ArrayList.java b/src/ArrayList.java index 7c6e42f..fb233ee 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -80,6 +80,20 @@ public int size() { return size; } + private void resize(int newSize) + { + E[] newBuffer = (E[])new Object[newSize]; + + for (int i = 0; i < size; i++) { + newBuffer[i] = buffer[i]; + } + //set the new space into the buffer + buffer = newBuffer; + + //the old space is no longer "pointed to" and will eventually + //be cleaned up by the garbage collector + } + @java.lang.Override public java.util.Iterator iterator() { return null; From a0767b8cbb262f092489a093fe224e103ff7b373 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Mon, 12 Feb 2024 14:00:10 -0800 Subject: [PATCH 05/36] Completed addBack() and add() methods --- src/ArrayList.java | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index fb233ee..b61c47d 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -27,12 +27,32 @@ public void addFront(E item) { @java.lang.Override public void addBack(E item) { + if(size == buffer.length) + { + resize(size * 2); + } + buffer[size] = item; + size++; } @java.lang.Override public void add(int i, E item) { - + if(i > size || i < 0) + { + throw new IndexOutOfBoundsException("Invalid index"); + } + if(size == buffer.length) + { + resize(size * 2); + } + if (i != size) { + for (int j = size; j >= i; j--) { + buffer[j] = buffer[j - 1]; + } + } + buffer[i] = item; + size++; } @java.lang.Override From 38556b69c703a445f984fcbd6db57065d2175d79 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Mon, 12 Feb 2024 14:48:45 -0800 Subject: [PATCH 06/36] Completed get() and set() methods. --- src/ArrayList.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index b61c47d..8eaa4e9 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -57,12 +57,20 @@ public void add(int i, E item) { @java.lang.Override public E get(int i) { - return null; + if(i > size) + { + throw new IndexOutOfBoundsException(); + } + return buffer[i]; } @java.lang.Override public void set(int i, E item) { - + if(i > size) + { + throw new IndexOutOfBoundsException("Invalid index given!"); + } + buffer[i] = item; } @java.lang.Override From 1d13e6a6c318ee20e960384b771aba203c0b58be Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Mon, 12 Feb 2024 17:22:11 -0800 Subject: [PATCH 07/36] Completed removeFront() and removeBack() methods. --- src/ArrayList.java | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index 8eaa4e9..172c687 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -1,3 +1,5 @@ +import java.util.NoSuchElementException; + public class ArrayList implements List{ // fields @@ -75,12 +77,29 @@ public void set(int i, E item) { @java.lang.Override public E removeFront() { - return null; + if(!isEmpty()) + { + E returnedItem = buffer[0]; + for (int i = 0; i <= size - 2; i++) { + buffer[i] = buffer[i + 1]; + } + buffer[size - 1] = null; + size--; + return returnedItem; + } + throw new NoSuchElementException("There are no elements to remove!"); } @java.lang.Override public E removeBack() { - return null; + if(!isEmpty()) + { + E returnedItem = buffer[size-1]; + buffer[size-1] = null; + size--; + return returnedItem; + } + throw new NoSuchElementException("There are no elements to remove!"); } @java.lang.Override From 05febbb2e16589fc23b1e41caa2c3922a4a2d6af Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Mon, 12 Feb 2024 17:32:47 -0800 Subject: [PATCH 08/36] Completed both remove() methods. --- src/ArrayList.java | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index 172c687..9544d45 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -104,12 +104,44 @@ public E removeBack() { @java.lang.Override public void remove(E item) { - + if(!contains(item)) + { + throw new NoSuchElementException("This element does not exist!"); + } + for (int i = 0; i < size-1; i++) { + if(buffer[i].equals(item)) + { + for (int j = i; j < size; j++) { + buffer[j] = buffer[j+1]; + } + //remove the final index value + buffer[size - 1] = null; + //decrement size + size--; + } + } } @java.lang.Override public E remove(int i) { - return null; + //check to see if index is valid + if(i >= size || i < 0) + { + throw new IndexOutOfBoundsException("Invalid index"); + } + // save a copy of the value to be returned later + E removedItem = buffer[i]; + + //shift values to the left + for (int j = i; j < size; j++) { + buffer[j] = buffer[j+1]; + } + //remove the final index value + buffer[size - 1] = null; + //decrement size + size--; + //return the removed value + return removedItem; } @java.lang.Override From 0ef7d8c468102a939c922a4c14ae84efbf8d5573 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Mon, 12 Feb 2024 17:49:30 -0800 Subject: [PATCH 09/36] Completed iterator() and contains() methods. --- src/ArrayList.java | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index 9544d45..a16f676 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -1,3 +1,4 @@ +import java.util.Iterator; import java.util.NoSuchElementException; public class ArrayList implements List{ @@ -146,6 +147,12 @@ public E remove(int i) { @java.lang.Override public boolean contains(E item) { + for (int i = 0; i < size; i++) { + if(buffer[i].equals(item)) + { + return true; + } + } return false; } @@ -174,7 +181,25 @@ private void resize(int newSize) } @java.lang.Override - public java.util.Iterator iterator() { - return null; + public Iterator iterator() { + return new Iterator() { + // fields + private int i; + @Override + public boolean hasNext() { + return i < size; + } + + @Override + public E next() { + if(i >= size) + { + throw new NoSuchElementException("i is out of bounds"); + } + E currentValue = buffer[i]; + i++; + return currentValue; + } + }; } } From 637f36bd830687ab3259bb1b757505e0ea4bcd29 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Mon, 12 Feb 2024 17:54:33 -0800 Subject: [PATCH 10/36] Added method headers, fields, and Node class. --- src/LinkedList.java | 83 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/LinkedList.java diff --git a/src/LinkedList.java b/src/LinkedList.java new file mode 100644 index 0000000..4595b46 --- /dev/null +++ b/src/LinkedList.java @@ -0,0 +1,83 @@ +import java.util.Iterator; + +public class LinkedList implements List{ + + // Node class + private class Node { + E data; + Node next; + } + + // fields + private Node head; + private int size; + + public LinkedList() { + head = null; + size = 0; + } + @Override + public void addFront(E item) { + + } + + @Override + public void addBack(E item) { + + } + + @Override + public void add(int i, E item) { + + } + + @Override + public E get(int i) { + return null; + } + + @Override + public void set(int i, E item) { + + } + + @Override + public E removeFront() { + return null; + } + + @Override + public E removeBack() { + return null; + } + + @Override + public void remove(E item) { + + } + + @Override + public E remove(int i) { + return null; + } + + @Override + public boolean contains(E item) { + return false; + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public int size() { + return 0; + } + + @Override + public Iterator iterator() { + return null; + } +} From db4c0d96915db7bcfc491b15f4a73b21b3da583c Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Mon, 12 Feb 2024 18:02:52 -0800 Subject: [PATCH 11/36] Completed all add methods. --- src/LinkedList.java | 88 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 85 insertions(+), 3 deletions(-) diff --git a/src/LinkedList.java b/src/LinkedList.java index 4595b46..447a516 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -18,17 +18,99 @@ public LinkedList() { } @Override public void addFront(E item) { - + Node newOne = new Node(); + newOne.data = item; + if(head == null) + { + // the list is currently empty + newOne.next = null; + head = newOne; + size++; + return; + } + // the list currently has some nodes in it + newOne.next = head; + head = newOne; + size++; } @Override public void addBack(E item) { - + Node newOne = new Node(); + newOne.data = item; + if(head == null) + { + // the list is currently empty + newOne.next = null; + head = newOne; + size++; + return; + } + // the list currently has some nodes in it + Node current = head; + while(current.next != null) + { + current = current.next; + } + current.next = newOne; + size++; } @Override public void add(int i, E item) { - +// throw exception if index given is out of bounds + if(i > size || i < 0) + { + throw new IndexOutOfBoundsException("Invalid index given!"); + } + if(head == null) + { + head = new Node(); + head.data = item; + head.next = null; + size++; + return; + } + // current Node to keep track of index + Node current = head; + // create a new Node + Node theNewOne = new Node(); + // set the new Nodes value + theNewOne.data = item; + // check if index is at the front of the LinkedList + if(i == 0) + { + theNewOne.next = head; + head = theNewOne; + size++; + return; + } + // check if index is at the end of the LinkedList + if(i == size()) + { + // loop to end of the list + while(current.next != null) + { + current = current.next; + } + // add the new Node + current.next = theNewOne; + size++; + return; + } + // else, for loop that stops one before new indexed Node + for (int j = 0; j < i-1; j++) { + current = current.next; + } + // save the old Node from that index so we can move it forward one space + Node nodeAfterAdded = current.next; + // put the new Node in current.next position + current.next = theNewOne; + // move current forward one + current = current.next; + // set the previous index Node forward one index position (e.g. index 3 Node would now become index 4) + current.next = nodeAfterAdded; + size++; } @Override From 6a357bda7b90dbb41ce3a0ae236f47a196b763d7 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Mon, 12 Feb 2024 18:10:19 -0800 Subject: [PATCH 12/36] Completed get() and set() methods. --- src/LinkedList.java | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/LinkedList.java b/src/LinkedList.java index 447a516..0ae1b7e 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -102,7 +102,7 @@ public void add(int i, E item) { for (int j = 0; j < i-1; j++) { current = current.next; } - // save the old Node from that index so we can move it forward one space + // save the old Node from that index, so we can move it forward one space Node nodeAfterAdded = current.next; // put the new Node in current.next position current.next = theNewOne; @@ -115,12 +115,28 @@ public void add(int i, E item) { @Override public E get(int i) { - return null; + if(i >= size || i < 0) + { + throw new IndexOutOfBoundsException("Invalid index given!"); + } + Node current = head; + for (int j = 0; j < i; j++) { + current = current.next; + } + return current.data; } @Override public void set(int i, E item) { - + if(i > size || i < 0) + { + throw new IndexOutOfBoundsException("Invalid index given!"); + } + Node current = head; + for (int j = 0; j < i; j++) { + current = current.next; + } + current.data = item; } @Override From 522fec41d820ae975fc68e89891830be3ef1598f Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Mon, 12 Feb 2024 18:18:15 -0800 Subject: [PATCH 13/36] Completed size() and isEmpty() methods. --- src/LinkedList.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/LinkedList.java b/src/LinkedList.java index 0ae1b7e..7f5ec6e 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -166,12 +166,12 @@ public boolean contains(E item) { @Override public boolean isEmpty() { - return false; + return size == 0; } @Override public int size() { - return 0; + return size; } @Override From 87f55de5da80f5be6f1cb4f5044a3d63a20956ee Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Mon, 12 Feb 2024 18:24:19 -0800 Subject: [PATCH 14/36] Completed LinkedList removeFront() and removeBack() methods. --- src/LinkedList.java | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/LinkedList.java b/src/LinkedList.java index 7f5ec6e..2dc6506 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -141,12 +141,34 @@ public void set(int i, E item) { @Override public E removeFront() { + if(size() > 0) + { + E returnedItem = head.data; + head = head.next; + size--; + return returnedItem; + } return null; } @Override public E removeBack() { - return null; + E returnedItem; + if(size <= 1) + { + returnedItem = head.data; + head = null; + size --; + return returnedItem; + } + Node current = head; + for (int i = 0; i < size()-2; i++) { + current = current.next; + } + returnedItem = current.next.data; + current.next = null; + size--; + return returnedItem; } @Override From c70fa582c232de263572935fee72a5a899d3ab4c Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Mon, 12 Feb 2024 19:18:48 -0800 Subject: [PATCH 15/36] Completed LinkedList remove() and contains() methods --- src/LinkedList.java | 63 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/src/LinkedList.java b/src/LinkedList.java index 2dc6506..6e74724 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -1,4 +1,5 @@ import java.util.Iterator; +import java.util.NoSuchElementException; public class LinkedList implements List{ @@ -173,16 +174,74 @@ public E removeBack() { @Override public void remove(E item) { - + if(!contains(item)) + { + throw new NoSuchElementException("Item does not exist!"); + } + Node current = head; + if(current.data.equals(item)) + { + head = head.next; + return; + } + while(current.next != null) + { + if(current.next.data.equals(item)) + { + current.next = current.next.next; + } + } } @Override public E remove(int i) { - return null; + // check if index is invalid + if(i >= size() || i < 0) + { + throw new IndexOutOfBoundsException("Invalid index given!"); + } + E dataToBeRemoved; + if(i == 0) + { + dataToBeRemoved = head.data; + head.data = null; + head = head.next; + size--; + return dataToBeRemoved; + } + // current Node to iterate through LinkedList + Node current = head; + // loop until one Node before our index + for (int j = 0; j < i-1; i++) { + current = current.next; + } + // save our value that will be removed + dataToBeRemoved = current.next.data; + // check if there are two Nodes ahead for easy shifting + if(size()-i > 1) + { + // move current.next an additional space forward + current.next = current.next.next; + size--; + return dataToBeRemoved; + } + // else, remove the next Node (there are no Nodes ahead of our index) + current.next = null; + size--; + return dataToBeRemoved; } @Override public boolean contains(E item) { + Node current = head; + while(current != null) + { + if(current.data.equals(item)) + { + return true; + } + current = current.next; + } return false; } From 8e8d311451d01cc58436bab364b8a0561a9366bf Mon Sep 17 00:00:00 2001 From: Shyesta Date: Tue, 13 Feb 2024 13:06:24 -0800 Subject: [PATCH 16/36] Added javadocs with Runtime analysis to LinkedList class --- src/LinkedList.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/LinkedList.java b/src/LinkedList.java index 6e74724..94e46aa 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -17,6 +17,13 @@ public LinkedList() { head = null; size = 0; } + + /** + * Runtime analysis: Worst case scenario, this would run at constant time + * since in every case, you only change the head reference to the new Node. + * There is no need to visit every Node. + * @param item the item to be added + */ @Override public void addFront(E item) { Node newOne = new Node(); @@ -35,6 +42,12 @@ public void addFront(E item) { size++; } + /** + * Runtime analysis: Worst case scenario, this would run at O(n) or linear time + * because you have to visit every Node to reach the back of the LinkedList + * and then assign a new Node to the back. + * @param item the item to be added + */ @Override public void addBack(E item) { Node newOne = new Node(); @@ -57,6 +70,14 @@ public void addBack(E item) { size++; } + /** + * Runtime analysis: Worst case scenario, this would be O(n), as in the worst case scenario, + * the index being added to could be the last index, meaning we would have to traverse the + * entire LinkedList to add the new Node. Best case scenario, it would be constant time, as + * the user could ask to put the new Node at index 0, meaning no iterating is required. + * @param i the index where the item should be added + * @param item the item to be added + */ @Override public void add(int i, E item) { // throw exception if index given is out of bounds From 521d2b8779c4e6675453c07231d7066a7ad87f77 Mon Sep 17 00:00:00 2001 From: Shyesta Date: Tue, 13 Feb 2024 14:00:47 -0800 Subject: [PATCH 17/36] Added more javadocs with Runtime analysis to LinkedList class --- src/LinkedList.java | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/LinkedList.java b/src/LinkedList.java index 94e46aa..4852ed7 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -135,6 +135,13 @@ public void add(int i, E item) { size++; } + /** + * Runtime analysis: Worst case scenario, this would be O(n) runtime because + * you might have to go to the end of the LinkedList to find an item, or + * not find the item at all. + * @param i the index where the item should be retrieved + * @return + */ @Override public E get(int i) { if(i >= size || i < 0) @@ -148,6 +155,14 @@ public E get(int i) { return current.data; } + /** + * Runtime analysis: Worst case scenario, this would be O(n) runtime. + * This is because the item you are wanting to set could be at + * the end of the list, or not there at all, meaning you + * iterate through each item in the LinkedList. + * @param i the index where the item should be saved + * @param item the item to be saved + */ @Override public void set(int i, E item) { if(i > size || i < 0) @@ -161,6 +176,12 @@ public void set(int i, E item) { current.data = item; } + /** + * Runtime analysis: This will always be constant time because + * removing the front Node does not vary. You simply move the head Node + * reference forward and that is it. + * @return + */ @Override public E removeFront() { if(size() > 0) @@ -173,6 +194,12 @@ public E removeFront() { return null; } + /** + * Runtime analysis: Worst case scenario, and best case scenario, + * this runs at O(n). This is because you always have to loop + * until you reach the end of the list to remove the last Node. + * @return + */ @Override public E removeBack() { E returnedItem; @@ -193,6 +220,12 @@ public E removeBack() { return returnedItem; } + /** + * Runtime analysis: Worst case scenario, this would be O(n). + * That is because you could iterate through the whole list + * only to find the item does not exist within the LinkedList. + * @param item the item to be removed + */ @Override public void remove(E item) { if(!contains(item)) @@ -266,11 +299,24 @@ public boolean contains(E item) { return false; } + /** + * Runtime analysis: This will always run in constant time because + * there is no variation based on size of the LinkedList. + * This is a simple 1 line return statement. + * @return + */ @Override public boolean isEmpty() { return size == 0; } + /** + * Runtime analysis: This will always run in constant time + * because there is no variation based on size of the LinkedList. + * This is a simple 1 line return statement to return the size + * of the LinkedList. + * @return + */ @Override public int size() { return size; From acd7107fdb9b48b13b02666967415f0f8bb2830a Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Wed, 14 Feb 2024 14:04:12 -0800 Subject: [PATCH 18/36] Updated .gitignore --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index f68d109..97a4001 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ out/ !**/src/main/**/out/ !**/src/test/**/out/ +/.idea/vcs.xml +/.idea/misc.xml ### Eclipse ### .apt_generated @@ -26,4 +28,5 @@ bin/ .vscode/ ### Mac OS ### -.DS_Store \ No newline at end of file +.DS_Store + From 94e54530b1d41dec26359e94bafe17f4b0a0d9ba Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Wed, 14 Feb 2024 15:01:21 -0800 Subject: [PATCH 19/36] Finished Runtime analysis --- src/LinkedList.java | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/LinkedList.java b/src/LinkedList.java index 4852ed7..f58af36 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -140,7 +140,7 @@ public void add(int i, E item) { * you might have to go to the end of the LinkedList to find an item, or * not find the item at all. * @param i the index where the item should be retrieved - * @return + * @return E */ @Override public E get(int i) { @@ -180,7 +180,7 @@ public void set(int i, E item) { * Runtime analysis: This will always be constant time because * removing the front Node does not vary. You simply move the head Node * reference forward and that is it. - * @return + * @return E */ @Override public E removeFront() { @@ -198,7 +198,7 @@ public E removeFront() { * Runtime analysis: Worst case scenario, and best case scenario, * this runs at O(n). This is because you always have to loop * until you reach the end of the list to remove the last Node. - * @return + * @return E */ @Override public E removeBack() { @@ -247,6 +247,14 @@ public void remove(E item) { } } + /** + * Runtime analysis: Worst case scenario, this would run at O(n) runtime. + * This is because the index given to the remove() method could be the + * very last index, meaning the method has to go individually node by + * node until it reaches the last node to remove. + * @param i the index where the item should be removed + * @return + */ @Override public E remove(int i) { // check if index is invalid @@ -285,6 +293,14 @@ public E remove(int i) { return dataToBeRemoved; } + /** + * Runtime analysis: Worst case scenario, this method will run until the + * end of the list, and not find the item it is searching for. + * This would make its runtime O(n), as it has to search through every + * item in the LinkedList. + * @param item the item to search for + * @return boolean + */ @Override public boolean contains(E item) { Node current = head; @@ -303,7 +319,7 @@ public boolean contains(E item) { * Runtime analysis: This will always run in constant time because * there is no variation based on size of the LinkedList. * This is a simple 1 line return statement. - * @return + * @return boolean */ @Override public boolean isEmpty() { @@ -315,7 +331,7 @@ public boolean isEmpty() { * because there is no variation based on size of the LinkedList. * This is a simple 1 line return statement to return the size * of the LinkedList. - * @return + * @return int */ @Override public int size() { @@ -324,6 +340,17 @@ public int size() { @Override public Iterator iterator() { - return null; + Node current = head; + return new Iterator() { + @Override + public boolean hasNext() { + return current.next != null; + } + + @Override + public E next() { + return current.next.data; + } + }; } } From 7835ae8d87a90ebdcf1ca4254796e1c701beeaa8 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Wed, 14 Feb 2024 15:01:44 -0800 Subject: [PATCH 20/36] Updated misc.xml --- .idea/misc.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 6f29fee..69ace3f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file From 1ed72397a5e0b1b90ff5622361bd6adc714db639 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Wed, 14 Feb 2024 18:19:51 -0800 Subject: [PATCH 21/36] Wrote unit tests for LinkedList class --- SDEV333-Term-Project.iml | 17 +++ src/LinkedList.java | 12 +- tests/LinkedListTest.java | 284 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 311 insertions(+), 2 deletions(-) create mode 100644 tests/LinkedListTest.java diff --git a/SDEV333-Term-Project.iml b/SDEV333-Term-Project.iml index c90834f..0f07f6c 100644 --- a/SDEV333-Term-Project.iml +++ b/SDEV333-Term-Project.iml @@ -4,8 +4,25 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/LinkedList.java b/src/LinkedList.java index f58af36..bd68154 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -203,7 +203,11 @@ public E removeFront() { @Override public E removeBack() { E returnedItem; - if(size <= 1) + if(size == 0) + { + return null; + } + if(size == 1) { returnedItem = head.data; head = null; @@ -236,6 +240,7 @@ public void remove(E item) { if(current.data.equals(item)) { head = head.next; + size--; return; } while(current.next != null) @@ -243,7 +248,10 @@ public void remove(E item) { if(current.next.data.equals(item)) { current.next = current.next.next; + size--; + return; } + current = current.next; } } @@ -274,7 +282,7 @@ public E remove(int i) { // current Node to iterate through LinkedList Node current = head; // loop until one Node before our index - for (int j = 0; j < i-1; i++) { + for (int j = 0; j < i-1; j++) { current = current.next; } // save our value that will be removed diff --git a/tests/LinkedListTest.java b/tests/LinkedListTest.java new file mode 100644 index 0000000..0cc0e68 --- /dev/null +++ b/tests/LinkedListTest.java @@ -0,0 +1,284 @@ +import static org.junit.jupiter.api.Assertions.*; + +class LinkedListTest { + + @org.junit.jupiter.api.Test + void addFront() { + LinkedList list = new LinkedList<>(); + assertEquals(0, list.size()); + list.addFront("Banana"); + assertEquals("Banana", list.get(0)); + assertEquals(1, list.size()); + list.addFront("Orange"); + assertEquals("Orange", list.get(0)); + assertEquals("Banana", list.get(1)); + assertEquals(2, list.size()); + list.addFront("Apple"); + list.addFront("Pear"); + list.addFront("Grapefruit"); + list.addFront("Grape"); + list.addFront("Lemon"); + list.addFront("Lime"); + list.addFront("Melon"); + assertEquals("Melon", list.get(0)); + assertEquals("Banana", list.get(list.size()-1)); + assertEquals(9, list.size()); + } + + @org.junit.jupiter.api.Test + void addBack() { + LinkedList list = new LinkedList<>(); + assertEquals(0, list.size()); + list.addBack("Banana"); + assertEquals("Banana", list.get(list.size()-1)); + assertEquals(1, list.size()); + list.addBack("Orange"); + assertEquals("Orange", list.get(list.size()-1)); + assertEquals("Banana", list.get(0)); + assertEquals(2, list.size()); + list.addBack("Apple"); + list.addBack("Pear"); + list.addBack("Grapefruit"); + list.addBack("Grape"); + list.addBack("Lemon"); + list.addBack("Lime"); + list.addBack("Melon"); + assertEquals("Melon", list.get(list.size()-1)); + assertEquals("Banana", list.get(0)); + assertEquals(9, list.size()); + } + + @org.junit.jupiter.api.Test + void add() { + LinkedList list = new LinkedList<>(); + list.add(0, "Banana"); + assertEquals(1, list.size()); + assertEquals("Banana", list.get(0)); + list.add(1, "Orange"); + assertEquals(2, list.size()); + assertEquals("Orange", list.get(1)); + list.add(0, "Pear"); + list.add(3, "Pineapple"); + list.add(4, "Kiwi"); + list.add(5, "Plum"); + list.add(6, "Peach"); + list.add(7, "Apple"); + list.add(8, "Grape"); + assertEquals(9, list.size()); + assertEquals("Pear", list.get(0)); + assertEquals("Banana", list.get(1)); + assertEquals("Orange", list.get(2)); + assertEquals("Plum", list.get(5)); + assertEquals("Peach", list.get(6)); + assertEquals("Apple", list.get(7)); + assertEquals("Grape", list.get(8)); + } + + @org.junit.jupiter.api.Test + void get() { + LinkedList list = new LinkedList<>(); + list.addBack("Banana"); + assertEquals("Banana", list.get(0)); + list.addBack("Apple"); + list.addBack("Pear"); + list.addBack("Peach"); + list.addBack("Pineapple"); + list.addBack("Orange"); + assertEquals("Orange", list.get(list.size()-1)); + list.removeBack(); + assertEquals("Pineapple", list.get(list.size()-1)); + assertEquals("Apple", list.get(1)); + assertEquals("Pear", list.get(2)); + } + + @org.junit.jupiter.api.Test + void set() { + LinkedList list = new LinkedList<>(); + list.addBack("Pear"); + assertEquals("Pear", list.get(0)); + list.set(0, "Peach"); + assertEquals("Peach", list.get(0)); + list.addBack("Pear"); + list.addBack("Apple"); + list.addBack("Plum"); + list.addBack("Kiwi"); + list.addBack("Orange"); + list.addBack("Pickle"); + list.addBack("Cucumber"); + assertEquals("Kiwi", list.get(4)); + list.set(4, "Pizza"); + assertEquals("Pizza", list.get(4)); + assertEquals("Pickle", list.get(6)); + list.set(6, "Coca Cola"); + assertEquals("Coca Cola", list.get(6)); + assertEquals("Cucumber", list.get(7)); + list.set(7, "Coconut"); + assertEquals("Coconut", list.get(7)); + } + + @org.junit.jupiter.api.Test + void removeFront() { + LinkedList list = new LinkedList<>(); + assertEquals(null, list.removeFront()); + list.add(0, "Banana"); + assertEquals("Banana", list.get(0)); + assertEquals(1, list.size()); + assertEquals("Banana", list.removeFront()); + list.add(0, "Pear"); + list.add(1, "Plum"); + assertEquals("Pear", list.removeFront()); + list.addBack("Peach"); + list.addBack("Orange"); + list.addBack("Pineapple"); + list.addBack("Kiwi"); + list.addBack("Pizza"); + list.addBack("Brownie"); + list.addBack("Pickle"); + list.addBack("Cucumber"); + assertEquals("Plum", list.removeFront()); + assertEquals("Peach", list.removeFront()); + assertEquals("Orange", list.removeFront()); + assertEquals("Pineapple", list.removeFront()); + assertEquals("Kiwi", list.removeFront()); + } + + @org.junit.jupiter.api.Test + void removeBack() { + LinkedList list = new LinkedList<>(); + assertEquals(null, list.removeBack()); + list.addBack("Peach"); + assertEquals("Peach", list.removeBack()); + list.addBack("Pear"); + list.addBack("Plum"); + assertEquals("Plum", list.removeBack()); + list.addBack("Pineapple"); + list.addBack("Pizza"); + list.addBack("Pickle"); + list.addBack("Crackers"); + list.addBack("Brownie"); + list.addBack("Cheese"); + list.addBack("Asparagus"); + assertEquals("Asparagus", list.removeBack()); + assertEquals("Cheese", list.removeBack()); + assertEquals("Brownie", list.removeBack()); + assertEquals("Crackers", list.removeBack()); + assertEquals("Pickle", list.removeBack()); + } + + @org.junit.jupiter.api.Test + void removeIndex() { // removes based on index given + LinkedList list = new LinkedList<>(); + list.addBack("Banana"); + assertEquals("Banana", list.remove(0)); + assertEquals(0, list.size()); + list.addBack("Peach"); + list.addBack("Pear"); + list.addBack("Pineapple"); + assertEquals("Pear", list.remove(1)); + list.addBack("Pizza"); + list.addBack("Pickle"); + list.addBack("Cucumber"); + list.addBack("Apple"); + list.addBack("Orange"); + assertEquals("Pickle", list.remove(3)); + assertEquals("Orange", list.remove(5)); + assertEquals("Apple", list.remove(4)); + } + + @org.junit.jupiter.api.Test + void removeItem() { // removes based on item given + LinkedList list = new LinkedList<>(); + list.addBack("Banana"); + list.remove("Banana"); + assertEquals(0, list.size()); + list.addBack("Banana"); + list.addBack("Peach"); + list.addBack("Orange"); + list.addBack("Pear"); + list.addBack("Banana"); + list.addBack("Pineapple"); + list.addBack("Carrot"); + list.addBack("Bread"); + assertEquals("Banana", list.get(0)); + list.remove("Banana"); + assertEquals("Peach", list.get(0)); + assertEquals("Banana", list.get(3)); + assertEquals("Pineapple", list.get(4)); + assertEquals("Carrot", list.get(5)); + assertEquals("Bread", list.get(list.size()-1)); + list.remove("Bread"); + assertEquals("Carrot", list.get(list.size()-1)); + } + + @org.junit.jupiter.api.Test + void contains() { + LinkedList list = new LinkedList<>(); + assertEquals(false, list.contains("")); + assertEquals(false, list.contains("Banana")); + assertEquals(false, list.contains("Orange")); + list.addBack("Banana"); + assertEquals(true, list.contains("Banana")); + list.addBack("Orange"); + assertEquals(true, list.contains("Orange")); + list.addBack("Pineapple"); + list.addBack("Lemon"); + list.addBack("Lime"); + list.addBack("Peach"); + list.addBack("Pear"); + list.addBack("Apple"); + list.addBack("Kiwi"); + assertEquals(true, list.contains("Lime")); + assertEquals(true, list.contains("Peach")); + assertEquals(true, list.contains("Kiwi")); + assertEquals(true, list.contains("Pear")); + assertEquals(true, list.contains("Pineapple")); + assertEquals(false, list.contains("Kumquat")); + assertEquals(false, list.contains("Pizza")); + list.remove("Pineapple"); + assertEquals(false, list.contains("Pineapple")); + } + + @org.junit.jupiter.api.Test + void isEmpty() { + LinkedList list = new LinkedList<>(); + assertEquals(true, list.isEmpty()); + list.addBack("Apple"); + assertEquals(false, list.isEmpty()); + list.addBack("Banana"); + assertEquals(false, list.isEmpty()); + list.addBack("Orange"); + list.addBack("Pear"); + list.addBack("Cucumber"); + list.addBack("Pizza"); + list.addBack("Brownies"); + list.addBack("Salad"); + list.addBack("Eggs"); + assertEquals(false, list.isEmpty()); + for (int i = 0; i < list.size(); i++) { + list.remove(i); + i--; + } + assertEquals(true, list.isEmpty()); + } + + @org.junit.jupiter.api.Test + void size() { + LinkedList list = new LinkedList<>(); + assertEquals(0, list.size()); + list.addBack("Banana"); + assertEquals(1, list.size()); + list.addBack("Orange"); + list.addBack("Grape"); + list.addBack("Pear"); + list.addBack("Apple"); + list.addBack("Melon"); + list.addBack("Passion fruit"); + assertEquals(7, list.size()); + list.remove("Melon"); + assertEquals(6, list.size()); + list.remove("Pear"); + assertEquals(5, list.size()); + list.remove("Banana"); + assertEquals(4, list.size()); + } +} \ No newline at end of file From 5ffbfdc2e1024fdfcce2136d885db51d6599c5a2 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Fri, 16 Feb 2024 13:26:27 -0800 Subject: [PATCH 22/36] Added runtime analysis for ArrayList class. --- src/ArrayList.java | 100 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/src/ArrayList.java b/src/ArrayList.java index a16f676..8d091af 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -13,6 +13,15 @@ public ArrayList() size = 0; buffer = (E[]) new Object[10]; } + + /** + * Runtime analysis: Worst case scenario, this will run at O(2n) time. + * This is because if we have a full buffer, we will need to resize. + * To resize, we go through every element and copy to a new buffer. + * We then have to addFront() by moving all elements over right one, + * and then add the item to the front. + * @param item the item to be added + */ @java.lang.Override public void addFront(E item) { if(size == buffer.length) @@ -28,6 +37,15 @@ public void addFront(E item) { size++; } + /** + * Runtime analysis: Worst case scenario, this will run at O(n). This is because + * if our buffer is full, we will have to resize. Resizing means visiting all + * elements in our original buffer and copying them over to a new one. + * Then we will add our new item to the end of our buffer. Best case scenario, + * this runs in constant time since we can just add an item to the back + * and increment size. + * @param item the item to be added + */ @java.lang.Override public void addBack(E item) { if(size == buffer.length) @@ -39,6 +57,15 @@ public void addBack(E item) { size++; } + /** + * Worst case scenario, this would run at O(2n) time. This is because + * if our buffer is full, we would need to resize, meaning we have to + * loop through the entire buffer to add all items to a new, larger buffer. + * After that, the index could be the first index meaning we have to shift + * every element over to the right one index. + * @param i the index where the item should be added + * @param item the item to be added + */ @java.lang.Override public void add(int i, E item) { if(i > size || i < 0) @@ -58,6 +85,13 @@ public void add(int i, E item) { size++; } + /** + * In every scenario, this would run in constant time. Since we are just + * returning an element located at in index, this is a single line return + * statement. There is no need to do a loop, traversal, or shifting of elements. + * @param i the index where the item should be retrieved + * @return + */ @java.lang.Override public E get(int i) { if(i > size) @@ -67,6 +101,13 @@ public E get(int i) { return buffer[i]; } + /** + * In every scenario, this would run in constant time. Since we are just + * changing a single elements value at an index, this is a single line, + * with no traversal, loop, or shifting of the buffer required. + * @param i the index where the item should be saved + * @param item the item to be saved + */ @java.lang.Override public void set(int i, E item) { if(i > size) @@ -76,6 +117,12 @@ public void set(int i, E item) { buffer[i] = item; } + /** + * In every scenario, this would run at O(n). This is because since we are + * removing the front element, we have to shift every element over to the + * left one. This requires a loop to visit every element in the buffer. + * @return + */ @java.lang.Override public E removeFront() { if(!isEmpty()) @@ -91,6 +138,14 @@ public E removeFront() { throw new NoSuchElementException("There are no elements to remove!"); } + /** + * Best and worst case scenario, if not accounting for the exception thrown, + * this would run in constant time. This is because we have no need to + * shift elements over since we are removing the final index. + * We also do not need to loop through any elements since we can + * remove the item from the index of size-1. + * @return + */ @java.lang.Override public E removeBack() { if(!isEmpty()) @@ -103,6 +158,16 @@ public E removeBack() { throw new NoSuchElementException("There are no elements to remove!"); } + /** + * Worst case scenario, this would be O(2n) runtime. This is because + * the worst case would be either that the item is at the front + * or at the back. If the item is at the front, we would find + * the item immediately, but then would need to shift every item + * over to the left. If the item is at the back, we still have to + * search through the entire buffer until we find it at the final + * index. + * @param item the item to be removed + */ @java.lang.Override public void remove(E item) { if(!contains(item)) @@ -123,6 +188,17 @@ public void remove(E item) { } } + /** + * Worst case scenario, this method will run at O(n). This is because the + * index it could be searching for could be the first index in the buffer. + * meaning it would have to reorder every index over to the left. + * Best case scenario, this would run at constant time, as + * it could be the very last index given, meaning it would have to do + * no reordering, or the index could not be valid, meaning an exception would + * be thrown. + * @param i the index where the item should be removed + * @return + */ @java.lang.Override public E remove(int i) { //check to see if index is valid @@ -145,6 +221,14 @@ public E remove(int i) { return removedItem; } + /** + * Worst case scenario, this will run at linear time or O(n). This is because + * the contains method could loop to the last element in the buffer and find + * the item, or it could loop to the end of the buffer and not find the item. + * Either way, it could potentially have to search through every index. + * @param item the item to search for + * @return + */ @java.lang.Override public boolean contains(E item) { for (int i = 0; i < size; i++) { @@ -156,16 +240,32 @@ public boolean contains(E item) { return false; } + /** + * This will always run in constant time since we are just returning + * a true or false based on if size is equal to 0. + * @return + */ @java.lang.Override public boolean isEmpty() { return size == 0; } + /** + * Runtime analysis: This will always run in constant time since we are just + * returning the variable size. + * @return + */ @java.lang.Override public int size() { return size; } + /** + * In every scenario, this would run at O(n). This is because we have to + * create a new buffer, and go through the old buffer to copy all the + * elements over to the new one. + * @param newSize + */ private void resize(int newSize) { E[] newBuffer = (E[])new Object[newSize]; From 022d5cb4dadc945b940efe2079103ad1f8ed6301 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Fri, 16 Feb 2024 13:27:39 -0800 Subject: [PATCH 23/36] Created ArrayList test method headers. --- tests/ArrayListTest.java | 54 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tests/ArrayListTest.java diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java new file mode 100644 index 0000000..43cfac2 --- /dev/null +++ b/tests/ArrayListTest.java @@ -0,0 +1,54 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ArrayListTest { + + @Test + void addFront() { + } + + @Test + void addBack() { + } + + @Test + void add() { + } + + @Test + void get() { + } + + @Test + void set() { + } + + @Test + void removeFront() { + } + + @Test + void removeBack() { + } + + @Test + void remove() { + } + + @Test + void testRemove() { + } + + @Test + void contains() { + } + + @Test + void isEmpty() { + } + + @Test + void size() { + } +} \ No newline at end of file From d51f980f717b7315401308e41efcf0bc1f85cdb3 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Fri, 16 Feb 2024 16:28:07 -0800 Subject: [PATCH 24/36] Completed unit tests for ArrayList class. --- src/ArrayList.java | 8 +- tests/ArrayListTest.java | 266 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 268 insertions(+), 6 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index 8d091af..fd01b5c 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -77,7 +77,7 @@ public void add(int i, E item) { resize(size * 2); } if (i != size) { - for (int j = size; j >= i; j--) { + for (int j = size; j > i; j--) { buffer[j] = buffer[j - 1]; } } @@ -174,10 +174,10 @@ public void remove(E item) { { throw new NoSuchElementException("This element does not exist!"); } - for (int i = 0; i < size-1; i++) { + for (int i = 0; i < size; i++) { if(buffer[i].equals(item)) { - for (int j = i; j < size; j++) { + for (int j = i; j < size-2; j++) { buffer[j] = buffer[j+1]; } //remove the final index value @@ -210,7 +210,7 @@ public E remove(int i) { E removedItem = buffer[i]; //shift values to the left - for (int j = i; j < size; j++) { + for (int j = i; j < size-2; j++) { buffer[j] = buffer[j+1]; } //remove the final index value diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java index 43cfac2..64b1987 100644 --- a/tests/ArrayListTest.java +++ b/tests/ArrayListTest.java @@ -6,49 +6,311 @@ class ArrayListTest { @Test void addFront() { + ArrayList list = new ArrayList<>(); + assertEquals(0, list.size()); + list.addFront("Banana"); + assertEquals("Banana", list.get(0)); + assertEquals(1, list.size()); + list.addFront("Apple"); + assertEquals("Apple", list.get(0)); + assertEquals(2, list.size()); + list.addFront("Cucumber"); + list.addFront("Pickle"); + list.addFront("Kiwi"); + list.addFront("Pizza"); + list.addFront("Grape"); + list.addFront("Melon"); + list.addFront("Passion fruit"); + list.addFront("Brownie"); + assertEquals(10, list.size()); + assertEquals("Brownie", list.get(0)); + assertEquals("Passion fruit", list.get(1)); + assertEquals("Melon", list.get(2)); + assertEquals("Grape", list.get(3)); + assertEquals("Banana", list.get(list.size()-1)); } @Test void addBack() { + ArrayList list = new ArrayList<>(); + assertEquals(0, list.size()); + list.addBack("Pickle"); + assertEquals(1, list.size()); + assertEquals("Pickle", list.get(list.size()-1)); + list.addBack("Cucumber"); + assertEquals(2, list.size()); + assertEquals("Cucumber", list.get(list.size()-1)); + list.addBack("Pear"); + list.addBack("Peach"); + list.addBack("Chicken"); + list.addBack("Lime"); + list.addBack("Lemon"); + list.addBack("Apple"); + list.addBack("Orange"); + list.addBack("Kiwi"); + assertEquals(10, list.size()); + assertEquals("Pear", list.get(2)); + assertEquals("Peach", list.get(3)); + assertEquals("Chicken", list.get(4)); + assertEquals("Lime", list.get(5)); + assertEquals("Lemon", list.get(6)); + assertEquals("Apple", list.get(7)); + assertEquals("Orange", list.get(8)); + assertEquals("Kiwi", list.get(9)); } @Test void add() { + ArrayList list = new ArrayList<>(); + assertEquals(0, list.size()); + list.add(0, "Pickle"); + assertEquals(1, list.size()); + list.add(1, "Cucumber"); + assertEquals("Pickle", list.get(0)); + assertEquals("Cucumber", list.get(1)); + assertEquals(2, list.size()); + list.add(0, "Pear"); + list.add(1, "Peach"); + assertEquals("Pear", list.get(0)); + assertEquals("Peach", list.get(1)); + assertEquals("Pickle", list.get(2)); + assertEquals("Cucumber", list.get(3)); + list.add(4, "Pizza"); + list.add(5, "Pasta"); + list.add(6, "Chicken"); + list.add(7, "Kiwi"); + list.add(8, "Melon"); + list.add(9, "Lemon"); + assertEquals("Lemon", list.get(9)); + assertEquals("Melon", list.get(8)); + assertEquals("Kiwi", list.get(7)); + assertEquals("Chicken", list.get(6)); + assertEquals("Pasta", list.get(5)); + assertEquals("Pizza", list.get(4)); } @Test void get() { + ArrayList list = new ArrayList<>(); + list.add(0, "Banana"); + assertEquals("Banana", list.get(0)); + list.add(1, "Kiwi"); + assertEquals("Kiwi", list.get(1)); + list.add(2, "Pear"); + list.add(3, "Peach"); + list.add(4, "Pizza"); + list.add(5, "Pasta"); + list.add(6, "Chicken"); + list.add(7, "Sandwich"); + list.add(8, "Pickle"); + list.add(9, "Cucumber"); + assertEquals("Pear", list.get(2)); + assertEquals("Peach", list.get(3)); + assertEquals("Pizza", list.get(4)); + assertEquals("Pasta", list.get(5)); + assertEquals("Chicken", list.get(6)); + assertEquals("Sandwich", list.get(7)); + assertEquals("Pickle", list.get(8)); + assertEquals("Cucumber", list.get(9)); + assertEquals("Banana", list.get(0)); + assertEquals("Kiwi", list.get(1)); } @Test void set() { + ArrayList list = new ArrayList<>(); + list.add(0, "Pickle"); + assertEquals("Pickle", list.get(0)); + list.set(0, "Cucumber"); + assertEquals("Cucumber", list.get(0)); + list.add(1, "Peach"); + assertEquals("Peach", list.get(1)); + list.set(1, "Pear"); + assertEquals("Pear", list.get(1)); + list.add(2, "Pizza"); + list.add(3, "Pasta"); + list.add(4, "Chicken"); + list.add(5, "Cookie"); + list.add(6, "Cake"); + list.add(7, "Pie"); + list.add(8, "Sandwich"); + list.add(9, "Eggs"); + assertEquals("Eggs", list.get(9)); + list.set(9, "Hashbrowns"); + assertEquals("Hashbrowns", list.get(9)); } @Test void removeFront() { + ArrayList list = new ArrayList<>(); + list.addFront("Banana"); + assertEquals(1, list.size()); + assertEquals("Banana", list.get(0)); + assertEquals("Banana", list.removeFront()); + assertEquals(0, list.size()); + list.addFront("Peach"); + list.addFront("Pear"); + assertEquals(2, list.size()); + assertEquals("Peach", list.get(1)); + assertEquals("Pear", list.get(0)); + assertEquals("Pear", list.removeFront()); + assertEquals("Peach", list.removeFront()); + list.addFront("Pizza"); + list.addFront("Brownie"); + list.addFront("Kiwi"); + list.addFront("Lime"); + list.addFront("Lemon"); + list.addFront("Pasta"); + list.addFront("Chicken"); + list.addFront("Rice"); + list.addFront("Cucumber"); + list.addFront("Pickle"); + assertEquals(10, list.size()); + assertEquals("Pickle", list.get(0)); + assertEquals("Pickle", list.removeFront()); + assertEquals("Cucumber", list.get(0)); + assertEquals("Cucumber", list.removeFront()); + assertEquals("Rice", list.get(0)); + assertEquals("Rice", list.removeFront()); + assertEquals("Chicken", list.get(0)); + assertEquals("Chicken", list.removeFront()); + assertEquals("Pasta", list.get(0)); + assertEquals("Pasta", list.removeFront()); + assertEquals(5, list.size()); } @Test void removeBack() { + ArrayList list = new ArrayList<>(); + list.addBack("Banana"); + assertEquals(1, list.size()); + assertEquals("Banana", list.removeBack()); + list.addBack("Cookie"); + list.addBack("Cake"); + assertEquals("Cake", list.removeBack()); + assertEquals("Cookie", list.removeBack()); + list.addBack("Pizza"); + list.addBack("Brownie"); + list.addBack("Grilled Cheese"); + list.addBack("Apple"); + list.addBack("Kiwi"); + list.addBack("Orange"); + list.addBack("Peach"); + list.addBack("Pear"); + list.addBack("Lemon"); + list.addBack("Lime"); + assertEquals(10, list.size()); + assertEquals("Lime", list.get(list.size()-1)); + assertEquals("Lime", list.removeBack()); + assertEquals("Lemon", list.get(list.size()-1)); + assertEquals("Lemon", list.removeBack()); + assertEquals("Pear", list.get(list.size()-1)); + assertEquals("Pear", list.removeBack()); + assertEquals("Peach", list.get(list.size()-1)); + assertEquals("Peach", list.removeBack()); + assertEquals(6, list.size()); } @Test - void remove() { + void removeItem() { + ArrayList list = new ArrayList<>(); + list.addBack("Banana"); + list.addBack("Pizza"); + list.addBack("Kiwi"); + list.addBack("Apple"); + list.addBack("Orange"); + list.addBack("Pasta"); + list.addBack("Lemon"); + list.addBack("Lime"); + list.addBack("Peach"); + list.addBack("Pear"); + assertEquals(10, list.size()); + assertEquals("Pear", list.get(list.size()-1)); + list.remove("Pear"); + assertEquals("Peach", list.get(list.size()-1)); } @Test - void testRemove() { + void removeIndex() { + ArrayList list = new ArrayList<>(); + list.addFront("Banana"); + assertEquals("Banana", list.remove(0)); + list.addFront("Kiwi"); + list.addFront("Orange"); + assertEquals("Kiwi", list.remove(1)); + list.addFront("Apple"); + list.addFront("Pasta"); + list.addFront("Chicken"); + list.addFront("Cucumber"); + list.addFront("Pizza"); + list.addFront("Brownie"); + list.addFront("Cookie"); + list.addFront("Cake"); + list.addFront("Pie"); + assertEquals("Orange", list.get(list.size()-1)); + assertEquals("Orange", list.remove(list.size()-1)); + assertEquals("Apple", list.get(list.size()-1)); + assertEquals("Pizza", list.remove(4)); + assertEquals("Cucumber", list.get(4)); + assertEquals("Brownie", list.remove(3)); + assertEquals("Cucumber", list.get(3)); + assertEquals("Pie", list.remove(0)); + assertEquals("Cookie", list.remove(1)); } @Test void contains() { + ArrayList list = new ArrayList<>(); + assertFalse(list.contains("Banana")); + list.addFront("Banana"); + assertTrue(list.contains("Banana")); + list.addFront("Kiwi"); + list.addFront("Watermelon"); + list.addFront("Cantaloupe"); + list.addFront("Apple"); + list.addFront("Orange"); + list.addFront("Passion fruit"); + assertFalse(list.contains("Pizza")); + assertTrue(list.contains("Orange")); + assertTrue(list.contains("Watermelon")); + assertFalse(list.contains("Brownie")); + assertFalse(list.contains("Pickle")); + assertTrue(list.contains("Kiwi")); + assertTrue(list.contains("Apple")); } @Test void isEmpty() { + ArrayList list = new ArrayList<>(); + assertTrue(list.isEmpty()); + list.addFront("Banana"); + assertFalse(list.isEmpty()); + list.addFront("Kiwi"); + assertFalse(list.isEmpty()); + list.removeFront(); + list.removeFront(); + assertTrue(list.isEmpty()); } @Test void size() { + ArrayList list = new ArrayList<>(); + assertEquals(0, list.size()); + list.addFront("Banana"); + assertEquals(1, list.size()); + list.addFront("Apple"); + list.addFront("Kumquat"); + list.addFront("Kiwi"); + list.addFront("Pizza"); + list.addFront("Brownie"); + list.addFront("Cracker"); + assertEquals(7, list.size()); + list.removeFront(); + assertEquals(6, list.size()); + list.removeFront(); + list.removeFront(); + list.removeFront(); + list.removeFront(); + assertEquals(2, list.size()); } } \ No newline at end of file From 8df0ccf2541e1ed567f2aff558c2935308a80e6d Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Sat, 17 Feb 2024 18:45:34 -0800 Subject: [PATCH 25/36] Created method headers for Bag interface. --- src/Bag.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/Bag.java diff --git a/src/Bag.java b/src/Bag.java new file mode 100644 index 0000000..26decf4 --- /dev/null +++ b/src/Bag.java @@ -0,0 +1,22 @@ +/** + * Bag API + */ +public interface Bag extends Iterable { + /** + * Add an item to the bag. + * @param item the item to be added + */ + void add(E item); + + /** + * Checks to see if the bag is emtpy + * @return true if the bag is empty, false otherwise. + */ + boolean isEmpty(); + + /** + * Returns a count of the number of items in the bag. + * @return the number of items in the bag + */ + int size(); +} From 61cfed4d66de129f65d3efb88b6ee29a44cbf233 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Sat, 17 Feb 2024 19:26:10 -0800 Subject: [PATCH 26/36] Created StackTestClient. --- src/ResizingArrayStack.java | 2 ++ src/StackTestClient.java | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 src/ResizingArrayStack.java create mode 100644 src/StackTestClient.java diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java new file mode 100644 index 0000000..7fef59a --- /dev/null +++ b/src/ResizingArrayStack.java @@ -0,0 +1,2 @@ +public class ResizingArrayStack implements Stack{ +} diff --git a/src/StackTestClient.java b/src/StackTestClient.java new file mode 100644 index 0000000..c08d366 --- /dev/null +++ b/src/StackTestClient.java @@ -0,0 +1,17 @@ +import java.util.Scanner; +public class StackTestClient { + public static void main(String[] args) { + Stack s = new ResizingArrayStack(); + Scanner in = new Scanner("to be or not to - be - - that - - - is"); + while (in.hasNext()) { + String item = in.next(); + if (item.equals("-")) { + s.push("item"); + } + else if (!s.isEmpty()) { + System.out.println("s.pop()" + " "); + } + } + System.out.println("(" + s.size() + " left on the stack"); + } + } From 475aebe2055275b2ae882710c21617872814f118 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Sat, 17 Feb 2024 20:14:30 -0800 Subject: [PATCH 27/36] Added methods for ResizingArrayStack and tested StackTestClient. --- src/ResizingArrayStack.java | 78 +++++++++++++++++++++++++++++++++++++ src/StackTestClient.java | 8 ++-- 2 files changed, 82 insertions(+), 4 deletions(-) diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java index 7fef59a..7a90980 100644 --- a/src/ResizingArrayStack.java +++ b/src/ResizingArrayStack.java @@ -1,2 +1,80 @@ +import java.util.Iterator; + public class ResizingArrayStack implements Stack{ + private E[] buffer; + private int size; + + public ResizingArrayStack() + { + buffer = (E[])new Object[1]; + size = 0; + } + + private void resize(int max) + { + E[] temp = (E[]) new Object[max]; + for (int i = 0; i < size; i++) { + temp[i] = buffer[i]; + } + buffer = temp; + } + @Override + public void push(E item) + { // Add item to top of stack. + if (size == buffer.length) { + resize(buffer.length*2); + } + buffer[size++] = item; + } + + @Override + public E pop() + { // Remove item from top of stack. + E item = buffer[--size]; + buffer[size] = null; // Avoid loitering + if (size > 0 && size == buffer.length/4) { + resize(buffer.length/2); + } + return item; + } + + @Override + public E peek() + { + return null; + } + + @Override + public boolean isEmpty() + { + return size == 0; + } + + @Override + public int size() + { + return size; + } + + @Override + public Iterator iterator() + { + return new ReverseArrayIterator(); + } + private class ReverseArrayIterator implements Iterator + { + private int i = size; + public boolean hasNext() + { + return i > 0; + } + public E next() + { + return buffer[--i]; + } + public void remove() + { + + } + } } diff --git a/src/StackTestClient.java b/src/StackTestClient.java index c08d366..01e95e4 100644 --- a/src/StackTestClient.java +++ b/src/StackTestClient.java @@ -5,13 +5,13 @@ public static void main(String[] args) { Scanner in = new Scanner("to be or not to - be - - that - - - is"); while (in.hasNext()) { String item = in.next(); - if (item.equals("-")) { - s.push("item"); + if (!item.equals("-")) { + s.push(item); } else if (!s.isEmpty()) { - System.out.println("s.pop()" + " "); + System.out.println(s.pop() + " "); } } - System.out.println("(" + s.size() + " left on the stack"); + System.out.println("(" + s.size() + " left on the stack)"); } } From 35b035112d55babc32c5dfc12d4d6d3170deb627 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Sun, 18 Feb 2024 20:18:05 -0800 Subject: [PATCH 28/36] Completed LinkedStack methods. --- src/LinkedStack.java | 75 +++++++++++++++++++++++++++++++++++++ src/ResizingArrayStack.java | 2 +- 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/LinkedStack.java diff --git a/src/LinkedStack.java b/src/LinkedStack.java new file mode 100644 index 0000000..3acd032 --- /dev/null +++ b/src/LinkedStack.java @@ -0,0 +1,75 @@ +import java.util.Iterator; +import java.util.ListIterator; + +public class LinkedStack implements Stack { + + private Node head; + private int size; + private class Node + { + E item; + Node next; + } + + public LinkedStack() + { + head = null; + size = 0; + } + @Override + public void push(E item) { + Node oldHead = head; + head = new Node(); + head.item = item; + head.next = oldHead; + size++; + } + + @Override + public E pop() { + E toBeRemoved = head.item; + head = head.next; + size--; + return toBeRemoved; + } + + @Override + public E peek() { + return head.item; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public int size() { + return size; + } + + @Override + public Iterator iterator() { + return new ListIterator(); + } + private class ListIterator implements Iterator + { + private Node current = head; + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public E next() { + E item = current.item; + current = current.next; + return item; + } + + @Override + public void remove() { + + } + } +} diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java index 7a90980..a633a10 100644 --- a/src/ResizingArrayStack.java +++ b/src/ResizingArrayStack.java @@ -41,7 +41,7 @@ public E pop() @Override public E peek() { - return null; + return buffer[0]; } @Override From 99722d700b403a1f3f961a250d30cffd403cd27a Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Sun, 18 Feb 2024 20:44:14 -0800 Subject: [PATCH 29/36] Updated to test LinkedStack implementation. --- src/StackTestClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StackTestClient.java b/src/StackTestClient.java index 01e95e4..3fe600a 100644 --- a/src/StackTestClient.java +++ b/src/StackTestClient.java @@ -1,7 +1,7 @@ import java.util.Scanner; public class StackTestClient { public static void main(String[] args) { - Stack s = new ResizingArrayStack(); + Stack s = new LinkedStack(); Scanner in = new Scanner("to be or not to - be - - that - - - is"); while (in.hasNext()) { String item = in.next(); From a3802e550360c9b2e7edfbad2853f9ac6d100202 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Sun, 18 Feb 2024 20:44:29 -0800 Subject: [PATCH 30/36] Updated to test LinkedStack implementation. --- src/StackTestClient.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StackTestClient.java b/src/StackTestClient.java index 3fe600a..63adb29 100644 --- a/src/StackTestClient.java +++ b/src/StackTestClient.java @@ -1,7 +1,7 @@ import java.util.Scanner; public class StackTestClient { public static void main(String[] args) { - Stack s = new LinkedStack(); + Stack s = new LinkedStack<>(); Scanner in = new Scanner("to be or not to - be - - that - - - is"); while (in.hasNext()) { String item = in.next(); From e8e8f68ace8ae7ab07fe32f865c2043f9b112571 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Sun, 18 Feb 2024 20:45:09 -0800 Subject: [PATCH 31/36] Removed ListIterator import statement --- src/LinkedStack.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/LinkedStack.java b/src/LinkedStack.java index 3acd032..8a847ce 100644 --- a/src/LinkedStack.java +++ b/src/LinkedStack.java @@ -1,5 +1,4 @@ import java.util.Iterator; -import java.util.ListIterator; public class LinkedStack implements Stack { From 6503d02fed2ca1b6087986a0a968246337a4a08d Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Sun, 18 Feb 2024 20:52:37 -0800 Subject: [PATCH 32/36] Completed LinkedQueue methods. --- src/LinkedQueue.java | 73 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/LinkedQueue.java diff --git a/src/LinkedQueue.java b/src/LinkedQueue.java new file mode 100644 index 0000000..b0bd0da --- /dev/null +++ b/src/LinkedQueue.java @@ -0,0 +1,73 @@ +import java.util.Iterator; + +public class LinkedQueue implements Queue{ + private Node head; + private Node tail; + private int size; + + private class Node + { + E item; + Node next; + } + @Override + public void enqueue(E item) { + Node oldTail = tail; + tail = new Node(); + tail.item = item; + tail.next = null; + if(isEmpty()) { + head = tail; + } + else { + oldTail.next = tail; + } + size++; + } + + @Override + public E dequeue() { + E item = head.item; + head = head.next; + if(isEmpty()) { + tail = null; + } + size--; + return item; + } + + @Override + public boolean isEmpty() { + return head == null; + } + + @Override + public int size() { + return size; + } + + @Override + public Iterator iterator() { + return new ListIterator(); + } + private class ListIterator implements Iterator + { + private Node current = head; + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public E next() { + E item = current.item; + current = current.next; + return item; + } + + @Override + public void remove() { + + } + } +} From 0dcbdaa82afd309b443dfdbb08ccf92249a5df0e Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Sun, 18 Feb 2024 21:20:23 -0800 Subject: [PATCH 33/36] Created and tested LinkedBag through Stats class. --- src/LinkedBag.java | 58 ++++++++++++++++++++++++++++++++++++++++ src/QueueTestClient.java | 19 +++++++++++++ src/StackTestClient.java | 14 +++++----- src/Stats.java | 25 +++++++++++++++++ 4 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 src/LinkedBag.java create mode 100644 src/QueueTestClient.java create mode 100644 src/Stats.java diff --git a/src/LinkedBag.java b/src/LinkedBag.java new file mode 100644 index 0000000..6b10396 --- /dev/null +++ b/src/LinkedBag.java @@ -0,0 +1,58 @@ +import java.util.Iterator; + +public class LinkedBag implements Bag { + private Node head; + private int size; + + public LinkedBag() + { + head = null; + size = 0; + } + private class Node + { + E item; + Node next; + } + @Override + public void add(E item) { + Node oldHead = head; + head = new Node(); + head.item = item; + head.next = oldHead; + size++; + } + + @Override + public boolean isEmpty() { + return head == null; + } + + @Override + public int size() { + return size; + } + + @Override + public Iterator iterator() { + return new ListIterator(); + } + private class ListIterator implements Iterator + { + private Node current = head; + public boolean hasNext() + { + return current != null; + } + public void remove() + { + + } + public E next() + { + E item = current.item; + current = current.next; + return item; + } + } +} diff --git a/src/QueueTestClient.java b/src/QueueTestClient.java new file mode 100644 index 0000000..4802053 --- /dev/null +++ b/src/QueueTestClient.java @@ -0,0 +1,19 @@ +import java.util.Scanner; + +public class QueueTestClient { + public static void main(String[] args) + { // Create a queue and enqueue/dequeue strings. + Queue q = new LinkedQueue<>(); + Scanner in = new Scanner("to be or not to - be - - that - - - is"); + while (in.hasNext()) { + String item = in.next(); + if (!item.equals("-")){ + q.enqueue(item); + } + else if (!q.isEmpty()) { + System.out.println((q.dequeue() + " ")); + } + } + System.out.println("(" + q.size() + " left on queue)"); + } +} diff --git a/src/StackTestClient.java b/src/StackTestClient.java index 63adb29..736a64f 100644 --- a/src/StackTestClient.java +++ b/src/StackTestClient.java @@ -4,14 +4,14 @@ public static void main(String[] args) { Stack s = new LinkedStack<>(); Scanner in = new Scanner("to be or not to - be - - that - - - is"); while (in.hasNext()) { - String item = in.next(); - if (!item.equals("-")) { - s.push(item); - } - else if (!s.isEmpty()) { - System.out.println(s.pop() + " "); + String item = in.next(); + if (!item.equals("-")) { + s.push(item); + } + else if (!s.isEmpty()) { + System.out.println(s.pop() + " "); + } } - } System.out.println("(" + s.size() + " left on the stack)"); } } diff --git a/src/Stats.java b/src/Stats.java new file mode 100644 index 0000000..ac49dbf --- /dev/null +++ b/src/Stats.java @@ -0,0 +1,25 @@ +import java.util.Scanner; + +public class Stats { + public static void main(String[] args) + { + Bag numbers = new LinkedBag(); + Scanner in = new Scanner("100 99 101 120 98 107 109 81 101 90"); + while(in.hasNextDouble()) { + numbers.add(in.nextDouble()); + } + int size = numbers.size(); + double sum = 0.0; + for (double x : numbers) { + sum += x; + } + double mean = sum/size; + sum = 0.0; + for (double x : numbers) { + sum += (x - mean)*(x - mean); + } + double std = Math.sqrt(sum/(size-1)); + System.out.printf("Mean: %.2f\n", mean); + System.out.printf("Std dev: %.2f\n", std); + } +} From 79d1426cc79c790a1fb28e8ddf39cf091f96d11a Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Sun, 18 Feb 2024 21:21:27 -0800 Subject: [PATCH 34/36] Fixed warnings in Stats class. --- src/Stats.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Stats.java b/src/Stats.java index ac49dbf..fe943b9 100644 --- a/src/Stats.java +++ b/src/Stats.java @@ -3,7 +3,7 @@ public class Stats { public static void main(String[] args) { - Bag numbers = new LinkedBag(); + Bag numbers = new LinkedBag<>(); Scanner in = new Scanner("100 99 101 120 98 107 109 81 101 90"); while(in.hasNextDouble()) { numbers.add(in.nextDouble()); From 767577b94b47e2d53e32bbc2404d29a7a579f720 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Sun, 18 Feb 2024 21:41:27 -0800 Subject: [PATCH 35/36] Added Runtime analysis for all methods. --- src/LinkedBag.java | 26 ++++++++++++++++++++ src/LinkedQueue.java | 35 +++++++++++++++++++++++++++ src/LinkedStack.java | 41 ++++++++++++++++++++++++++++++- src/ResizingArrayStack.java | 48 ++++++++++++++++++++++++++++++++++++- 4 files changed, 148 insertions(+), 2 deletions(-) diff --git a/src/LinkedBag.java b/src/LinkedBag.java index 6b10396..73c35f8 100644 --- a/src/LinkedBag.java +++ b/src/LinkedBag.java @@ -1,5 +1,11 @@ import java.util.Iterator; +/** + * Creates a linked list bag object. Has the ability to add items, check the + * size of the bag, and status of if the bag is empty. + * @author Sage Bain + * @param + */ public class LinkedBag implements Bag { private Node head; private int size; @@ -14,6 +20,12 @@ private class Node E item; Node next; } + + /** + * This will always run in constant time as we are simply creating a new + * head Node and incrementing size, no looping or iteration required. + * @param item the item to be added + */ @Override public void add(E item) { Node oldHead = head; @@ -23,16 +35,30 @@ public void add(E item) { size++; } + /** + * This will always run in constant time as we are simply returning + * true or false based on if head is equal to null. + * @return + */ @Override public boolean isEmpty() { return head == null; } + /** + * This will always run in constant time as we are simply returning + * the value of size. + * @return + */ @Override public int size() { return size; } + /** + * + * @return a new ListIterator + */ @Override public Iterator iterator() { return new ListIterator(); diff --git a/src/LinkedQueue.java b/src/LinkedQueue.java index b0bd0da..d71d205 100644 --- a/src/LinkedQueue.java +++ b/src/LinkedQueue.java @@ -1,5 +1,12 @@ import java.util.Iterator; +/** + * Creates a new LinkedQueue object. Has the ability to add and remove items + * from the linked queue, as well as check the size and status of if the + * linked queue is empty. + * @author Sage Bain + * @param + */ public class LinkedQueue implements Queue{ private Node head; private Node tail; @@ -10,6 +17,13 @@ private class Node E item; Node next; } + + /** + * This will always run in constant time, as there is no need to iterate + * through the linked queue. We are simply making adding a new item to the + * tail of the queue, and checking if the linked queue was originally empty. + * @param item the item to be added + */ @Override public void enqueue(E item) { Node oldTail = tail; @@ -25,6 +39,13 @@ public void enqueue(E item) { size++; } + /** + * This will always run in constant time, as there is no need to iterate + * through the linked queue. We are simply moving head forward one space. + * Afterward, we check to see the linked queue was originally empty. If so, + * we set tail to null. Then we decrement size and return the removed item. + * @return + */ @Override public E dequeue() { E item = head.item; @@ -36,16 +57,30 @@ public E dequeue() { return item; } + /** + * This will always run at constant time since we are simply returning + * true or false based on if head is equal to null. + * @return + */ @Override public boolean isEmpty() { return head == null; } + /** + * This will always run at constant time since we are simply returning + * the value of size. + * @return + */ @Override public int size() { return size; } + /** + * + * @return a new ListIterator + */ @Override public Iterator iterator() { return new ListIterator(); diff --git a/src/LinkedStack.java b/src/LinkedStack.java index 8a847ce..27fd883 100644 --- a/src/LinkedStack.java +++ b/src/LinkedStack.java @@ -1,5 +1,12 @@ import java.util.Iterator; +/** + * Creates a linked stack object. Has the ability to add and remove items, + * peek at the top item, and view the size and status of if the linked stack is + * empty. + * @author Sage Bain + * @param + */ public class LinkedStack implements Stack { private Node head; @@ -15,6 +22,13 @@ public LinkedStack() head = null; size = 0; } + + /** + * This will always run at constant time, since there is no need to + * go through all the elements in the LinkedStack. This method + * simply creates a new head element and increments size in 5 lines. + * @param item the item to be added + */ @Override public void push(E item) { Node oldHead = head; @@ -24,6 +38,13 @@ public void push(E item) { size++; } + /** + * This will always run at constant time, since there is no need to go + * through all the elements in the LinkedStack. This method simply + * saves the element to be removed in a variable, moves head one element + * forward, decrements size, and returns the removed Node. + * @return + */ @Override public E pop() { E toBeRemoved = head.item; @@ -32,21 +53,39 @@ public E pop() { return toBeRemoved; } + /** + * This will always run at constant time, since we are simply + * returning the value at head. + * @return + */ @Override public E peek() { return head.item; } + /** + * This will always run at constant time, since we are simply + * returning true or false based on if size is equal to 0 or not. + * @return + */ @Override public boolean isEmpty() { return size == 0; } - + /** + * This will always run at constant time, since we are simply + * returning the value of size. + * @return + */ @Override public int size() { return size; } + /** + * + * @return a new ListIterator + */ @Override public Iterator iterator() { return new ListIterator(); diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java index a633a10..81a23ec 100644 --- a/src/ResizingArrayStack.java +++ b/src/ResizingArrayStack.java @@ -1,5 +1,12 @@ import java.util.Iterator; +/** + * Creates a resizing array stack object. Has the ability to add and remove items, + * peek at the top item, and view the size and status of if the linked stack is + * empty. + * @author Sage Bain + * @param + */ public class ResizingArrayStack implements Stack{ private E[] buffer; private int size; @@ -10,6 +17,11 @@ public ResizingArrayStack() size = 0; } + /** + * This method will always run at O(n) because we are manually copying + * all items over from the original buffer to a new buffer. + * @param max + */ private void resize(int max) { E[] temp = (E[]) new Object[max]; @@ -18,6 +30,14 @@ private void resize(int max) } buffer = temp; } + + /** + * Worst case scenario, this method will run at O(n) if we need to resize + * the buffer due to it being at max capacity. Best case scenario, + * this method runs at constant time due to being able to insert the new + * item at the end of the buffer in a single statement. + * @param item the item to be added + */ @Override public void push(E item) { // Add item to top of stack. @@ -27,6 +47,13 @@ public void push(E item) buffer[size++] = item; } + /** + * Worst case scenario, this method will run at O(n). + * This is because if the method detects the buffer is using only 1/4 + * of its capacity, it will resize itself down by 1/2 of its original + * capacity. Best case scenario, this will run at constant time. + * @return + */ @Override public E pop() { // Remove item from top of stack. @@ -38,24 +65,43 @@ public E pop() return item; } + /** + * This will always run at constant time since it simply returns + * the item located at buffer index 0. + * @return + */ @Override public E peek() { - return buffer[0]; + return buffer[size-1]; } + /** + * This will always run at constant time since it simply returns + * true or false based on if size is equal to 0. + * @return + */ @Override public boolean isEmpty() { return size == 0; } + /** + * This will always run at constant time since it simply returns the + * value of size. + * @return + */ @Override public int size() { return size; } + /** + * + * @return a new ListIterator + */ @Override public Iterator iterator() { From 6c1d363ff70e51036fed57a62b664834f70b67b2 Mon Sep 17 00:00:00 2001 From: Sage Bain Date: Sun, 18 Feb 2024 21:45:47 -0800 Subject: [PATCH 36/36] Fixed warnings in all methods. --- src/LinkedBag.java | 4 ++-- src/LinkedList.java | 4 ++-- src/LinkedQueue.java | 6 +++--- src/LinkedStack.java | 8 ++++---- src/ResizingArrayStack.java | 10 +++++----- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/LinkedBag.java b/src/LinkedBag.java index 73c35f8..b9f8388 100644 --- a/src/LinkedBag.java +++ b/src/LinkedBag.java @@ -38,7 +38,7 @@ public void add(E item) { /** * This will always run in constant time as we are simply returning * true or false based on if head is equal to null. - * @return + * @return true or false based on if the bag is empty. */ @Override public boolean isEmpty() { @@ -48,7 +48,7 @@ public boolean isEmpty() { /** * This will always run in constant time as we are simply returning * the value of size. - * @return + * @return size */ @Override public int size() { diff --git a/src/LinkedList.java b/src/LinkedList.java index bd68154..a3e565e 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -261,7 +261,7 @@ public void remove(E item) { * very last index, meaning the method has to go individually node by * node until it reaches the last node to remove. * @param i the index where the item should be removed - * @return + * @return item to be removed */ @Override public E remove(int i) { @@ -349,7 +349,7 @@ public int size() { @Override public Iterator iterator() { Node current = head; - return new Iterator() { + return new Iterator<>() { @Override public boolean hasNext() { return current.next != null; diff --git a/src/LinkedQueue.java b/src/LinkedQueue.java index d71d205..290b1ba 100644 --- a/src/LinkedQueue.java +++ b/src/LinkedQueue.java @@ -44,7 +44,7 @@ public void enqueue(E item) { * through the linked queue. We are simply moving head forward one space. * Afterward, we check to see the linked queue was originally empty. If so, * we set tail to null. Then we decrement size and return the removed item. - * @return + * @return item to be removed */ @Override public E dequeue() { @@ -60,7 +60,7 @@ public E dequeue() { /** * This will always run at constant time since we are simply returning * true or false based on if head is equal to null. - * @return + * @return true or false based on if the queue is empty */ @Override public boolean isEmpty() { @@ -70,7 +70,7 @@ public boolean isEmpty() { /** * This will always run at constant time since we are simply returning * the value of size. - * @return + * @return size */ @Override public int size() { diff --git a/src/LinkedStack.java b/src/LinkedStack.java index 27fd883..d161e8e 100644 --- a/src/LinkedStack.java +++ b/src/LinkedStack.java @@ -43,7 +43,7 @@ public void push(E item) { * through all the elements in the LinkedStack. This method simply * saves the element to be removed in a variable, moves head one element * forward, decrements size, and returns the removed Node. - * @return + * @return item removed from the stack */ @Override public E pop() { @@ -56,7 +56,7 @@ public E pop() { /** * This will always run at constant time, since we are simply * returning the value at head. - * @return + * @return item at the top of the stack */ @Override public E peek() { @@ -66,7 +66,7 @@ public E peek() { /** * This will always run at constant time, since we are simply * returning true or false based on if size is equal to 0 or not. - * @return + * @return true or false based on if size is equal to 0. */ @Override public boolean isEmpty() { @@ -75,7 +75,7 @@ public boolean isEmpty() { /** * This will always run at constant time, since we are simply * returning the value of size. - * @return + * @return size */ @Override public int size() { diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java index 81a23ec..b27ab95 100644 --- a/src/ResizingArrayStack.java +++ b/src/ResizingArrayStack.java @@ -20,7 +20,7 @@ public ResizingArrayStack() /** * This method will always run at O(n) because we are manually copying * all items over from the original buffer to a new buffer. - * @param max + * @param max capacity of the buffer */ private void resize(int max) { @@ -52,7 +52,7 @@ public void push(E item) * This is because if the method detects the buffer is using only 1/4 * of its capacity, it will resize itself down by 1/2 of its original * capacity. Best case scenario, this will run at constant time. - * @return + * @return removed item */ @Override public E pop() @@ -68,7 +68,7 @@ public E pop() /** * This will always run at constant time since it simply returns * the item located at buffer index 0. - * @return + * @return top item of the stack */ @Override public E peek() @@ -79,7 +79,7 @@ public E peek() /** * This will always run at constant time since it simply returns * true or false based on if size is equal to 0. - * @return + * @return true or false based on if size is equal to 0 */ @Override public boolean isEmpty() @@ -90,7 +90,7 @@ public boolean isEmpty() /** * This will always run at constant time since it simply returns the * value of size. - * @return + * @return size */ @Override public int size()