From 13d2978c21575764cbe9ab7681edc83e257d4043 Mon Sep 17 00:00:00 2001 From: InfiniteGmng Date: Tue, 13 Feb 2024 14:31:42 -0800 Subject: [PATCH 01/15] Implemented the methods from List and implemented the interator() method. Added throw exceptions to the methods that need it --- src/ArrayList.java | 163 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 src/ArrayList.java diff --git a/src/ArrayList.java b/src/ArrayList.java new file mode 100644 index 0000000..8840662 --- /dev/null +++ b/src/ArrayList.java @@ -0,0 +1,163 @@ +import java.util.Iterator; +import java.util.*; + +public class ArrayList implements List { + private int size; + private E[] buffer; + + public ArrayList() { + size = 0; + buffer = (E[]) new Object[10]; + } + + + @Override + public void addFront(E item) { + if (size == buffer.length) { + resize(); + } + for (int i = size; i >= 1; i--) { + buffer[i] = buffer[i - 1]; + } + buffer[0] = item; + size++; + } + + @Override + public void addBack(E item) { + if (size == buffer.length) { + resize(); + } + buffer[size++] = item; + } + + @Override + public void add(int index, E item) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index is out of bounds"); + } + if (size == buffer.length) { + resize(); + } + for (int i = size; i > index; i--) { + buffer[i] = buffer[i - 1]; + } + buffer[index] = item; + size++; + } + + @Override + public E get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index is out of bounds"); + } + return buffer[index]; + } + + @Override + public void set(int index, E item) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index is out of bounds"); + } + buffer[index] = item; + } + + @Override + public E removeFront() { + if (isEmpty()) { + throw new NoSuchElementException("List is empty"); + } + E removedItem = buffer[0]; + for (int i = 0; i < size - 1; i++) { + buffer[i] = buffer[i + 1]; + } + size--; + return removedItem; + } + + @Override + public E removeBack() { + if (isEmpty()) { + throw new NoSuchElementException("List is empty"); + } + E removedItem = buffer[size - 1]; + buffer[--size] = null; + return removedItem; + } + + @Override + public void remove(E item) { + for (int i = 0; i < size; i++) { + if (buffer[i].equals(item)) { + for (int j = i; j < size - 1; j++) { + buffer[j] = buffer[j + 1]; + } + buffer[--size] = null; + return; + } + } + } + + @Override + public E remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index is out of bounds"); + } + E removedItem = buffer[index]; + for (int i = index; i < size - 1; i++) { + buffer[i] = buffer[i + 1]; + } + buffer[--size] = null; + return removedItem; + } + + @Override + public boolean contains(E item) { + for (int i = 0; i < size; i++) { + if (buffer[i].equals(item)) { + return true; + } + } + return false; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public int size() { + return size; + } + + private void resize() { + E[] newBuffer = (E[]) new Object[size * 2]; + for (int i = 0; i < size; i++) { + newBuffer[i] = buffer[i]; + } + buffer = newBuffer; + } + + @Override + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + private int index = 0; + + @Override + public boolean hasNext() { + return index < size; + } + + @Override + public E next() { + if (!hasNext()) { + throw new UnsupportedOperationException("No more elements in list"); + } + return buffer[index++]; + } + } +} From d01854a6055efaae06eea98792e1f88f2d6cdb51 Mon Sep 17 00:00:00 2001 From: InfiniteGmng Date: Tue, 13 Feb 2024 20:18:49 -0800 Subject: [PATCH 02/15] Added JUnit tests for ArrayList --- Tests/ArrayListTests.java | 309 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 309 insertions(+) create mode 100644 Tests/ArrayListTests.java diff --git a/Tests/ArrayListTests.java b/Tests/ArrayListTests.java new file mode 100644 index 0000000..39d7aa9 --- /dev/null +++ b/Tests/ArrayListTests.java @@ -0,0 +1,309 @@ +import static org.junit.Assert.*; +import org.junit.Test; + +import java.util.Iterator; + +public class ArrayListTests { + @Test + public void testAddFrontEmptyList() { + ArrayList list = new ArrayList<>(); + list.addFront(5); + assertEquals(1, list.size()); + assertEquals(Integer.valueOf(5), list.get(0)); + } + + @Test + public void testAddFrontNearlyEmptyList() { + ArrayList list = new ArrayList<>(); + list.addFront(5); + list.addFront(10); + assertEquals(2, list.size()); + assertEquals(Integer.valueOf(10), list.get(0)); + assertEquals(Integer.valueOf(5), list.get(1)); + } + + @Test + public void testAddFrontNonEmptyList() { + ArrayList list = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + list.addBack(i); + } + list.addFront(100); + assertEquals(11, list.size()); + assertEquals(Integer.valueOf(100), list.get(0)); + } + + @Test + public void testAddBackEmptyList() { + ArrayList list = new ArrayList<>(); + list.addBack(5); + assertEquals(1, list.size()); + assertEquals(Integer.valueOf(5), list.get(0)); + } + + @Test + public void testAddBackNearlyEmptyList() { + ArrayList list = new ArrayList<>(); + list.addBack(5); + list.addBack(10); + assertEquals(2, list.size()); + assertEquals(Integer.valueOf(5), list.get(0)); + assertEquals(Integer.valueOf(10), list.get(1)); + } + + @Test + public void testAddBackNonEmptyList() { + ArrayList list = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + list.addBack(i); + } + list.addBack(100); + assertEquals(11, list.size()); + assertEquals(Integer.valueOf(0), list.get(0)); + assertEquals(Integer.valueOf(100), list.get(10)); + } + + @Test + public void testAddEmptyList() { + ArrayList list = new ArrayList<>(); + list.add(0, 5); + assertEquals(1, list.size()); + assertEquals(Integer.valueOf(5), list.get(0)); + } + + @Test + public void testAddNearlyEmptyList() { + ArrayList list = new ArrayList<>(); + list.add(0, 5); + list.add(1, 10); + assertEquals(2, list.size()); + assertEquals(Integer.valueOf(5), list.get(0)); + assertEquals(Integer.valueOf(10), list.get(1)); + } + + @Test + public void testAddNonEmptyList() { + ArrayList list = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + list.addBack(i); + } + list.add(5, 100); + assertEquals(11, list.size()); + assertEquals(Integer.valueOf(100), list.get(5)); + assertEquals(Integer.valueOf(5), list.get(6)); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testAddIndexOutOfBounds() { + ArrayList list = new ArrayList<>(); + list.add(1, 5); + } + + @Test + public void testGetNearlyEmptyList() { + ArrayList list = new ArrayList<>(); + list.addBack(5); + assertEquals(Integer.valueOf(5), list.get(0)); + } + + @Test + public void testGetNonEmptyList() { + ArrayList list = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + list.addBack(i); + } + assertEquals(Integer.valueOf(5), list.get(5)); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testGetIndexOutOfBounds() { + ArrayList list = new ArrayList<>(); + list.addBack(5); + list.get(1); + } + + @Test + public void testSetNearlyEmptyList() { + ArrayList list = new ArrayList<>(); + list.addBack(5); + list.set(0, 10); + assertEquals(Integer.valueOf(10), list.get(0)); + } + + @Test + public void testSetNonEmptyList() { + ArrayList list = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + list.addBack(i); + } + list.set(5, 100); + assertEquals(Integer.valueOf(100), list.get(5)); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testSetIndexOutOfBounds() { + ArrayList list = new ArrayList<>(); + list.set(1, 5); + } + + @Test + public void testRemoveFrontNearlyEmptyList() { + ArrayList list = new ArrayList<>(); + list.addBack(5); + assertEquals(Integer.valueOf(5), list.removeFront()); + assertTrue(list.isEmpty()); + } + + @Test + public void testRemoveFrontNonEmptyList() { + ArrayList list = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + list.addBack(i); + } + assertEquals(Integer.valueOf(0), list.removeFront()); + assertEquals(9, list.size()); + assertEquals(Integer.valueOf(1), list.get(0)); + } + + @Test + public void testRemoveBackNearlyEmptyList() { + ArrayList list = new ArrayList<>(); + list.addBack(5); + assertEquals(Integer.valueOf(5), list.removeBack()); + assertTrue(list.isEmpty()); + } + + @Test + public void testRemoveBackNonEmptyList() { + ArrayList list = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + list.addBack(i); + } + assertEquals(Integer.valueOf(9), list.removeBack()); + assertEquals(9, list.size()); + } + + @Test + public void testRemoveEmptyList() { + ArrayList list = new ArrayList<>(); + list.remove(Integer.valueOf(5)); + assertTrue(list.isEmpty()); + } + + @Test + public void testRemoveNearlyEmptyList() { + ArrayList list = new ArrayList<>(); + list.addBack(5); + list.remove(Integer.valueOf(5)); + assertTrue(list.isEmpty()); + } + + @Test + public void testRemoveNonEmptyList() { + ArrayList list = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + list.addBack(i); + } + list.remove(Integer.valueOf(5)); + assertEquals(9, list.size()); + assertFalse(list.contains(5)); + } + + @Test + public void testRemoveIndexNearlyEmptyList() { + ArrayList list = new ArrayList<>(); + list.addBack(5); + assertEquals(Integer.valueOf(5), list.remove(0)); + assertTrue(list.isEmpty()); + } + + @Test + public void testRemoveIndexNonEmptyList() { + ArrayList list = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + list.addBack(i); + } + assertEquals(Integer.valueOf(5), list.remove(5)); + assertEquals(9, list.size()); + assertFalse(list.contains(5)); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testRemoveIndexOutOfBounds() { + ArrayList list = new ArrayList<>(); + list.addBack(5); + list.remove(1); + } + + @Test + public void testContainsEmptyList() { + ArrayList list = new ArrayList<>(); + assertFalse(list.contains(5)); + } + + @Test + public void testContainsNearlyEmptyList() { + ArrayList list = new ArrayList<>(); + list.addBack(5); + assertTrue(list.contains(5)); + } + + @Test + public void testContainsNonEmptyList() { + ArrayList list = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + list.addBack(i); + } + assertTrue(list.contains(5)); + } + + @Test + public void testIsEmptyEmptyList() { + ArrayList list = new ArrayList<>(); + assertTrue(list.isEmpty()); + } + + @Test + public void testIsEmptyNonEmptyList() { + ArrayList list = new ArrayList<>(); + list.addBack(5); + assertFalse(list.isEmpty()); + } + + @Test + public void testSizeEmptyList() { + ArrayList list = new ArrayList<>(); + assertEquals(0, list.size()); + } + + @Test + public void testSizeNearlyEmptyList() { + ArrayList list = new ArrayList<>(); + list.addBack(5); + assertEquals(1, list.size()); + } + + @Test + public void testSizeNonEmptyList() { + ArrayList list = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + list.addBack(i); + } + assertEquals(10, list.size()); + } + + @Test + public void testIterator() { + ArrayList list = new ArrayList<>(); + for (int i = 0; i < 10; i++) { + list.addBack(i); + } + + Iterator iterator = list.iterator(); + int count = 0; + while (iterator.hasNext()) { + assertEquals(Integer.valueOf(count), iterator.next()); + count++; + } + } +} From 6e12a89dc3085e0872a2cb6071c27ed5961d74c8 Mon Sep 17 00:00:00 2001 From: InfiniteGmng Date: Tue, 13 Feb 2024 20:36:26 -0800 Subject: [PATCH 03/15] Finished the LinkedList method implementations --- src/LinkedList.java | 202 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 src/LinkedList.java diff --git a/src/LinkedList.java b/src/LinkedList.java new file mode 100644 index 0000000..593a106 --- /dev/null +++ b/src/LinkedList.java @@ -0,0 +1,202 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList() { + head = null; + size = 0; + } + + private static class Node { + E data; + Node next; + + public Node(E data) { + this.data = data; + this.next = null; + } + } + + @Override + public void addFront(E item) { + Node newNode = new Node<>(item); + newNode.next = head; + head = newNode; + size++; + } + + @Override + public void addBack(E item) { + if (head == null) { + addFront(item); + } else { + Node newNode = new Node<>(item); + Node current = head; + while (current.next != null) { + current = current.next; + } + current.next = newNode; + size++; + } + } + + @Override + public void add(int index, E item) { + if (index < 0 || index > size) { + throw new IndexOutOfBoundsException("Index is out of bounds"); + } + if (index == 0) { + addFront(item); + } else { + Node newNode = new Node<>(item); + Node current = head; + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + newNode.next = current.next; + current.next = newNode; + size++; + } + } + + @Override + public E get(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index is out of bounds"); + } + Node current = head; + for (int i = 0; i < index; i++) { + current = current.next; + } + return current.data; + } + + @Override + public void set(int index, E item) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index is out of bounds"); + } + Node current = head; + for (int i = 0; i < index; i++) { + current = current.next; + } + current.data = item; + } + + @Override + public E removeFront() { + if (isEmpty()) { + throw new NoSuchElementException("List is empty"); + } + E removedItem = head.data; + head = head.next; + size--; + return removedItem; + } + + @Override + public E removeBack() { + if (isEmpty()) { + throw new NoSuchElementException("List is empty"); + } + if (size == 1) { + return removeFront(); + } + Node current = head; + while (current.next.next != null) { + current = current.next; + } + E removedItem = current.next.data; + current.next = null; + size--; + return removedItem; + } + + @Override + public void remove(E item) { + if (isEmpty()) { + return; + } + if (head.data.equals(item)) { + removeFront(); + return; + } + Node current = head; + while (current.next != null) { + if (current.next.data.equals(item)) { + current.next = current.next.next; + size--; + return; + } + current = current.next; + } + } + + @Override + public E remove(int index) { + if (index < 0 || index >= size) { + throw new IndexOutOfBoundsException("Index is out of bounds"); + } + if (index == 0) { + return removeFront(); + } + Node current = head; + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + E removedItem = current.next.data; + current.next = current.next.next; + size--; + return removedItem; + } + + @Override + public boolean contains(E item) { + Node current = head; + while (current != null) { + if (current.data.equals(item)) { + return true; + } + current = current.next; + } + return false; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public int size() { + return size; + } + + @Override + public Iterator iterator() { + return new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + private Node current = head; + + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public E next() { + if (!hasNext()) { + throw new NoSuchElementException("No more elements in list"); + } + E data = current.data; + current = current.next; + return data; + } + } +} From f045c032c9b2118b9b9ef744a77c7616398fa0f4 Mon Sep 17 00:00:00 2001 From: InfiniteGmng Date: Tue, 13 Feb 2024 20:36:53 -0800 Subject: [PATCH 04/15] Finished the JUnit tests for LinkedList --- Tests/LinkedListTests.java | 159 +++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 Tests/LinkedListTests.java diff --git a/Tests/LinkedListTests.java b/Tests/LinkedListTests.java new file mode 100644 index 0000000..183110d --- /dev/null +++ b/Tests/LinkedListTests.java @@ -0,0 +1,159 @@ +import static org.junit.Assert.*; +import org.junit.Test; + +public class LinkedListTests { + @Test + public void testAddFrontEmptyList() { + LinkedList list = new LinkedList<>(); + list.addFront(5); + assertEquals(Integer.valueOf(5), list.get(0)); + assertEquals(1, list.size()); + } + + @Test + public void testAddFrontNonEmptyList() { + LinkedList list = new LinkedList<>(); + list.addFront(5); + list.addFront(10); + assertEquals(Integer.valueOf(10), list.get(0)); + assertEquals(Integer.valueOf(5), list.get(1)); + assertEquals(2, list.size()); + } + + @Test + public void testAddBackEmptyList() { + LinkedList list = new LinkedList<>(); + list.addBack(5); + assertEquals(Integer.valueOf(5), list.get(0)); + assertEquals(1, list.size()); + } + + @Test + public void testAddBackNonEmptyList() { + LinkedList list = new LinkedList<>(); + list.addBack(5); + list.addBack(10); + assertEquals(Integer.valueOf(5), list.get(0)); + assertEquals(Integer.valueOf(10), list.get(1)); + assertEquals(2, list.size()); + } + + @Test + public void testGetEmptyList() { + LinkedList list = new LinkedList<>(); + assertNull(list.get(0)); + } + + @Test + public void testGetNonEmptyList() { + LinkedList list = new LinkedList<>(); + list.addFront(5); + list.addFront(10); + assertEquals(Integer.valueOf(10), list.get(0)); + assertEquals(Integer.valueOf(5), list.get(1)); + } + + @Test + public void testSetEmptyList() { + LinkedList list = new LinkedList<>(); + list.addFront(5); + list.set(0, 10); + assertEquals(Integer.valueOf(10), list.get(0)); + } + + @Test + public void testSetNonEmptyList() { + LinkedList list = new LinkedList<>(); + list.addFront(5); + list.addFront(10); + list.set(1, 20); + assertEquals(Integer.valueOf(10), list.get(0)); + assertEquals(Integer.valueOf(20), list.get(1)); + } + + @Test + public void testRemoveFrontEmptyList() { + LinkedList list = new LinkedList<>(); + assertNull(list.removeFront()); + } + + @Test + public void testRemoveFrontNonEmptyList() { + LinkedList list = new LinkedList<>(); + list.addFront(5); + list.addFront(10); + assertEquals(Integer.valueOf(10), list.removeFront()); + assertEquals(1, list.size()); + } + + @Test + public void testRemoveBackEmptyList() { + LinkedList list = new LinkedList<>(); + assertNull(list.removeBack()); + } + + @Test + public void testRemoveBackNonEmptyList() { + LinkedList list = new LinkedList<>(); + list.addFront(5); + list.addFront(10); + assertEquals(Integer.valueOf(5), list.removeBack()); + assertEquals(1, list.size()); + } + + @Test + public void testRemoveEmptyList() { + LinkedList list = new LinkedList<>(); + assertNull(list.remove(0)); + } + + @Test + public void testRemoveNonEmptyList() { + LinkedList list = new LinkedList<>(); + list.addFront(5); + list.addFront(10); + assertEquals(Integer.valueOf(5), list.remove(1)); + assertEquals(1, list.size()); + } + + @Test + public void testContainsEmptyList() { + LinkedList list = new LinkedList<>(); + assertFalse(list.contains(5)); + } + + @Test + public void testContainsNonEmptyList() { + LinkedList list = new LinkedList<>(); + list.addFront(5); + list.addFront(10); + assertTrue(list.contains(5)); + } + + @Test + public void testIsEmptyEmptyList() { + LinkedList list = new LinkedList<>(); + assertTrue(list.isEmpty()); + } + + @Test + public void testIsEmptyNonEmptyList() { + LinkedList list = new LinkedList<>(); + list.addFront(5); + assertFalse(list.isEmpty()); + } + + @Test + public void testSizeEmptyList() { + LinkedList list = new LinkedList<>(); + assertEquals(0, list.size()); + } + + @Test + public void testSizeNonEmptyList() { + LinkedList list = new LinkedList<>(); + list.addFront(5); + list.addFront(10); + assertEquals(2, list.size()); + } +} From ef0e4500ec7de717851c80eff8091e21304759b7 Mon Sep 17 00:00:00 2001 From: InfiniteGmng Date: Tue, 13 Feb 2024 20:45:13 -0800 Subject: [PATCH 05/15] Added documentation and runtime definitions for each method --- src/ArrayList.java | 127 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 125 insertions(+), 2 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index 8840662..e220940 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -1,16 +1,34 @@ import java.util.Iterator; import java.util.*; +/** + * Implementation of a dynamic array-based list. + * + * @author Noah Lanctot + * @version 1.0 + * @param the type of elements in this list + */ public class ArrayList implements List { private int size; private E[] buffer; + /** + * Constructs an empty ArrayList with an initial capacity of 10. + */ public ArrayList() { size = 0; buffer = (E[]) new Object[10]; } - + /** + * Adds the specified element to the front of this list. + * If the internal buffer is full, this method resizes the buffer. + * This method has a worst-case runtime complexity of O(n), + * where n is the number of elements in the list, because it may + * need to shift all existing elements to the right. + * + * @param item the element to be added to the front of this list + */ @Override public void addFront(E item) { if (size == buffer.length) { @@ -23,6 +41,14 @@ public void addFront(E item) { size++; } + /** + * Adds the specified element to the back of this list. + * If the internal buffer is full, this method resizes the buffer. + * This method has a worst-case runtime complexity of O(1), + * as it simply inserts the element at the end of the buffer. + * + * @param item the element to be added to the back of this list + */ @Override public void addBack(E item) { if (size == buffer.length) { @@ -31,6 +57,17 @@ public void addBack(E item) { buffer[size++] = item; } + /** + * Adds the specified element at the specified index in this list. + * If the internal buffer is full, this method resizes the buffer. + * This method has a worst-case runtime complexity of O(n), + * where n is the number of elements in the list, because it may + * need to shift all existing elements after the specified index to the right. + * + * @param index the index at which the specified element is to be inserted + * @param item the element to be inserted + * @throws IndexOutOfBoundsException if the index is out of range + */ @Override public void add(int index, E item) { if (index < 0 || index > size) { @@ -46,6 +83,15 @@ public void add(int index, E item) { size++; } + /** + * Returns the element at the specified index in this list. + * This method has a worst-case runtime complexity of O(1), + * as it accesses the element directly by index. + * + * @param index the index of the element to return + * @return the element at the specified index in this list + * @throws IndexOutOfBoundsException if the index is out of range + */ @Override public E get(int index) { if (index < 0 || index >= size) { @@ -54,6 +100,15 @@ public E get(int index) { return buffer[index]; } + /** + * Replaces the element at the specified index in this list with the specified element. + * This method has a worst-case runtime complexity of O(1), + * as it accesses the element directly by index. + * + * @param index the index of the element to replace + * @param item the element to be stored at the specified index + * @throws IndexOutOfBoundsException if the index is out of range + */ @Override public void set(int index, E item) { if (index < 0 || index >= size) { @@ -62,6 +117,15 @@ public void set(int index, E item) { buffer[index] = item; } + /** + * Removes and returns the element at the front of this list. + * This method has a worst-case runtime complexity of O(n), + * where n is the number of elements in the list, because it may + * need to shift all existing elements to the left. + * + * @return the element at the front of this list + * @throws NoSuchElementException if this list is empty + */ @Override public E removeFront() { if (isEmpty()) { @@ -75,6 +139,14 @@ public E removeFront() { return removedItem; } + /** + * Removes and returns the element at the back of this list. + * This method has a worst-case runtime complexity of O(1), + * as it simply removes the element at the end of the buffer. + * + * @return the element at the back of this list + * @throws NoSuchElementException if this list is empty + */ @Override public E removeBack() { if (isEmpty()) { @@ -85,6 +157,14 @@ public E removeBack() { return removedItem; } + /** + * Removes the first occurrence of the specified element from this list, if it is present. + * This method has a worst-case runtime complexity of O(n), + * where n is the number of elements in the list, because it may + * need to shift all existing elements after the removed element to the left. + * + * @param item the element to be removed from this list + */ @Override public void remove(E item) { for (int i = 0; i < size; i++) { @@ -98,6 +178,16 @@ public void remove(E item) { } } + /** + * Removes and returns the element at the specified index in this list. + * This method has a worst-case runtime complexity of O(n), + * where n is the number of elements in the list, because it may + * need to shift all existing elements after the specified index to the left. + * + * @param index the index of the element to be removed + * @return the element previously at the specified position + * @throws IndexOutOfBoundsException if the index is out of range + */ @Override public E remove(int index) { if (index < 0 || index >= size) { @@ -111,6 +201,15 @@ public E remove(int index) { return removedItem; } + /** + * Returns true if this list contains the specified element. + * This method has a worst-case runtime complexity of O(n), + * where n is the number of elements in the list, because it may + * need to iterate through all elements to find the specified element. + * + * @param item the element to be checked for containment in this list + * @return true if this list contains the specified element + */ @Override public boolean contains(E item) { for (int i = 0; i < size; i++) { @@ -121,16 +220,34 @@ public boolean contains(E item) { return false; } + /** + * Returns true if this list contains no elements. + * This method has a runtime complexity of O(1). + * + * @return true if this list contains no elements + */ @Override public boolean isEmpty() { return size == 0; } + /** + * Returns the number of elements in this list. + * This method has a runtime complexity of O(1). + * + * @return the number of elements in this list + */ @Override public int size() { return size; } + /** + * Resizes the internal buffer by doubling its capacity. + * This method has a worst-case runtime complexity of O(n), + * where n is the number of elements in the list, because it + * needs to copy all existing elements to the new buffer. + */ private void resize() { E[] newBuffer = (E[]) new Object[size * 2]; for (int i = 0; i < size; i++) { @@ -139,6 +256,12 @@ private void resize() { buffer = newBuffer; } + /** + * Returns an iterator over the elements in this list. + * This method has a runtime complexity of O(1). + * + * @return an iterator over the elements in this list + */ @Override public Iterator iterator() { return new ArrayListIterator(); @@ -160,4 +283,4 @@ public E next() { return buffer[index++]; } } -} +} \ No newline at end of file From b5b6c20c19575ce47edcb1c6484977ee141d46ea Mon Sep 17 00:00:00 2001 From: InfiniteGmng Date: Tue, 13 Feb 2024 20:47:15 -0800 Subject: [PATCH 06/15] Added documentation and runtime definitions for each method --- src/LinkedList.java | 117 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 116 insertions(+), 1 deletion(-) diff --git a/src/LinkedList.java b/src/LinkedList.java index 593a106..81b54b6 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -1,11 +1,21 @@ import java.util.Iterator; import java.util.NoSuchElementException; +/** + * Implementation of a singly linked list. + * + * @author Noah Lanctot + * @version 1.0 + * @param the type of elements in this list + */ public class LinkedList implements List { private Node head; private int size; + /** + * Constructs an empty LinkedList. + */ public LinkedList() { head = null; size = 0; @@ -21,6 +31,12 @@ public Node(E data) { } } + /** + * Adds the specified element to the front of this list. + * This method has a runtime complexity of O(1). + * + * @param item the element to be added to the front of this list + */ @Override public void addFront(E item) { Node newNode = new Node<>(item); @@ -29,6 +45,14 @@ public void addFront(E item) { size++; } + /** + * Adds the specified element to the back of this list. + * This method has a runtime complexity of O(n), + * where n is the number of elements in the list, because it + * may need to traverse the list to find the last node. + * + * @param item the element to be added to the back of this list + */ @Override public void addBack(E item) { if (head == null) { @@ -44,6 +68,16 @@ public void addBack(E item) { } } + /** + * Adds the specified element at the specified index in this list. + * This method has a runtime complexity of O(n), + * where n is the number of elements in the list, because it + * may need to traverse the list to find the node before the specified index. + * + * @param index the index at which the specified element is to be inserted + * @param item the element to be inserted + * @throws IndexOutOfBoundsException if the index is out of range + */ @Override public void add(int index, E item) { if (index < 0 || index > size) { @@ -63,6 +97,16 @@ public void add(int index, E item) { } } + /** + * Returns the element at the specified index in this list. + * This method has a runtime complexity of O(n), + * where n is the index of the element to be retrieved, + * because it may need to traverse the list to find the node at the specified index. + * + * @param index the index of the element to return + * @return the element at the specified index in this list + * @throws IndexOutOfBoundsException if the index is out of range + */ @Override public E get(int index) { if (index < 0 || index >= size) { @@ -75,6 +119,16 @@ public E get(int index) { return current.data; } + /** + * Replaces the element at the specified index in this list with the specified element. + * This method has a runtime complexity of O(n), + * where n is the index of the element to be replaced, + * because it may need to traverse the list to find the node at the specified index. + * + * @param index the index of the element to replace + * @param item the element to be stored at the specified index + * @throws IndexOutOfBoundsException if the index is out of range + */ @Override public void set(int index, E item) { if (index < 0 || index >= size) { @@ -87,6 +141,13 @@ public void set(int index, E item) { current.data = item; } + /** + * Removes and returns the element at the front of this list. + * This method has a runtime complexity of O(1). + * + * @return the element at the front of this list + * @throws NoSuchElementException if this list is empty + */ @Override public E removeFront() { if (isEmpty()) { @@ -98,6 +159,15 @@ public E removeFront() { return removedItem; } + /** + * Removes and returns the element at the back of this list. + * This method has a runtime complexity of O(n), + * where n is the number of elements in the list, because it + * may need to traverse the list to find the second-to-last node. + * + * @return the element at the back of this list + * @throws NoSuchElementException if this list is empty + */ @Override public E removeBack() { if (isEmpty()) { @@ -116,6 +186,14 @@ public E removeBack() { return removedItem; } + /** + * Removes the first occurrence of the specified element from this list, if it is present. + * This method has a runtime complexity of O(n), + * where n is the number of elements in the list, because it + * may need to traverse the list to find the specified element. + * + * @param item the element to be removed from this list + */ @Override public void remove(E item) { if (isEmpty()) { @@ -136,6 +214,16 @@ public void remove(E item) { } } + /** + * Removes and returns the element at the specified index in this list. + * This method has a runtime complexity of O(n), + * where n is the index of the element to be removed, + * because it may need to traverse the list to find the node before the specified index. + * + * @param index the index of the element to be removed + * @return the element previously at the specified position + * @throws IndexOutOfBoundsException if the index is out of range + */ @Override public E remove(int index) { if (index < 0 || index >= size) { @@ -154,6 +242,15 @@ public E remove(int index) { return removedItem; } + /** + * Returns true if this list contains the specified element. + * This method has a runtime complexity of O(n), + * where n is the number of elements in the list, because it + * may need to traverse the list to find the specified element. + * + * @param item the element to be checked for containment in this list + * @return true if this list contains the specified element + */ @Override public boolean contains(E item) { Node current = head; @@ -166,16 +263,34 @@ public boolean contains(E item) { return false; } + /** + * Returns true if this list contains no elements. + * This method has a runtime complexity of O(1). + * + * @return true if this list contains no elements + */ @Override public boolean isEmpty() { return size == 0; } + /** + * Returns the number of elements in this list. + * This method has a runtime complexity of O(1). + * + * @return the number of elements in this list + */ @Override public int size() { return size; } + /** + * Returns an iterator over the elements in this list. + * This method has a runtime complexity of O(1). + * + * @return an iterator over the elements in this list + */ @Override public Iterator iterator() { return new LinkedListIterator(); @@ -199,4 +314,4 @@ public E next() { return data; } } -} +} \ No newline at end of file From d4021ec3a0ff1579ba8a5cb4e8768367b27060dc Mon Sep 17 00:00:00 2001 From: InfiniteGmng Date: Mon, 19 Feb 2024 21:33:49 -0800 Subject: [PATCH 07/15] Added the StackTestClient to the main class and method to test the ResizingArrayStack class --- src/Main.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Main.java b/src/Main.java index 8f8f984..9ea9e18 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,10 +1,18 @@ -//TIP To Run code, press or -// click the icon in the gutter. +import java.util.Scanner; + 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.println("Hello and welcome!"); + 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.pop(); + } else if (!s.isEmpty()) { + System.out.println(s.pop() + " "); + } + } + System.out.println("(" + s.size() + " left on the stack)"); } } \ No newline at end of file From d9b28c498aa8e33175b500847809def068436a41 Mon Sep 17 00:00:00 2001 From: InfiniteGmng Date: Mon, 19 Feb 2024 21:34:39 -0800 Subject: [PATCH 08/15] Created Bag interface that includes the methods that are in the book --- src/Bag.java | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/Bag.java diff --git a/src/Bag.java b/src/Bag.java new file mode 100644 index 0000000..61b1e54 --- /dev/null +++ b/src/Bag.java @@ -0,0 +1,6 @@ +public interface Bag extends Iterable { + Item Bag(); + void add(Item item); + boolean isEmpty(); + int size(); +} From 9749a20e445ea837779eef899a449f2978150440 Mon Sep 17 00:00:00 2001 From: InfiniteGmng Date: Mon, 19 Feb 2024 21:50:59 -0800 Subject: [PATCH 09/15] Moved the stack testing method to the Tests directory and finished implementing the methods in the ResizingArrayStack class --- Tests/StackTestClient.java | 19 +++++++++++++++++++ src/Main.java | 15 +-------------- 2 files changed, 20 insertions(+), 14 deletions(-) create mode 100644 Tests/StackTestClient.java diff --git a/Tests/StackTestClient.java b/Tests/StackTestClient.java new file mode 100644 index 0000000..327eada --- /dev/null +++ b/Tests/StackTestClient.java @@ -0,0 +1,19 @@ +import java.util.Scanner; + +public class StackTestClient { + public static void main(String[] args) { + Stack stack = new ResizingArrayStack(); + Scanner console = new Scanner("to be or not to - be - - that - - - is"); + while (console.hasNext()) { + String item = console.next(); + if (item.equals("-")) { + if (!stack.isEmpty()) { + System.out.println(stack.pop() + " "); + } + } else { + stack.push(item); + } + } + System.out.println("(" + stack.size() + " left on the stack)"); + } +} diff --git a/src/Main.java b/src/Main.java index 9ea9e18..503e72c 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,18 +1,5 @@ -import java.util.Scanner; - public class Main { 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.pop(); - } else if (!s.isEmpty()) { - System.out.println(s.pop() + " "); - } - } - System.out.println("(" + s.size() + " left on the stack)"); - + System.out.println("Hello World!"); } } \ No newline at end of file From 62c9b67d3429c153ed73c6ccc6e6147dc38fd122 Mon Sep 17 00:00:00 2001 From: InfiniteGmng Date: Mon, 19 Feb 2024 21:51:32 -0800 Subject: [PATCH 10/15] Moved the stack testing method to the Tests directory and finished implementing the methods in the ResizingArrayStack class --- src/ResizingArrayStack.java | 81 +++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/ResizingArrayStack.java diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java new file mode 100644 index 0000000..33ab58c --- /dev/null +++ b/src/ResizingArrayStack.java @@ -0,0 +1,81 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class ResizingArrayStack implements Stack { + private E[] array; + private int size; + + public ResizingArrayStack() { + array = (E[]) new Object[10]; + size = 0; + } + + private void resize(int capacity) { + E[] newArray = (E[]) new Object[capacity]; + for (int i = 0; i < size; i++) { + newArray[i] = array[i]; + } + array = newArray; + } + + @Override + public void push(E item) { + if (size == array.length) { + resize(2 * array.length); + } + array[size++] = item; + } + + @Override + public E pop() { + if (isEmpty()) { + throw new NoSuchElementException("Stack underflow"); + } + E item = array[--size]; + array[size] = null; + if (size > 0 && size == array.length / 4) { + resize(array.length / 2); + } + return item; + } + + @Override + public E peek() { + if (isEmpty()) { + throw new NoSuchElementException("Stack underflow"); + } + return array[size - 1]; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public int size() { + return size; + } + + @Override + public Iterator iterator() { + return new ArrayIterator(); + } + + private class ArrayIterator implements Iterator { + private int index = size - 1; + + @Override + public boolean hasNext() { + return index >= 0; + } + + @Override + public E next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + return array[index--]; + } + } +} \ No newline at end of file From c6a8636e5277dee844340aac7a4effe080592595 Mon Sep 17 00:00:00 2001 From: InfiniteGmng Date: Mon, 19 Feb 2024 22:03:39 -0800 Subject: [PATCH 11/15] Finished implementing the methods in the LinkedStack class and then added the test method to the StackTestClient class --- Tests/StackTestClient.java | 17 ++++++++ src/LinkedStack.java | 85 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 src/LinkedStack.java diff --git a/Tests/StackTestClient.java b/Tests/StackTestClient.java index 327eada..e74382c 100644 --- a/Tests/StackTestClient.java +++ b/Tests/StackTestClient.java @@ -15,5 +15,22 @@ public static void main(String[] args) { } } System.out.println("(" + stack.size() + " left on the stack)"); + + System.out.println(); + System.out.println(); + + 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("-")) { + if (!s.isEmpty()) { + System.out.println(s.pop() + " "); + } + } else { + s.push(item); + } + } + System.out.println("(" + s.size() + " left on the stack)"); } } diff --git a/src/LinkedStack.java b/src/LinkedStack.java new file mode 100644 index 0000000..cc0a810 --- /dev/null +++ b/src/LinkedStack.java @@ -0,0 +1,85 @@ +import java.util.Iterator; + +public class LinkedStack implements Stack { + + private Node top; + private int size; + + private static class Node { + private E data; + private Node next; + + public Node(E data) { + this.data = data; + this.next = null; + } + } + + public LinkedStack() { + top = null; + size = 0; + } + + @Override + public void push(E item) { + Node newNode = new Node<>(item); + + newNode.next = top; + top = newNode; + size++; + } + + @Override + public E pop() { + if (isEmpty()) { + throw new IllegalStateException("Stack is empty"); + } + + E data = top.data; + top = top.next; + size--; + + return data; + } + + @Override + public E peek() { + if (isEmpty()) { + throw new IllegalStateException("Stack is empty"); + } + + return top.data; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public int size() { + return size; + } + + @Override + public Iterator iterator() { + return new Iterator() { + private Node current = top; + + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public E next() { + if (!hasNext()) { + throw new IllegalStateException("No more elements in the stack"); + } + E data = current.data; + current = current.next; + return data; + } + }; + } +} \ No newline at end of file From 72099736c9dc2d3480db50c0c84479fcae09aa6d Mon Sep 17 00:00:00 2001 From: InfiniteGmng Date: Mon, 19 Feb 2024 22:14:04 -0800 Subject: [PATCH 12/15] Finished implementing the methods in the LinkedQueue class and then updated and added the test method to the QueueTestClient class --- Tests/QueueTestClient.java | 17 ++++++++ src/LinkedQueue.java | 79 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 Tests/QueueTestClient.java create mode 100644 src/LinkedQueue.java diff --git a/Tests/QueueTestClient.java b/Tests/QueueTestClient.java new file mode 100644 index 0000000..9cd0f96 --- /dev/null +++ b/Tests/QueueTestClient.java @@ -0,0 +1,17 @@ +import java.util.Scanner; + +public class QueueTestClient { + public static void main(String[] args) { + Queue queue = new LinkedQueue(); + Scanner console = new Scanner("to be or not to - be - - that - - - is"); + while (console.hasNext()) { + String item = console.next(); + if (!item.equals("-")) { + queue.enqueue(item); + } else if (!queue.isEmpty()) { + System.out.print(queue.dequeue() + " "); + } + } + System.out.println("(" + queue.size() + " left in the queue)"); + } +} \ No newline at end of file diff --git a/src/LinkedQueue.java b/src/LinkedQueue.java new file mode 100644 index 0000000..30b7c0d --- /dev/null +++ b/src/LinkedQueue.java @@ -0,0 +1,79 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class LinkedQueue implements Queue { + + private Node first; + private Node last; + private int size; + + private static class Node { + private E item; + private Node next; + } + + @Override + public void enqueue(E item) { + Node newNode = new Node<>(); + newNode.item = item; + newNode.next = null; + + if (isEmpty()) { + first = newNode; + } else { + last.next = newNode; + } + + last = newNode; + size++; + } + + @Override + public E dequeue() { + if (isEmpty()) { + throw new NoSuchElementException("Queue underflow"); + } + + E item = first.item; + first = first.next; + size--; + if (isEmpty()) { + last = null; + } + return item; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public int size() { + return size; + } + + @Override + public Iterator iterator() { + return new QueueIterator(); + } + + private class QueueIterator implements Iterator { + private Node current = first; + + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public E next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + E item = current.item; + current = current.next; + return item; + } + } +} \ No newline at end of file From d19a3f748edde7cf0dbdad124fc9482aea3841f3 Mon Sep 17 00:00:00 2001 From: InfiniteGmng Date: Mon, 19 Feb 2024 22:27:44 -0800 Subject: [PATCH 13/15] Finished implementing the methods in the LinkedBag class and then updated and added the test method to the Stats class --- Tests/Stats.java | 35 ++++++++++++++++++++++++++ src/Bag.java | 1 - src/LinkedBag.java | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 Tests/Stats.java create mode 100644 src/LinkedBag.java diff --git a/Tests/Stats.java b/Tests/Stats.java new file mode 100644 index 0000000..6f5ee10 --- /dev/null +++ b/Tests/Stats.java @@ -0,0 +1,35 @@ +import java.util.Scanner; + +public class Stats { + public static void main(String[] args) { + Bag numbers = new LinkedBag<>(); + Scanner console = new Scanner("100 99 101 120 98 107 109 81 101 90"); + while (console.hasNextDouble()) { + numbers.add(console.nextDouble()); + } + console.close(); + + int count = 0; + double sum = 0; + double sumOfSquares = 0; + + for (double num : numbers) { + count++; + sum += num; + sumOfSquares += num * num; + } + + double mean = sum / count; + double variance = (sumOfSquares / count) - (mean * mean); + double stddev = Math.sqrt(variance); + + // Print numbers + for (double num : numbers) { + System.out.println(num); + } + + // Print mean and standard deviation + System.out.printf("%nMean: %.2f%n", mean); + System.out.printf("Stddev: %.2f%n", stddev); + } +} \ No newline at end of file diff --git a/src/Bag.java b/src/Bag.java index 61b1e54..cec3c34 100644 --- a/src/Bag.java +++ b/src/Bag.java @@ -1,5 +1,4 @@ public interface Bag extends Iterable { - Item Bag(); void add(Item item); boolean isEmpty(); int size(); diff --git a/src/LinkedBag.java b/src/LinkedBag.java new file mode 100644 index 0000000..7a31bce --- /dev/null +++ b/src/LinkedBag.java @@ -0,0 +1,61 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class LinkedBag implements Bag { + + private Node first; + private int size; + + private static class Node { + private E item; + private Node next; + } + + public LinkedBag() { + first = null; + size = 0; + } + + @Override + public void add(E e) { + Node newNode = new Node<>(); + newNode.item = e; + newNode.next = first; + first = newNode; + size++; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public int size() { + return size; + } + + @Override + public Iterator iterator() { + return new BagIterator(); + } + + private class BagIterator implements Iterator { + private Node current = first; + + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public E next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + E item = current.item; + current = current.next; + return item; + } + } +} \ No newline at end of file From 753b008c26ecff0db5ba85d9032bd66fcf5c197b Mon Sep 17 00:00:00 2001 From: InfiniteGmng Date: Mon, 19 Feb 2024 22:44:26 -0800 Subject: [PATCH 14/15] Added documentation and Big O notation for the runtime --- src/LinkedBag.java | 26 +++++++++++++++++++++ src/LinkedQueue.java | 30 +++++++++++++++++++++++++ src/LinkedStack.java | 40 +++++++++++++++++++++++++++++++++ src/ResizingArrayStack.java | 45 +++++++++++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+) diff --git a/src/LinkedBag.java b/src/LinkedBag.java index 7a31bce..80717a8 100644 --- a/src/LinkedBag.java +++ b/src/LinkedBag.java @@ -11,11 +11,20 @@ private static class Node { private Node next; } + /** + * Initializes an empty bag. + */ public LinkedBag() { first = null; size = 0; } + /** + * Adds an item to the bag. + * Time complexity: O(1) (amortized), O(N) (worst-case when resizing) + * + * @param e the item to add + */ @Override public void add(E e) { Node newNode = new Node<>(); @@ -25,16 +34,33 @@ public void add(E e) { size++; } + /** + * Checks whether the bag is empty. + * Time complexity: O(1) (amortized), O(N) (worst-case when resizing) + * + * @return true if the bag is empty, false otherwise + */ @Override public boolean isEmpty() { return size == 0; } + /** + * Returns the number of items in the bag. + * Time complexity: O(1) (amortized), O(N) (worst-case when resizing) + * + * @return the number of items in the bag + */ @Override public int size() { return size; } + /** + * Returns an iterator to traverse the items in the bag. + * + * @return an iterator to traverse the items in the bag + */ @Override public Iterator iterator() { return new BagIterator(); diff --git a/src/LinkedQueue.java b/src/LinkedQueue.java index 30b7c0d..fae3c82 100644 --- a/src/LinkedQueue.java +++ b/src/LinkedQueue.java @@ -12,6 +12,12 @@ private static class Node { private Node next; } + /** + * Adds an item to the end of the queue. + * Time complexity: O(1) (amortized), O(N) (worst-case when resizing) + * + * @param item the item to enqueue + */ @Override public void enqueue(E item) { Node newNode = new Node<>(); @@ -28,6 +34,13 @@ public void enqueue(E item) { size++; } + /** + * Removes and returns the item least recently added to this queue. + * Time complexity: O(1) (amortized), O(N) (worst-case when resizing) + * + * @return the item least recently added + * @throws NoSuchElementException if the queue is empty + */ @Override public E dequeue() { if (isEmpty()) { @@ -43,16 +56,33 @@ public E dequeue() { return item; } + /** + * Checks whether the queue is empty. + * Time complexity: O(1) (amortized), O(N) (worst-case when resizing) + * + * @return true if the queue is empty, false otherwise + */ @Override public boolean isEmpty() { return size == 0; } + /** + * Returns the number of items in the queue. + * Time complexity: O(1) (amortized), O(N) (worst-case when resizing) + * + * @return the number of items in the queue + */ @Override public int size() { return size; } + /** + * Returns an iterator to traverse the items in the queue. + * + * @return an iterator to traverse the items in the queue + */ @Override public Iterator iterator() { return new QueueIterator(); diff --git a/src/LinkedStack.java b/src/LinkedStack.java index cc0a810..1dfce2c 100644 --- a/src/LinkedStack.java +++ b/src/LinkedStack.java @@ -15,11 +15,20 @@ public Node(E data) { } } + /** + * Initializes an empty stack. + */ public LinkedStack() { top = null; size = 0; } + /** + * Adds an item to the top of the stack. + * Time complexity: O(1) (amortized), O(N) (worst-case when resizing) + * + * @param item the item to be added + */ @Override public void push(E item) { Node newNode = new Node<>(item); @@ -29,6 +38,13 @@ public void push(E item) { size++; } + /** + * Removes and returns the item most recently added to this stack. + * Time complexity: O(1) (amortized), O(N) (worst-case when resizing) + * + * @return the item most recently added + * @throws IllegalStateException if the stack is empty + */ @Override public E pop() { if (isEmpty()) { @@ -42,6 +58,13 @@ public E pop() { return data; } + /** + * Returns the item most recently added to this stack without removing it. + * Time complexity: O(1) (amortized), O(N) (worst-case when resizing) + * + * @return the item most recently added + * @throws IllegalStateException if the stack is empty + */ @Override public E peek() { if (isEmpty()) { @@ -51,16 +74,33 @@ public E peek() { return top.data; } + /** + * Checks whether the stack is empty. + * Time complexity: O(1) (amortized), O(N) (worst-case when resizing) + * + * @return true if the stack is empty, false otherwise + */ @Override public boolean isEmpty() { return size == 0; } + /** + * Returns the number of items in the stack. + * Time complexity: O(1) (amortized), O(N) (worst-case when resizing) + * + * @return the number of items in the stack + */ @Override public int size() { return size; } + /** + * Returns an iterator to traverse the items in the stack. + * + * @return an iterator to traverse the items in the stack + */ @Override public Iterator iterator() { return new Iterator() { diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java index 33ab58c..44fbb9d 100644 --- a/src/ResizingArrayStack.java +++ b/src/ResizingArrayStack.java @@ -5,11 +5,19 @@ public class ResizingArrayStack implements Stack { private E[] array; private int size; + /** + * Initializes an empty stack with an initial capacity of 10. + */ public ResizingArrayStack() { array = (E[]) new Object[10]; size = 0; } + /** + * Resizes the array to the given capacity. + * + * @param capacity the new capacity of the array + */ private void resize(int capacity) { E[] newArray = (E[]) new Object[capacity]; for (int i = 0; i < size; i++) { @@ -18,6 +26,12 @@ private void resize(int capacity) { array = newArray; } + /** + * Adds an item to the top of the stack. + * Time complexity: O(1) (amortized), O(N) (worst-case when resizing) + * + * @param item the item to be added + */ @Override public void push(E item) { if (size == array.length) { @@ -26,6 +40,13 @@ public void push(E item) { array[size++] = item; } + /** + * Removes and returns the item most recently added to this stack. + * Time complexity: O(1) (amortized), O(N) (worst-case when resizing) + * + * @return the item most recently added + * @throws NoSuchElementException if the stack is empty + */ @Override public E pop() { if (isEmpty()) { @@ -39,6 +60,13 @@ public E pop() { return item; } + /** + * Returns the item most recently added to this stack without removing it. + * Time complexity: O(1) + * + * @return the item most recently added + * @throws NoSuchElementException if the stack is empty + */ @Override public E peek() { if (isEmpty()) { @@ -47,16 +75,33 @@ public E peek() { return array[size - 1]; } + /** + * Checks whether the stack is empty. + * Time complexity: O(1) + * + * @return true if the stack is empty, false otherwise + */ @Override public boolean isEmpty() { return size == 0; } + /** + * Returns the number of items in the stack. + * Time complexity: O(1) + * + * @return the number of items in the stack + */ @Override public int size() { return size; } + /** + * Returns an iterator to traverse the items in the stack. + * + * @return an iterator to traverse the items in the stack + */ @Override public Iterator iterator() { return new ArrayIterator(); From cc892ae385a34058d04580467efd18795704a853 Mon Sep 17 00:00:00 2001 From: InfiniteGmng Date: Mon, 19 Feb 2024 22:50:02 -0800 Subject: [PATCH 15/15] Added documentation for the class with author and version and description --- src/LinkedBag.java | 7 +++++++ src/LinkedQueue.java | 7 +++++++ src/LinkedStack.java | 7 +++++++ src/ResizingArrayStack.java | 7 +++++++ 4 files changed, 28 insertions(+) diff --git a/src/LinkedBag.java b/src/LinkedBag.java index 80717a8..17031f6 100644 --- a/src/LinkedBag.java +++ b/src/LinkedBag.java @@ -1,6 +1,13 @@ import java.util.Iterator; import java.util.NoSuchElementException; +/** + * LinkedBag represents a bag (or multiset) data structure implemented using a linked list. + * + * @param the type of elements stored in the stack + * @author Noah Lanctot + * @version 1.0 + */ public class LinkedBag implements Bag { private Node first; diff --git a/src/LinkedQueue.java b/src/LinkedQueue.java index fae3c82..10d16c4 100644 --- a/src/LinkedQueue.java +++ b/src/LinkedQueue.java @@ -1,6 +1,13 @@ import java.util.Iterator; import java.util.NoSuchElementException; +/** + * LinkedQueue represents a queue data structure implemented using a linked list. + * + * @param the type of elements stored in the stack + * @author Noah Lanctot + * @version 1.0 + */ public class LinkedQueue implements Queue { private Node first; diff --git a/src/LinkedStack.java b/src/LinkedStack.java index 1dfce2c..c9e4792 100644 --- a/src/LinkedStack.java +++ b/src/LinkedStack.java @@ -1,5 +1,12 @@ import java.util.Iterator; +/** + * LinkedStack represents a stack data structure implemented using a linked list. + * + * @param the type of elements stored in the stack + * @author Noah Lanctot + * @version 1.0 + */ public class LinkedStack implements Stack { private Node top; diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java index 44fbb9d..ec0ce0f 100644 --- a/src/ResizingArrayStack.java +++ b/src/ResizingArrayStack.java @@ -1,6 +1,13 @@ import java.util.Iterator; import java.util.NoSuchElementException; +/** + * Implementation of a stack using a resizing array. + * + * @param the type of elements stored in the stack + * @author Noah Lanctot + * @version 1.0 + */ public class ResizingArrayStack implements Stack { private E[] array; private int size;