From 298f1109b4e9b74d83256cee7582e84da762e787 Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Tue, 13 Feb 2024 22:07:06 -0800 Subject: [PATCH 01/20] added arraylist, started the methods --- .idea/misc.xml | 1 - src/ArrayList.java | 168 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 src/ArrayList.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 6f29fee..5af9c98 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/src/ArrayList.java b/src/ArrayList.java new file mode 100644 index 0000000..7a2eaa9 --- /dev/null +++ b/src/ArrayList.java @@ -0,0 +1,168 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + private int size; + private E[] buffer; + + public ArrayList() { + buffer = (E[]) new Object[10]; + size = 0; +} + /** + * Add item to the front. + * + * @param item the item to be added + */ + @Override + public void addFront(E item) { + for (int i = size - 1; i>=0; i--){ // shift elements + buffer[i+1] = buffer[i]; + } + buffer[0] = item; + size++; + } + + /** + * Add item to the back. + * + * @param item the item to be added + */ + @Override + public void addBack(E item) { + + } + + /** + * Add an item at specified index (position). + * + * @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) { + + } + + /** + * Get the item at a specified index. + * + * @param i the index where the item should be retrieved + * @return the item located at that index + */ + @Override + public E get(int i) { + return null; + } + + /** + * Set (save) an item at a specified index. Previous + * item at that index is overwritten. + * + * @param i the index where the item should be saved + * @param item the item to be saved + */ + @Override + public void set(int i, Object item) { + + } + + /** + * Remove item at the front of the list. + * + * @return the item that was removed + */ + @Override + public E removeFront() { + return null; + } + + /** + * Remove item at the back of the list + * + * @return the item that was removed + */ + @Override + public E removeBack() { + return null; + } + + /** + * Remove item from the list + * + * @param item the item to be removed + */ + @Override + public void remove(E item) { + + } + + /** + * Remove item at a specified index. + * + * @param i the index where the item should be removed + * @return the item that was removed + */ + @Override + public E remove(int i) { + return null; + } + + /** + * Checks if an item is in the list. + * + * @param item the item to search for + * @return true if the item is in the list, false otherwise + */ + @Override + public boolean contains(E item) { + return false; + } + + /** + * Checks if the list is empty. + * + * @return true if the list is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Provides a count of the number of items in the list. + * + * @return number of items in the list + */ + @Override + public int size() { + return 0; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } + private class ArrayListIterator implements Iterator { + private int currentIndex = 0; + + @Override + public boolean hasNext() { + return currentIndex < size; + } + + @Override + public E next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + return buffer[currentIndex++]; + } + } + +} From 7739cfb6d1be7abcc43c924a5c44460021e5ee6f Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Tue, 13 Feb 2024 22:24:07 -0800 Subject: [PATCH 02/20] worked on a more methods!... still waiting to test them. --- src/ArrayList.java | 279 +++++++++++++++++++++++++-------------------- 1 file changed, 158 insertions(+), 121 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index 7a2eaa9..7ceb9db 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -1,3 +1,4 @@ +import java.util.Arrays; import java.util.Iterator; import java.util.NoSuchElementException; @@ -8,7 +9,8 @@ public class ArrayList implements List { public ArrayList() { buffer = (E[]) new Object[10]; size = 0; -} + } + /** * Add item to the front. * @@ -16,8 +18,8 @@ public ArrayList() { */ @Override public void addFront(E item) { - for (int i = size - 1; i>=0; i--){ // shift elements - buffer[i+1] = buffer[i]; + for (int i = size - 1; i >= 0; i--) { // shift elements + buffer[i + 1] = buffer[i]; } buffer[0] = item; size++; @@ -30,139 +32,174 @@ public void addFront(E item) { */ @Override public void addBack(E item) { + increaseBuffer(size + 1); + buffer[size++] = item; + } - } - - /** - * Add an item at specified index (position). - * - * @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) { - - } - - /** - * Get the item at a specified index. - * - * @param i the index where the item should be retrieved - * @return the item located at that index - */ - @Override - public E get(int i) { - return null; - } - - /** - * Set (save) an item at a specified index. Previous - * item at that index is overwritten. - * - * @param i the index where the item should be saved - * @param item the item to be saved - */ - @Override - public void set(int i, Object item) { - - } + /** + * Add an item at specified index (position). + * + * @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){ - /** - * Remove item at the front of the list. - * - * @return the item that was removed - */ - @Override - public E removeFront() { - return null; - } + } - /** - * Remove item at the back of the list - * - * @return the item that was removed - */ - @Override - public E removeBack() { - return null; - } + /** + * Get the item at a specified index. + * + * @param i the index where the item should be retrieved + * @return the item located at that index + */ + @Override + public E get(int i) { + if (i < 0 || i >= size) { + throw new IndexOutOfBoundsException("index is out of bounds!"); + } + return buffer[i]; + } - /** - * Remove item from the list - * - * @param item the item to be removed - */ - @Override - public void remove(E item) { + /** + * Set (save) an item at a specified index. Previous + * item at that index is overwritten. + * + * @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 < 0 || i >= size) { + throw new IndexOutOfBoundsException("index is out of bounds!"); + } + buffer[i] = item; + } - } + /** + * Remove item at the front of the list. + * + * @return the item that was removed + */ + @Override + public E removeFront () { + if (isEmpty()) { + throw new NoSuchElementException("List is Empty!"); + } + E removedItem = buffer[0]; + for (int i = 0; i < size - 1 ; size++) { + buffer[i] = buffer[i + 1]; + } + size--; + return removedItem; + } - /** - * Remove item at a specified index. - * - * @param i the index where the item should be removed - * @return the item that was removed - */ - @Override - public E remove(int i) { - return null; - } + /** + * Remove item at the back of the list + * + * @return the item that was removed + */ + @Override + public E removeBack () { + if (isEmpty()) { + throw new NoSuchElementException("List is Empty!"); + } + E removedItem = buffer[size - 1]; + buffer[size - 1] = null; + size--; + return removedItem; + } - /** - * Checks if an item is in the list. - * - * @param item the item to search for - * @return true if the item is in the list, false otherwise - */ - @Override - public boolean contains(E item) { - return false; - } + /** + * Remove item from the list + * + * @param item the item to be removed + */ + @Override + public void remove(E item) { + for (int i = 0; i < size; i++) { + if (buffer[i].equals(item)) { + remove(i); + return; + } + } + size--; + } - /** - * Checks if the list is empty. - * - * @return true if the list is empty, false otherwise - */ - @Override - public boolean isEmpty() { - return false; - } + /** + * Remove item at a specified index. + * + * @param i the index where the item should be removed + * @return the item that was removed + */ + @Override + public E remove ( int i){ + return null; + } - /** - * Provides a count of the number of items in the list. - * - * @return number of items in the list - */ - @Override - public int size() { - return 0; - } + /** + * Checks if an item is in the list. + * + * @param item the item to search for + * @return true if the item is in the list, false otherwise + */ + @Override + public boolean contains (E item){ + return false; + } - /** - * Returns an iterator over elements of type {@code T}. - * - * @return an Iterator. - */ - @Override - public Iterator iterator() { - return null; - } - private class ArrayListIterator implements Iterator { - private int currentIndex = 0; + /** + * Checks if the list is empty. + * + * @return true if the list is empty, false otherwise + */ + @Override + public boolean isEmpty () { + return false; + } + /** + * Provides a count of the number of items in the list. + * + * @return number of items in the list + */ @Override - public boolean hasNext() { - return currentIndex < size; + public int size () { + return 0; } + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ @Override - public E next() { - if (!hasNext()) { - throw new NoSuchElementException(); + public Iterator iterator () { + return null; + } + private class ArrayListIterator implements Iterator { + private int currentIndex = 0; + + @Override + public boolean hasNext() { + return currentIndex < size; + } + + @Override + public E next() { + if (!hasNext()) { + throw new NoSuchElementException("no index exists!"); + } + return buffer[currentIndex++]; + } + } + private void increaseBuffer ( int increaseBuffer){ + if (increaseBuffer > buffer.length) { + int newBuffer = buffer.length * 2; + if (newBuffer < increaseBuffer) { + newBuffer = increaseBuffer; + } + buffer = Arrays.copyOf(buffer, newBuffer); } - return buffer[currentIndex++]; } } - -} From 6632dc33f1178ee2b4c4eee2a5ca17afe2252c38 Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Tue, 13 Feb 2024 22:50:02 -0800 Subject: [PATCH 03/20] added more methods, couple more left. --- src/ArrayList.java | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index 7ceb9db..be0a4a9 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -43,8 +43,10 @@ public void addBack(E item) { * @param item the item to be added */ @Override - public void add ( int i, E item){ - + public void add(int i, E item) { + if (i < 0 || i > size) { + throw new IndexOutOfBoundsException(); + } } /** @@ -133,10 +135,17 @@ public void remove(E item) { * @return the item that was removed */ @Override - public E remove ( int i){ - return null; + public E remove(int i) { + if (i < 0 || i >= size) { + throw new IndexOutOfBoundsException(); + } + E removedItem = buffer[i]; + for (int j = 0; j < size-1; j++ ) { + buffer[j] = buffer[j + 1]; // to avoid memory leak + } + size--; + return removedItem; } - /** * Checks if an item is in the list. * @@ -144,8 +153,17 @@ public E remove ( int i){ * @return true if the item is in the list, false otherwise */ @Override - public boolean contains (E item){ - return false; + public boolean contains(E item) { + if (isEmpty()) { + return false; + } + for (int i = 0; i < size; i++) { + if (buffer[i].equals(item)) { + return true; + + } + } + return false; } /** @@ -154,8 +172,8 @@ public boolean contains (E item){ * @return true if the list is empty, false otherwise */ @Override - public boolean isEmpty () { - return false; + public boolean isEmpty() { + return size == 0; } /** @@ -165,7 +183,7 @@ public boolean isEmpty () { */ @Override public int size () { - return 0; + return size; } /** From 76820716a3ce0bcac9ff26f6702902125c1b98c1 Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Tue, 13 Feb 2024 22:51:01 -0800 Subject: [PATCH 04/20] added linkedlist class --- src/LinkedList.java | 139 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 src/LinkedList.java diff --git a/src/LinkedList.java b/src/LinkedList.java new file mode 100644 index 0000000..55b4ae2 --- /dev/null +++ b/src/LinkedList.java @@ -0,0 +1,139 @@ +import java.util.Iterator; + +public class LinkedList implements List{ + /** + * Add item to the front. + * + * @param item the item to be added + */ + @Override + public void addFront(E item) { + + } + + /** + * Add item to the back. + * + * @param item the item to be added + */ + @Override + public void addBack(E item) { + + } + + /** + * Add an item at specified index (position). + * + * @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) { + + } + + /** + * Get the item at a specified index. + * + * @param i the index where the item should be retrieved + * @return the item located at that index + */ + @Override + public E get(int i) { + return null; + } + + /** + * Set (save) an item at a specified index. Previous + * item at that index is overwritten. + * + * @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) { + + } + + /** + * Remove item at the front of the list. + * + * @return the item that was removed + */ + @Override + public E removeFront() { + return null; + } + + /** + * Remove item at the back of the list + * + * @return the item that was removed + */ + @Override + public E removeBack() { + return null; + } + + /** + * Remove item from the list + * + * @param item the item to be removed + */ + @Override + public void remove(E item) { + + } + + /** + * Remove item at a specified index. + * + * @param i the index where the item should be removed + * @return the item that was removed + */ + @Override + public E remove(int i) { + return null; + } + + /** + * Checks if an item is in the list. + * + * @param item the item to search for + * @return true if the item is in the list, false otherwise + */ + @Override + public boolean contains(E item) { + return false; + } + + /** + * Checks if the list is empty. + * + * @return true if the list is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Provides a count of the number of items in the list. + * + * @return number of items in the list + */ + @Override + public int size() { + return 0; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } +} From 7ce421b126b7de28f3007a86c37ec10696dbe4f8 Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Tue, 13 Feb 2024 23:14:20 -0800 Subject: [PATCH 05/20] finished some methods within linkedlist, still need to do runtime analysis/finish methods.. --- src/ArrayList.java | 4 +- src/LinkedList.java | 89 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 82 insertions(+), 11 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index be0a4a9..46dce03 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -6,8 +6,8 @@ public class ArrayList implements List { private int size; private E[] buffer; - public ArrayList() { - buffer = (E[]) new Object[10]; + public ArrayList(int capacity) { + buffer = (E[]) new Object[capacity]; size = 0; } diff --git a/src/LinkedList.java b/src/LinkedList.java index 55b4ae2..4c122c8 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -1,6 +1,29 @@ import java.util.Iterator; +import java.util.NoSuchElementException; +/* +still need to do runtime analysis of each method!! +@ Ryder Dettloff +@ version 1.0 +@ 02-13-2024 + */ +public class LinkedList implements List { + private class Node { + E data; + Node nextNode; + } + + //set up the head + private Node head; + //set up the size field + private int size; + + //add constructor to initialize the data + public LinkedList() { + head = null; + size = 0; + + } -public class LinkedList implements List{ /** * Add item to the front. * @@ -8,7 +31,10 @@ public class LinkedList implements List{ */ @Override public void addFront(E item) { - + Node newNode = new Node(); + newNode.nextNode = head; + head = newNode; + size++; } /** @@ -35,14 +61,19 @@ public void add(int i, E item) { /** * Get the item at a specified index. * - * @param i the index where the item should be retrieved + * @param index the index where the item should be retrieved * @return the item located at that index */ @Override - public E get(int i) { - return null; + public E get(int index) { + Node current = head; + for (int i = 0; i < index; i++) { + current = current.nextNode; + } + return (E) current; } + /** * Set (save) an item at a specified index. Previous * item at that index is overwritten. @@ -52,7 +83,14 @@ public E get(int i) { */ @Override public void set(int i, E item) { - + if (i < 0 || i >= size) { + throw new IndexOutOfBoundsException("no such index exists!"); + } + Node current = head; + for (int index = 0; index < i; index++) { + current = current.nextNode; + } + current.data = item; } /** @@ -104,6 +142,16 @@ public E remove(int i) { */ @Override public boolean contains(E item) { + Node current = head; + if (isEmpty()) { + return false; + } + while (current != null) { + if (current.data.equals(item)) { + return true; + } + current = current.nextNode; + } return false; } @@ -114,7 +162,7 @@ public boolean contains(E item) { */ @Override public boolean isEmpty() { - return false; + return size == 0; } /** @@ -124,7 +172,7 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + return size; } /** @@ -134,6 +182,29 @@ public int size() { */ @Override public Iterator iterator() { - return null; + return new LinkedListIterator(); + } + + public class LinkedListIterator implements Iterator { + private Node current; + + private LinkedListIterator() { + current = head; + } +// returns ture if iteration has no more elements + @Override + public boolean hasNext() { + return current != null; + } +// returns next element in the iteration + @Override + public E next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + E data = current.data; + current = current.nextNode; + return data; + } } } From ce94d9a864760a98d586aa89ec5a1e287f219731 Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Tue, 13 Feb 2024 23:15:43 -0800 Subject: [PATCH 06/20] added javadoc --- src/ArrayList.java | 7 ++++++- src/LinkedList.java | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index 46dce03..522db26 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -1,7 +1,12 @@ import java.util.Arrays; import java.util.Iterator; import java.util.NoSuchElementException; - +/* +Need to finish methods and runtime analysis +@ Ryder Dettloff +@ version 1.0 +@ 02-13-2024 + */ public class ArrayList implements List { private int size; private E[] buffer; diff --git a/src/LinkedList.java b/src/LinkedList.java index 4c122c8..c03363a 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -1,5 +1,6 @@ import java.util.Iterator; import java.util.NoSuchElementException; + /* still need to do runtime analysis of each method!! @ Ryder Dettloff From cedd8260177c85af2542bd405d340b4800e4ae37 Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Fri, 16 Feb 2024 22:40:07 -0800 Subject: [PATCH 07/20] finished tests for ArrayList. passes all cases. working on linkedlist tests.. finished linkedlist methods --- SDEV333-Term-Project.iml | 53 ++++++++++++++++++ src/ArrayList.java | 64 +++++++++++++--------- src/ArrayListTest.java | 113 +++++++++++++++++++++++++++++++++++++++ src/LinkedList.java | 112 +++++++++++++++++++++++++++++--------- src/LinkedListTest.java | 74 +++++++++++++++++++++++++ 5 files changed, 366 insertions(+), 50 deletions(-) create mode 100644 src/ArrayListTest.java create mode 100644 src/LinkedListTest.java diff --git a/SDEV333-Term-Project.iml b/SDEV333-Term-Project.iml index c90834f..e32ac48 100644 --- a/SDEV333-Term-Project.iml +++ b/SDEV333-Term-Project.iml @@ -4,8 +4,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ArrayList.java b/src/ArrayList.java index 522db26..33ecad5 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -11,8 +11,8 @@ public class ArrayList implements List { private int size; private E[] buffer; - public ArrayList(int capacity) { - buffer = (E[]) new Object[capacity]; + public ArrayList() { + buffer = (E[]) new Object[10]; size = 0; } @@ -20,9 +20,14 @@ public ArrayList(int capacity) { * Add item to the front. * * @param item the item to be added + * Runtime Analysis: This method will run in O(n) worst case because it has to traverse through the whole list */ @Override public void addFront(E item) { + if(size == buffer.length) + { + increaseBuffer(size); + } for (int i = size - 1; i >= 0; i--) { // shift elements buffer[i + 1] = buffer[i]; } @@ -32,10 +37,11 @@ public void addFront(E item) { /** * Add item to the back. - * + * Worse case the buffer needs to beresized and will run in O(n) but for empty arrays it will log O(1) for best case * @param item the item to be added */ @Override + public void addBack(E item) { increaseBuffer(size + 1); buffer[size++] = item; @@ -43,20 +49,30 @@ public void addBack(E item) { /** * Add an item at specified index (position). - * + * Runtime Analysis O(n) has to shift all elements in the list * @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) { if (i < 0 || i > size) { - throw new IndexOutOfBoundsException(); + throw new IndexOutOfBoundsException("Index is out of bounds!"); + } + + if (size == buffer.length) { + increaseBuffer(size + 1); + } + for (int j = size; j > i; j--) { + buffer[j] = buffer[j - 1]; } + + buffer[i] = item; + size++; } /** * Get the item at a specified index. - * + * Runtime Analysis: runs in O(1) since it grabs it from the specific index * @param i the index where the item should be retrieved * @return the item located at that index */ @@ -71,7 +87,7 @@ public E get(int i) { /** * Set (save) an item at a specified index. Previous * item at that index is overwritten. - * + * Runtime Analysis: Also invloves indexing so O(1) * @param i the index where the item should be saved * @param item the item to be saved */ @@ -85,7 +101,7 @@ public void set(int i, E item) { /** * Remove item at the front of the list. - * + * Runtime Analysis: O(n) since we will have to shift the whole list left * @return the item that was removed */ @Override @@ -103,7 +119,7 @@ public E removeFront () { /** * Remove item at the back of the list - * + * O(1) since we are only updating the last element in the array (no shifting) * @return the item that was removed */ @Override @@ -119,7 +135,7 @@ public E removeBack () { /** * Remove item from the list - * + * Runtime Analysis: O(n) to traverse he whole list * @param item the item to be removed */ @Override @@ -135,7 +151,7 @@ public void remove(E item) { /** * Remove item at a specified index. - * + * Runtime Analysis: O(n) for worse case, if item is last in the array it can be O(1) * @param i the index where the item should be removed * @return the item that was removed */ @@ -146,14 +162,14 @@ public E remove(int i) { } E removedItem = buffer[i]; for (int j = 0; j < size-1; j++ ) { - buffer[j] = buffer[j + 1]; // to avoid memory leak + buffer[j] = buffer[j + 1]; } size--; return removedItem; } /** * Checks if an item is in the list. - * + * Runtime Analysis: O(n) worse case it needs to traverse the whole list to find item. * @param item the item to search for * @return true if the item is in the list, false otherwise */ @@ -173,7 +189,7 @@ public boolean contains(E item) { /** * Checks if the list is empty. - * + * O(1) only requires checking size variable * @return true if the list is empty, false otherwise */ @Override @@ -183,7 +199,7 @@ public boolean isEmpty() { /** * Provides a count of the number of items in the list. - * + * Runtime Analysis: O(1) only returns the size variable * @return number of items in the list */ @Override @@ -203,12 +219,10 @@ public Iterator iterator () { private class ArrayListIterator implements Iterator { private int currentIndex = 0; - @Override public boolean hasNext() { return currentIndex < size; } - @Override public E next() { if (!hasNext()) { throw new NoSuchElementException("no index exists!"); @@ -216,13 +230,13 @@ public E next() { return buffer[currentIndex++]; } } - private void increaseBuffer ( int increaseBuffer){ - if (increaseBuffer > buffer.length) { - int newBuffer = buffer.length * 2; - if (newBuffer < increaseBuffer) { - newBuffer = increaseBuffer; - } - buffer = Arrays.copyOf(buffer, newBuffer); + + /* increase buffer helper grabs the length of the buffer and doubles it + * it then copies the original buffer to the new one + * this runs in O(n) because it simply does a calculation then assigns the new buffer with all the old buffers data + */ + private void increaseBuffer(int increaseBuffer){ + int newBuffer = buffer.length * 2; + buffer = Arrays.copyOf(buffer, newBuffer); } } - } diff --git a/src/ArrayListTest.java b/src/ArrayListTest.java new file mode 100644 index 0000000..8d87326 --- /dev/null +++ b/src/ArrayListTest.java @@ -0,0 +1,113 @@ +import static org.junit.jupiter.api.Assertions.*; +/* + * Ryder Dettloff + * Tests for IntList Review; ArrayList methods + */ +class ArrayListTest { + ArrayList myArray = new ArrayList<>(); + + ArrayList myArrayFilled = listFiller(); + + /* + helper to fill array with numbers that increment by 3 + */ + public ArrayList listFiller() { + ArrayList myArray = new ArrayList<>(); + for (int i = 0; i <= 10 - 1; i++) { + myArray.addFront(i + 1); + } + return myArray; + } + + @org.junit.jupiter.api.Test + void testAddFront() { + assertTrue(myArray.isEmpty()); + myArray.addFront(9); + assertEquals(9,myArray.get(0)); + assertEquals(10, myArrayFilled.get(0)); + myArrayFilled.addFront(16); + assertEquals(16, myArrayFilled.get(0)); + } + + @org.junit.jupiter.api.Test + void addBack() { + assertTrue(myArray.isEmpty()); + myArray.addBack(9); + assertEquals(9,myArray.get(myArray.size()-1)); + assertEquals(10, myArrayFilled.get(0)); + myArrayFilled.addBack(16); + assertEquals(16, myArrayFilled.get(myArrayFilled.size()-1)); + } + + @org.junit.jupiter.api.Test + void testAdd() { + assertTrue(myArray.isEmpty()); + myArray.add(0,22); + assertEquals(22,myArray.get(0)); + assertEquals(10, myArrayFilled.get(0)); + myArrayFilled.add(2,25); + assertEquals(25, myArrayFilled.get(2)); + } + + @org.junit.jupiter.api.Test + void removeFront() { + assertFalse(myArrayFilled.isEmpty()); + assertEquals(10,myArrayFilled.get(0)); + myArrayFilled.removeFront(); + } + + @org.junit.jupiter.api.Test + void removeBack() { + assertFalse(myArrayFilled.isEmpty()); + assertEquals(1,myArrayFilled.get(myArrayFilled.size()-1)); + myArrayFilled.removeBack(); + } + + @org.junit.jupiter.api.Test + void remove() { + assertFalse(myArrayFilled.isEmpty()); + assertEquals(10,myArrayFilled.get(0)); + myArrayFilled.remove(2); + assertEquals(7,myArrayFilled.get(2)); + } + + @org.junit.jupiter.api.Test + void get() { + ArrayList myArray = new ArrayList<>(); + myArray.addFront(1); + myArray.addFront(2); + myArray.addFront(3); + assertEquals(3, myArray.get(0)); + assertEquals(2, myArray.get(1)); + assertEquals(1, myArray.get(2)); + } + + @org.junit.jupiter.api.Test + void contains() { + ArrayList myArray = new ArrayList<>(); + myArray.addFront(1); + myArray.addFront(2); + myArray.addFront(3); + assertFalse(myArray.contains(5)); + assertTrue(myArray.contains(1)); + } + + + @org.junit.jupiter.api.Test + void isEmpty() { + ArrayList myArray = new ArrayList<>(); + assertTrue(myArray.isEmpty()); + myArray.addFront(1); + assertFalse(myArray.isEmpty()); + } + + @org.junit.jupiter.api.Test + void size() { + ArrayList myArray = new ArrayList<>(); + assertEquals(0, myArray.size()); + myArray.addFront(1); + myArray.addFront(2); + myArray.addFront(3); + assertEquals(3, myArray.size()); + } +} \ No newline at end of file diff --git a/src/LinkedList.java b/src/LinkedList.java index c03363a..932f243 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -2,7 +2,6 @@ import java.util.NoSuchElementException; /* -still need to do runtime analysis of each method!! @ Ryder Dettloff @ version 1.0 @ 02-13-2024 @@ -27,7 +26,7 @@ public LinkedList() { /** * Add item to the front. - * + * Runtime Analysis: O(1) for linkedlist, it creates a new head node which is the same regardless of size * @param item the item to be added */ @Override @@ -40,28 +39,50 @@ public void addFront(E item) { /** * Add item to the back. - * + * Runtime Analysis: O(n) required traversing through the whole list to get to the last node * @param item the item to be added */ @Override public void addBack(E item) { + Node newNode = new Node(); + Node current = head; + if (isEmpty()) { + head = newNode; + } + while (current.nextNode != null) { + current = current.nextNode; + } + current.nextNode = newNode; + size++; + } - } /** * Add an item at specified index (position). - * + * Runtime Analysis: O(n) may require traversing through the whole list * @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) { + Node newNode = new Node(); + Node previousNode = (Node) get(i - 1); + if (i < 0 || i > size) { + throw new IndexOutOfBoundsException(); + } + if (i == 0) { + addFront(item); + } else { + newNode.nextNode = previousNode.nextNode; + previousNode.nextNode = newNode; + size++; + } } /** * Get the item at a specified index. - * + * Runtime Analysis: (may require traversing the whole list to find the given index O(n)) * @param index the index where the item should be retrieved * @return the item located at that index */ @@ -78,7 +99,7 @@ public E get(int index) { /** * Set (save) an item at a specified index. Previous * item at that index is overwritten. - * + * Runtime Analysis: (may require traversing the whole list to find the fiven index O(n)) * @param i the index where the item should be saved * @param item the item to be saved */ @@ -96,48 +117,89 @@ public void set(int i, E item) { /** * Remove item at the front of the list. - * + * Runtime Analysis: O(1) only need to change the head reference * @return the item that was removed */ @Override public E removeFront() { - return null; + if (isEmpty()) { + throw new NoSuchElementException("the list is empty!"); + } + E removedItem = head.data; + head = head.nextNode; + size--; + return removedItem; } /** * Remove item at the back of the list - * + * Runtime Analysis: (May require traversing the whole list to get to the last node O(n)) * @return the item that was removed */ @Override public E removeBack() { - return null; + Node previousNode = (Node) get(size - 2); + if (isEmpty()) { + throw new NoSuchElementException("No item exists! The list is empty!"); + } + if (size == 1) { + return removeFront(); + } + E removedItem = previousNode.nextNode.data; + previousNode.nextNode = null; + size--; + return removedItem; } /** * Remove item from the list - * + * Runtime Analysis (may require traversing the whole list to find the given item O(n)) * @param item the item to be removed */ @Override public void remove(E item) { - + Node current = new Node(); + if (isEmpty()) { + throw new NoSuchElementException("No item exists! The list is empty!"); + } + if (head.data.equals(item)) { + removeFront(); + size--; + } + while(current.nextNode != null && !current.nextNode.data.equals(item)) { + current = current.nextNode; + } + if (current.nextNode != null) { + current.nextNode = current.nextNode.nextNode; + size--; + } } /** * Remove item at a specified index. - * + * Runtime Analysis (may require traversing the whole list to find the given index O(n)) * @param i the index where the item should be removed * @return the item that was removed */ @Override - public E remove(int i) { - return null; - } + public E remove(int i) { + Node previousNode = (Node) get(i - 1); + if (i < 0 || i >= size) { + throw new IndexOutOfBoundsException("no index exists!"); + } + if (i == 0) { + return removeFront(); + } + + E removedItem = previousNode.nextNode.data; + previousNode.nextNode = previousNode.nextNode.nextNode; + size--; + return removedItem; + } /** * Checks if an item is in the list. - * + * Runtime Analysis (may require traversing the whole list to find the given item O(n)) * @param item the item to search for * @return true if the item is in the list, false otherwise */ @@ -158,7 +220,7 @@ public boolean contains(E item) { /** * Checks if the list is empty. - * + * O(1) only requires checking the variable * @return true if the list is empty, false otherwise */ @Override @@ -168,7 +230,7 @@ public boolean isEmpty() { /** * Provides a count of the number of items in the list. - * + * O(1) only requires returning the size variables data * @return number of items in the list */ @Override @@ -178,7 +240,7 @@ public int size() { /** * Returns an iterator over elements of type {@code T}. - * + * O(1) because the methods within simply assign variables new data or check * @return an Iterator. */ @Override @@ -192,13 +254,13 @@ public class LinkedListIterator implements Iterator { private LinkedListIterator() { current = head; } -// returns ture if iteration has no more elements - @Override + // returns ture if iteration has no more elements + public boolean hasNext() { return current != null; } -// returns next element in the iteration - @Override + // returns next element in the iteration + public E next() { if (!hasNext()) { throw new NoSuchElementException(); diff --git a/src/LinkedListTest.java b/src/LinkedListTest.java new file mode 100644 index 0000000..464afc9 --- /dev/null +++ b/src/LinkedListTest.java @@ -0,0 +1,74 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class LinkedListTest { + LinkedList myLinkedList = new LinkedList<>(); + + public LinkedList myFilledLinkedList = fillListWithNumbers(); + +/* +^^^^^ +helper method to create a full linkedlist of 10 nodes + */ + private LinkedList fillListWithNumbers() { + LinkedList list = new LinkedList<>(); + for (int i = 1; i <= 10; i++) { + list.addFront(i); + } + return list; + } + @Test + void addFront() { + assertTrue(myLinkedList.isEmpty()); + myLinkedList.addFront(9); + assertEquals(9, myLinkedList.get(0)); + assertEquals(10, myFilledLinkedList.get(0)); + myFilledLinkedList.addFront(16); + assertEquals(16, myFilledLinkedList.get(0)); + } + + @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 a359f238c20f491a9fc5a819b4a8cd07994ef217 Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Fri, 16 Feb 2024 22:54:33 -0800 Subject: [PATCH 08/20] I think something is wrong with my getter. I can't write my tests for the linkedlist until I figure it out. --- src/LinkedList.java | 2 +- src/LinkedListTest.java | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/LinkedList.java b/src/LinkedList.java index 932f243..b9b11f0 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -92,7 +92,7 @@ public E get(int index) { for (int i = 0; i < index; i++) { current = current.nextNode; } - return (E) current; + return current.data; } diff --git a/src/LinkedListTest.java b/src/LinkedListTest.java index 464afc9..c2ea9b6 100644 --- a/src/LinkedListTest.java +++ b/src/LinkedListTest.java @@ -2,6 +2,10 @@ import static org.junit.jupiter.api.Assertions.*; +/* +I need to get debug, I think something is wrong with my getter.. I will review and see if I +can get it fixed asap. + */ class LinkedListTest { LinkedList myLinkedList = new LinkedList<>(); @@ -20,12 +24,9 @@ private LinkedList fillListWithNumbers() { } @Test void addFront() { - assertTrue(myLinkedList.isEmpty()); + assertEquals(0,myLinkedList.size()); myLinkedList.addFront(9); - assertEquals(9, myLinkedList.get(0)); assertEquals(10, myFilledLinkedList.get(0)); - myFilledLinkedList.addFront(16); - assertEquals(16, myFilledLinkedList.get(0)); } @Test From 73aa399cdb8e22caacb16e3d9a2b579bd96b072b Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Sun, 18 Feb 2024 12:16:27 -0800 Subject: [PATCH 09/20] started project 2, created required files and added code to stacktestclient... --- src/LinkedStack.java | 2 ++ src/Main.java | 1 + src/ResizingArrayStack.java | 2 ++ src/StackTestClient.java | 22 ++++++++++++++++++++++ src/bag.java | 10 ++++++++++ 5 files changed, 37 insertions(+) create mode 100644 src/LinkedStack.java create mode 100644 src/ResizingArrayStack.java create mode 100644 src/StackTestClient.java create mode 100644 src/bag.java diff --git a/src/LinkedStack.java b/src/LinkedStack.java new file mode 100644 index 0000000..901e285 --- /dev/null +++ b/src/LinkedStack.java @@ -0,0 +1,2 @@ +public class LinkedStack { +} diff --git a/src/Main.java b/src/Main.java index 8f8f984..252d78a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -6,5 +6,6 @@ public static void main(String[] args) { // to see how IntelliJ IDEA suggests fixing it. System.out.println("Hello and welcome!"); + } } \ No newline at end of file diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java new file mode 100644 index 0000000..641e661 --- /dev/null +++ b/src/ResizingArrayStack.java @@ -0,0 +1,2 @@ +public class ResizingArrayStack { +} diff --git a/src/StackTestClient.java b/src/StackTestClient.java new file mode 100644 index 0000000..80de63a --- /dev/null +++ b/src/StackTestClient.java @@ -0,0 +1,22 @@ +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"); + } +} +} diff --git a/src/bag.java b/src/bag.java new file mode 100644 index 0000000..e1cc747 --- /dev/null +++ b/src/bag.java @@ -0,0 +1,10 @@ +public interface bag extends Iterable { + + void add(E item); + + boolean isEmpty(); + + + int size(); + +} From 3ce236fb9159875fa76a4f7761cbeb0dbe1c3c16 Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Sun, 18 Feb 2024 12:24:03 -0800 Subject: [PATCH 10/20] Added other required files.. --- src/LinkedBag.java | 2 ++ src/LinkedQueue.java | 54 ++++++++++++++++++++++++++++++ src/LinkedStack.java | 65 ++++++++++++++++++++++++++++++++++++- src/ResizingArrayStack.java | 65 ++++++++++++++++++++++++++++++++++++- 4 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 src/LinkedBag.java create mode 100644 src/LinkedQueue.java diff --git a/src/LinkedBag.java b/src/LinkedBag.java new file mode 100644 index 0000000..6f47748 --- /dev/null +++ b/src/LinkedBag.java @@ -0,0 +1,2 @@ +public class LinkedBag { +} diff --git a/src/LinkedQueue.java b/src/LinkedQueue.java new file mode 100644 index 0000000..5218c07 --- /dev/null +++ b/src/LinkedQueue.java @@ -0,0 +1,54 @@ +import java.util.Iterator; + +public class LinkedQueue implements Queue{ + + /** + * Add an item to the queue. + * + * @param item the item to be added + */ + @Override + public void enqueue(E item) { + + } + + /** + * Remove an item from the queue. + * + * @return the item that was removed + */ + @Override + public E dequeue() { + return null; + } + + /** + * Checks to see if the queue is empty. + * + * @return true if the queue is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Returns a count of the number of items in the queue. + * + * @return the number of items in the queue + */ + @Override + public int size() { + return 0; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } +} diff --git a/src/LinkedStack.java b/src/LinkedStack.java index 901e285..f8a09f7 100644 --- a/src/LinkedStack.java +++ b/src/LinkedStack.java @@ -1,2 +1,65 @@ -public class LinkedStack { +import java.util.Iterator; + +public class LinkedStack implements Stack{ + + /** + * Add an item to the stack. + * + * @param item the item to be added + */ + @Override + public void push(E item) { + + } + + /** + * Removes the most recently added item from the stack. + * + * @return the item that was removed + */ + @Override + public E pop() { + return null; + } + + /** + * Returns the item at the top of the stack. + * Does not modify the stack or the item at the top. + * + * @return item at the top of the stack. + */ + @Override + public E peek() { + return null; + } + + /** + * Checks to see if the stack is empty. + * + * @return true if the stack is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Returns a count of the number of items in the stack. + * + * @return the number of items in the stack + */ + @Override + public int size() { + return 0; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } } diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java index 641e661..960c0e8 100644 --- a/src/ResizingArrayStack.java +++ b/src/ResizingArrayStack.java @@ -1,2 +1,65 @@ -public class ResizingArrayStack { +import java.util.Iterator; + +public class ResizingArrayStack implements Stack{ + + /** + * Add an item to the stack. + * + * @param item the item to be added + */ + @Override + public void push(E item) { + + } + + /** + * Removes the most recently added item from the stack. + * + * @return the item that was removed + */ + @Override + public E pop() { + return null; + } + + /** + * Returns the item at the top of the stack. + * Does not modify the stack or the item at the top. + * + * @return item at the top of the stack. + */ + @Override + public E peek() { + return null; + } + + /** + * Checks to see if the stack is empty. + * + * @return true if the stack is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Returns a count of the number of items in the stack. + * + * @return the number of items in the stack + */ + @Override + public int size() { + return 0; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } } From 46e5ea0e2bcb27a76c3c28f72c76c8460d15145f Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Thu, 22 Feb 2024 12:15:38 -0800 Subject: [PATCH 11/20] finished ResizingArrayStack Class and it's meethods. fixed stack interface API from not loading. All methods run with no error. --- .idea/uiDesigner.xml | 124 ++++++++++++++++++++++++++++++++ .idea/vcs.xml | 6 ++ src/LinkedBag.java | 94 +++++++++++++++++++++++- src/LinkedStack.java | 3 +- src/ResizingArrayStack.java | 88 ++++++++++++++++++++--- src/StackTestClient.java | 9 ++- src/bag.java | 10 --- src/interfaces/Bag.java | 30 ++++++++ src/{ => interfaces}/Stack.java | 6 +- 9 files changed, 346 insertions(+), 24 deletions(-) create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml delete mode 100644 src/bag.java create mode 100644 src/interfaces/Bag.java rename src/{ => interfaces}/Stack.java (91%) diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/LinkedBag.java b/src/LinkedBag.java index 6f47748..a5c58f9 100644 --- a/src/LinkedBag.java +++ b/src/LinkedBag.java @@ -1,2 +1,94 @@ -public class LinkedBag { +import interfaces.Bag; + +import java.util.Iterator; +import java.util.Spliterator; +import java.util.function.Consumer; + +public class LinkedBag implements Bag { + /** + * Add an item to the bag. + * + * @param item the item to be added + */ + @Override + public void add(E item) { + + } + + /** + * Checks to see if the bag is empty. + * + * @return true if the bag is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Returns a count of the number of items in the bag. + * + * @return the number of items in the bag + */ + @Override + public int size() { + return 0; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } + + /** + * Performs the given action for each element of the {@code Iterable} + * until all elements have been processed or the action throws an + * exception. Actions are performed in the order of iteration, if that + * order is specified. Exceptions thrown by the action are relayed to the + * caller. + *

+ * The behavior of this method is unspecified if the action performs + * side-effects that modify the underlying source of elements, unless an + * overriding class has specified a concurrent modification policy. + * + * @param action The action to be performed for each element + * @throws NullPointerException if the specified action is null + * @implSpec

The default implementation behaves as if: + *

{@code
+     *     for (T t : this)
+     *         action.accept(t);
+     * }
+ * @since 1.8 + */ + @Override + public void forEach(Consumer action) { + Bag.super.forEach(action); + } + + /** + * Creates a {@link Spliterator} over the elements described by this + * {@code Iterable}. + * + * @return a {@code Spliterator} over the elements described by this + * {@code Iterable}. + * @implSpec The default implementation creates an + * early-binding + * spliterator from the iterable's {@code Iterator}. The spliterator + * inherits the fail-fast properties of the iterable's iterator. + * @implNote The default implementation should usually be overridden. The + * spliterator returned by the default implementation has poor splitting + * capabilities, is unsized, and does not report any spliterator + * characteristics. Implementing classes can nearly always provide a + * better implementation. + * @since 1.8 + */ + @Override + public Spliterator spliterator() { + return Bag.super.spliterator(); + } } diff --git a/src/LinkedStack.java b/src/LinkedStack.java index f8a09f7..d77040a 100644 --- a/src/LinkedStack.java +++ b/src/LinkedStack.java @@ -1,6 +1,7 @@ import java.util.Iterator; +import interfaces.Stack; -public class LinkedStack implements Stack{ +public class LinkedStack implements Stack { /** * Add an item to the stack. diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java index 960c0e8..d7aed7e 100644 --- a/src/ResizingArrayStack.java +++ b/src/ResizingArrayStack.java @@ -1,15 +1,50 @@ +import java.util.Arrays; +import interfaces.Stack; import java.util.Iterator; +import java.util.NoSuchElementException; -public class ResizingArrayStack implements Stack{ +/** + * Ryder Dettloff + * Resizing Arraystack implements adding and removing from the top of a stack. + * @param + */ +public class ResizingArrayStack implements Stack { + //fields + private E[] buffer; + private int size; + private static final int INITIAL_SIZE = 10; + + public ResizingArrayStack() { + this.buffer = (E[]) new Object[INITIAL_SIZE]; + this.size = 0; + + } + + /** + * doubles the size of the buffer to be used in the push method + * @param increaseBuffer + */ + private void increaseBuffer(int increaseBuffer) { + //helper for push/pop doubles size of array and copies it over to the new one + int newBuffer = buffer.length * 2; + buffer = Arrays.copyOf(buffer, newBuffer); + } /** * Add an item to the stack. - * + * checks to see if buffer is of size then pushes the item to the stack * @param item the item to be added */ @Override - public void push(E item) { + public void push(E item) { + //checks if buffer needs an increase in size + if (size == buffer.length) { + increaseBuffer(2 * buffer.length); + } + //assigns item to the top of stack and increments it + buffer[size] = item; + size++; } /** @@ -19,7 +54,19 @@ public void push(E item) { */ @Override public E pop() { - return null; + if (isEmpty()) { + throw new IllegalStateException("stack is empty"); + } + // access item at top of stack and sets it to null for garbage collector + E item = buffer[size - 1]; + size--; + buffer[size] = null; + //decreases the size of the buffer + if (size > 0 && size == buffer.length / 2) { + increaseBuffer(buffer.length % 2); + } + + return item; } /** @@ -30,7 +77,13 @@ public E pop() { */ @Override public E peek() { - return null; + //exception if the stack is empty(no peek) + if (isEmpty()) { + throw new IllegalStateException("Stack is empty, cannot peek()"); + } + + //finds and returns the top of stack with the index + return buffer[size-1]; } /** @@ -40,7 +93,7 @@ public E peek() { */ @Override public boolean isEmpty() { - return false; + return size == 0; } /** @@ -50,7 +103,7 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + return size; } /** @@ -59,7 +112,24 @@ public int size() { * @return an Iterator. */ @Override - public Iterator iterator() { + public Iterator iterator() { return null; } -} + + + //copied from my ArrayList class , need to update + private class ArrayListIterator implements Iterator { + private int currentIndex = 0; + + public boolean hasNext() { + return currentIndex < size; + } + + public E next() { + if (!hasNext()) { + throw new NoSuchElementException("no index exists!"); + } + return buffer[currentIndex++]; + } + } +} \ No newline at end of file diff --git a/src/StackTestClient.java b/src/StackTestClient.java index 80de63a..55450cc 100644 --- a/src/StackTestClient.java +++ b/src/StackTestClient.java @@ -1,3 +1,11 @@ +import interfaces.Stack; + +import java.util.Scanner; + +/** + * @Ryder Dettloff + * tests my methods for ResizingArrayStack class. + */ public class StackTestClient { public static void main(String[] args) { @@ -19,4 +27,3 @@ public static void main(String[] args) { System.out.println("(" + s.size() + ") left on the stack"); } } -} diff --git a/src/bag.java b/src/bag.java deleted file mode 100644 index e1cc747..0000000 --- a/src/bag.java +++ /dev/null @@ -1,10 +0,0 @@ -public interface bag extends Iterable { - - void add(E item); - - boolean isEmpty(); - - - int size(); - -} diff --git a/src/interfaces/Bag.java b/src/interfaces/Bag.java new file mode 100644 index 0000000..a833361 --- /dev/null +++ b/src/interfaces/Bag.java @@ -0,0 +1,30 @@ +package interfaces; + +/** + * interfaces.Bag interface (random order, no deletion) + * + * @param class / data type of the items in the queue + */ +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 empty. + * + * @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(); +} diff --git a/src/Stack.java b/src/interfaces/Stack.java similarity index 91% rename from src/Stack.java rename to src/interfaces/Stack.java index bb1000d..865ad33 100644 --- a/src/Stack.java +++ b/src/interfaces/Stack.java @@ -1,5 +1,7 @@ +package interfaces; + /** - * Stack (LIFO: last-in, first-out) API + * interfaces.Stack (LIFO: last-in, first-out) API * @param class / data type of the items in the stack */ public interface Stack extends Iterable { @@ -33,4 +35,4 @@ public interface Stack extends Iterable { * @return the number of items in the stack */ int size(); -} +} \ No newline at end of file From c223719be47ddea1db776abe4734c0d50bb5ba59 Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Thu, 22 Feb 2024 12:28:20 -0800 Subject: [PATCH 12/20] updated linkedStack to reflect interface usage. finished iterator method and peek method --- src/LinkedStack.java | 54 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/src/LinkedStack.java b/src/LinkedStack.java index d77040a..5de6fc7 100644 --- a/src/LinkedStack.java +++ b/src/LinkedStack.java @@ -1,8 +1,22 @@ import java.util.Iterator; +import java.util.ListIterator; +import java.util.NoSuchElementException; import interfaces.Stack; -public class LinkedStack implements Stack { +import interfaces.Stack; + +public class LinkedStack implements Stack +{ + //fields + private Node head; + private int size; + //Node constructor + private class Node + { + E item; + Node nextNode; + } /** * Add an item to the stack. * @@ -30,10 +44,16 @@ public E pop() { * @return item at the top of the stack. */ @Override - public E peek() { - return null; + public E peek() + { + //throw exception if stack is empty + if(isEmpty()) + { + throw new IllegalStateException("Stack is empty, cannot peek()"); + } + //finds and returns the top of the stack + return head.item; } - /** * Checks to see if the stack is empty. * @@ -41,7 +61,7 @@ public E peek() { */ @Override public boolean isEmpty() { - return false; + return size == 0; } /** @@ -51,7 +71,7 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + return size; } /** @@ -59,8 +79,26 @@ public int size() { * * @return an Iterator. */ - @Override public Iterator iterator() { - return null; + return new ListIterator(); + } + + public class ListIterator implements Iterator{ + // returns ture if iteration has no more elements + private Node current = head; + public boolean hasNext() { + return current != null; } + // returns next element in the iteration + + public E next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + //goes to the next node in the stack and returns it + E item = current.item; + current = current.nextNode; + return item; + } +} } From 283851d6093565cdb8faa5f0afcdbce82788a869 Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Thu, 22 Feb 2024 12:42:55 -0800 Subject: [PATCH 13/20] created queue test client and stats class --- src/QueueTestClient.java | 28 ++++++++++++++++++++++++++++ src/Stats.java | 2 ++ 2 files changed, 30 insertions(+) create mode 100644 src/QueueTestClient.java create mode 100644 src/Stats.java diff --git a/src/QueueTestClient.java b/src/QueueTestClient.java new file mode 100644 index 0000000..0a18222 --- /dev/null +++ b/src/QueueTestClient.java @@ -0,0 +1,28 @@ +import java.util.Scanner; + +/** + * + * @Ryder Dettloff + * Test for the LinkedQueue Class + * + */ +public class QueueTestClient +{ + public static void main(String[] args) + { + Queue s = new LinkedQueue<>(); + Scanner in = new Scanner("to be or not to - be - - that - - - is"); +// checks next string for "-" if it doesn't contain it places it at the end of the queue + while(in.hasNext()){ + String item = in.next(); + if(!item.equals("-")) { + s.enqueue(item); + //if it contains "-" and is not empty it removes it from the head and replace it with a space + } else if(!s.isEmpty()) { + System.out.println(s.dequeue() + " "); + } + } + //after all items are processed it returns the size left in the stack + 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..aeb1479 --- /dev/null +++ b/src/Stats.java @@ -0,0 +1,2 @@ +public class Stats { +} From 15d2062b282f4eca6a567f9a58f2dc30a232c087 Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Sun, 25 Feb 2024 12:54:38 -0800 Subject: [PATCH 14/20] finished methods for linkedstack. need to add some comments to walk through code --- src/LinkedStack.java | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/LinkedStack.java b/src/LinkedStack.java index 5de6fc7..dbe28e8 100644 --- a/src/LinkedStack.java +++ b/src/LinkedStack.java @@ -14,7 +14,7 @@ public class LinkedStack implements Stack //Node constructor private class Node { - E item; + E data; Node nextNode; } /** @@ -24,7 +24,11 @@ private class Node */ @Override public void push(E item) { - + head = new Node(); + Node prev = head; + head.data = item; + head.nextNode = prev; + size++; } /** @@ -33,8 +37,16 @@ public void push(E item) { * @return the item that was removed */ @Override - public E pop() { - return null; + public E pop() { + if(isEmpty()) + { + throw new IllegalStateException("The stack is empty"); + } + + E item = head.data; + head = head.nextNode; + size--; + return item; } /** @@ -52,7 +64,7 @@ public E peek() throw new IllegalStateException("Stack is empty, cannot peek()"); } //finds and returns the top of the stack - return head.item; + return head.data; } /** * Checks to see if the stack is empty. @@ -96,7 +108,7 @@ public E next() { throw new NoSuchElementException(); } //goes to the next node in the stack and returns it - E item = current.item; + E item = current.data; current = current.nextNode; return item; } From 3749f432271101c209979fa34d207a007b8b31fc Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Sun, 25 Feb 2024 13:12:11 -0800 Subject: [PATCH 15/20] addded methods for linkedbag, still need to walk through my logic. --- src/LinkedBag.java | 50 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/src/LinkedBag.java b/src/LinkedBag.java index a5c58f9..4041dcc 100644 --- a/src/LinkedBag.java +++ b/src/LinkedBag.java @@ -1,10 +1,27 @@ import interfaces.Bag; import java.util.Iterator; +import java.util.NoSuchElementException; import java.util.Spliterator; import java.util.function.Consumer; -public class LinkedBag implements Bag { +public class LinkedBag implements Bag + { + //fields + private Node head; + private int size; + + //Node constructor + private class Node + { + E data; + Node nextNode; + } + + public LinkedBag(){ + this.head = null; + this.size = 0; + } /** * Add an item to the bag. * @@ -12,7 +29,11 @@ public class LinkedBag implements Bag { */ @Override public void add(E item) { - + head = new Node(); + Node prev = head; + head.data = item; + head.nextNode = prev; + size++; } /** @@ -22,7 +43,7 @@ public void add(E item) { */ @Override public boolean isEmpty() { - return false; + return size == 0; } /** @@ -32,7 +53,7 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + return size; } /** @@ -42,9 +63,28 @@ public int size() { */ @Override public Iterator iterator() { - return null; + return new ListIterator(); } + public class ListIterator implements Iterator{ + // returns ture if iteration has no more elements + private Node current = head; + public boolean hasNext() { + return current != null; + } + // returns next element in the iteration + + public E next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + //goes to the next node in the stack and returns it + E item = current.data; + current = current.nextNode; + return item; + } + } + /** * Performs the given action for each element of the {@code Iterable} * until all elements have been processed or the action throws an From 18eadc91dc5aed8bdfe3f0290f8f4b842039094b Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Sun, 25 Feb 2024 13:19:48 -0800 Subject: [PATCH 16/20] added queue methods and logic. --- src/LinkedQueue.java | 80 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/src/LinkedQueue.java b/src/LinkedQueue.java index 5218c07..1c624b8 100644 --- a/src/LinkedQueue.java +++ b/src/LinkedQueue.java @@ -1,6 +1,24 @@ import java.util.Iterator; +import java.util.NoSuchElementException; public class LinkedQueue implements Queue{ + private Node head; + private Node tail; + private int size; + + + private class Node + { + E data; + Node nextNode; + } + + public LinkedQueue() + { + this.head = null; + this.tail = null; + this.size = 0; + } /** * Add an item to the queue. @@ -8,8 +26,24 @@ public class LinkedQueue implements Queue{ * @param item the item to be added */ @Override - public void enqueue(E item) { + public void enqueue(E item) { + //create a new head node that will be the top of the queue + head = new Node(); + Node prev = head; + head.data = item; + //check if queue is empty, if it is set the tail to head(new data) + if(isEmpty()) + { + tail = head; + } + else + { + //if it isn't empty set the prev node to the head and add item to end of queue + prev.nextNode = head; + } + + size++; } /** @@ -18,8 +52,22 @@ public void enqueue(E item) { * @return the item that was removed */ @Override - public E dequeue() { - return null; + public E dequeue() { + if(isEmpty()) + { + //check if stack is empty + throw new IllegalStateException("Stack is empty, cannot pop()"); + } + //get tail(back) of queue. if tails next node is empty, set head to null. + E item = tail.data; + tail = tail.nextNode; + if(isEmpty()) + { + head = null; + } + //decrement size to account for dequeue + size--; + return item; } /** @@ -29,7 +77,7 @@ public E dequeue() { */ @Override public boolean isEmpty() { - return false; + return size == 0; } /** @@ -39,7 +87,7 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + return size; } /** @@ -47,8 +95,26 @@ public int size() { * * @return an Iterator. */ - @Override public Iterator iterator() { - return null; + return new ListIterator(); + } + + public class ListIterator implements Iterator{ + // returns ture if iteration has no more elements + private Node current = head; + public boolean hasNext() { + return current != null; + } + // returns next element in the iteration + + public E next() { + if (!hasNext()) { + throw new NoSuchElementException(); + } + //goes to the next node in the stack and returns it + E item = current.data; + current = current.nextNode; + return item; + } } } From 0601651b0784c6608a57b258292a2e0fd1c30fb5 Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Sun, 25 Feb 2024 13:31:31 -0800 Subject: [PATCH 17/20] moved all data structure elements to interfaces folder. fixed logic for linkedqueue. --- src/ArrayList.java | 6 ++++-- src/LinkedList.java | 2 ++ src/LinkedQueue.java | 24 +++++++++++++----------- src/QueueTestClient.java | 2 ++ src/StackTestClient.java | 2 +- src/{ => interfaces}/Deque.java | 4 +++- src/{ => interfaces}/List.java | 4 +++- src/{ => interfaces}/Queue.java | 4 +++- 8 files changed, 31 insertions(+), 17 deletions(-) rename src/{ => interfaces}/Deque.java (92%) rename src/{ => interfaces}/List.java (96%) rename src/{ => interfaces}/Queue.java (89%) diff --git a/src/ArrayList.java b/src/ArrayList.java index 33ecad5..f273ec0 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -1,3 +1,5 @@ +import interfaces.List; + import java.util.Arrays; import java.util.Iterator; import java.util.NoSuchElementException; @@ -107,7 +109,7 @@ public void set(int i, E item) { @Override public E removeFront () { if (isEmpty()) { - throw new NoSuchElementException("List is Empty!"); + throw new NoSuchElementException("interfaces.List is Empty!"); } E removedItem = buffer[0]; for (int i = 0; i < size - 1 ; size++) { @@ -125,7 +127,7 @@ public E removeFront () { @Override public E removeBack () { if (isEmpty()) { - throw new NoSuchElementException("List is Empty!"); + throw new NoSuchElementException("interfaces.List is Empty!"); } E removedItem = buffer[size - 1]; buffer[size - 1] = null; diff --git a/src/LinkedList.java b/src/LinkedList.java index b9b11f0..da43539 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -1,3 +1,5 @@ +import interfaces.List; + import java.util.Iterator; import java.util.NoSuchElementException; diff --git a/src/LinkedQueue.java b/src/LinkedQueue.java index 1c624b8..583de84 100644 --- a/src/LinkedQueue.java +++ b/src/LinkedQueue.java @@ -1,7 +1,14 @@ +import interfaces.Queue; import java.util.Iterator; import java.util.NoSuchElementException; -public class LinkedQueue implements Queue{ +/** + * Ryder Dettloff + * FIFO Queue Data Structure + * @param + */ + +public class LinkedQueue implements Queue { private Node head; private Node tail; private int size; @@ -29,16 +36,13 @@ public LinkedQueue() public void enqueue(E item) { //create a new head node that will be the top of the queue - head = new Node(); Node prev = head; + head = new Node(); head.data = item; //check if queue is empty, if it is set the tail to head(new data) - if(isEmpty()) - { + if(isEmpty()) { tail = head; - } - else - { + } else { //if it isn't empty set the prev node to the head and add item to end of queue prev.nextNode = head; } @@ -53,16 +57,14 @@ public void enqueue(E item) { */ @Override public E dequeue() { - if(isEmpty()) - { + if(isEmpty()) { //check if stack is empty throw new IllegalStateException("Stack is empty, cannot pop()"); } //get tail(back) of queue. if tails next node is empty, set head to null. E item = tail.data; tail = tail.nextNode; - if(isEmpty()) - { + if(isEmpty()) { head = null; } //decrement size to account for dequeue diff --git a/src/QueueTestClient.java b/src/QueueTestClient.java index 0a18222..77aed75 100644 --- a/src/QueueTestClient.java +++ b/src/QueueTestClient.java @@ -1,3 +1,5 @@ +import interfaces.Queue; + import java.util.Scanner; /** diff --git a/src/StackTestClient.java b/src/StackTestClient.java index 55450cc..6bb4245 100644 --- a/src/StackTestClient.java +++ b/src/StackTestClient.java @@ -10,7 +10,7 @@ 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"); diff --git a/src/Deque.java b/src/interfaces/Deque.java similarity index 92% rename from src/Deque.java rename to src/interfaces/Deque.java index 0107f47..ccc0916 100644 --- a/src/Deque.java +++ b/src/interfaces/Deque.java @@ -1,5 +1,7 @@ +package interfaces; + /** - * Deque: double-ended queue API + * interfaces.Deque: double-ended queue API * Supports adding and removing items at both ends. * * @param diff --git a/src/List.java b/src/interfaces/List.java similarity index 96% rename from src/List.java rename to src/interfaces/List.java index 2bf4aef..384b294 100644 --- a/src/List.java +++ b/src/interfaces/List.java @@ -1,5 +1,7 @@ +package interfaces; + /*** - * List interface (API / abstract data type) + * interfaces.List interface (API / abstract data type) * @param Class or data type of the items in the list. */ public interface List extends Iterable { diff --git a/src/Queue.java b/src/interfaces/Queue.java similarity index 89% rename from src/Queue.java rename to src/interfaces/Queue.java index ab5ca29..bceaf58 100644 --- a/src/Queue.java +++ b/src/interfaces/Queue.java @@ -1,5 +1,7 @@ +package interfaces; + /** - * FIFO (first-in, first-out) Queue API + * FIFO (first-in, first-out) interfaces.Queue API * @param class / data type of the items in the queue */ public interface Queue extends Iterable { From eb7054585df5e71691853fa535d30caaa8772e0d Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Sun, 25 Feb 2024 14:05:38 -0800 Subject: [PATCH 18/20] finished tests and all implementations of data structures. scanning for erros --- src/LinkedBag.java | 21 ++++++++++++++---- src/LinkedListTest.java | 4 ++++ src/LinkedQueue.java | 15 ++++++++----- src/LinkedStack.java | 43 +++++++++++++++++++++++-------------- src/QueueTestClient.java | 3 +-- src/ResizingArrayStack.java | 15 +++++++------ src/StackTestClient.java | 6 +++--- src/Stats.java | 34 ++++++++++++++++++++++++++++- 8 files changed, 104 insertions(+), 37 deletions(-) diff --git a/src/LinkedBag.java b/src/LinkedBag.java index 4041dcc..c19c9f6 100644 --- a/src/LinkedBag.java +++ b/src/LinkedBag.java @@ -5,6 +5,11 @@ import java.util.Spliterator; import java.util.function.Consumer; +/** + * Ryder Dettloff + * LinkedBag data structure + * @param + */ public class LinkedBag implements Bag { //fields @@ -24,35 +29,43 @@ public LinkedBag(){ } /** * Add an item to the bag. - * + * time complexity: O(N)(Constant) simple operations. creating/assigning variables. size of bag doesn't matter * @param item the item to be added */ @Override public void add(E item) { - head = new Node(); + //assign head to prev node + Node prev = head; + //create new node to be the head + head = new Node(); + //assign new item to heads data head.data = item; + //set the new head's next node to previous head.nextNode = prev; + //increment size size++; } /** * Checks to see if the bag is empty. - * + *time complexity: O(N)(Constant) comparing size to 0 * @return true if the bag is empty, false otherwise */ @Override public boolean isEmpty() { + //compare size to 0, return boolean value return size == 0; } /** * Returns a count of the number of items in the bag. - * + *time complexity: O(N)(Constant) returing size variab;e * @return the number of items in the bag */ @Override public int size() { + //return size variable return size; } diff --git a/src/LinkedListTest.java b/src/LinkedListTest.java index c2ea9b6..8f01092 100644 --- a/src/LinkedListTest.java +++ b/src/LinkedListTest.java @@ -2,6 +2,10 @@ import static org.junit.jupiter.api.Assertions.*; +/** + * Ryder Dettloff + * test for ResizingArrayStack and LinkedStack + */ /* I need to get debug, I think something is wrong with my getter.. I will review and see if I can get it fixed asap. diff --git a/src/LinkedQueue.java b/src/LinkedQueue.java index 583de84..93a67e7 100644 --- a/src/LinkedQueue.java +++ b/src/LinkedQueue.java @@ -9,6 +9,8 @@ */ public class LinkedQueue implements Queue { + + //data fields for queue private Node head; private Node tail; private int size; @@ -16,10 +18,11 @@ public class LinkedQueue implements Queue { private class Node { + //create class node E data; Node nextNode; } - + //create constructor for LinkedQueue public LinkedQueue() { this.head = null; @@ -29,7 +32,7 @@ public LinkedQueue() /** * Add an item to the queue. - * + * time complexity: O(N)(Constant) simple operations. creating/assigning variables. size of queue doesn't matter * @param item the item to be added */ @Override @@ -52,7 +55,7 @@ public void enqueue(E item) { /** * Remove an item from the queue. - * + * time complexity: O(N)(Constant) simple operations. removing variables. size of queue doesn't matter * @return the item that was removed */ @Override @@ -74,21 +77,23 @@ public E dequeue() { /** * Checks to see if the queue is empty. - * + * time complexity: O(N)(Constant) comparing variable to 0 * @return true if the queue is empty, false otherwise */ @Override public boolean isEmpty() { + //compare size to 0 return boolean value return size == 0; } /** * Returns a count of the number of items in the queue. - * + *time complexity: O(N)(Constant) returning size variable * @return the number of items in the queue */ @Override public int size() { + //return size variable return size; } diff --git a/src/LinkedStack.java b/src/LinkedStack.java index dbe28e8..39edfc8 100644 --- a/src/LinkedStack.java +++ b/src/LinkedStack.java @@ -1,17 +1,20 @@ import java.util.Iterator; -import java.util.ListIterator; import java.util.NoSuchElementException; import interfaces.Stack; -import interfaces.Stack; +/** + * Ryder Dettloff + * LinkedStack Data Structure + * @param + */ public class LinkedStack implements Stack { - //fields + //node fields private Node head; private int size; - //Node constructor + //Node class private class Node { E data; @@ -19,48 +22,54 @@ private class Node } /** * Add an item to the stack. - * + * time complexity: O(N)(Constant) simple operations * @param item the item to be added */ @Override public void push(E item) { + //creat new head node head = new Node(); + //assign head to prev node Node prev = head; + //assign heads data to new item head.data = item; + //set heads next node to previous head.nextNode = prev; + //increment size size++; } /** * Removes the most recently added item from the stack. - * + * time complexity: O(N)(Constant) simple operations * @return the item that was removed */ @Override public E pop() { - if(isEmpty()) - { + if(isEmpty()) { + //check if stack is empty throw new IllegalStateException("The stack is empty"); } - + //assign heads data to item E item = head.data; + //set head to the heads next node head = head.nextNode; + //decrement size size--; + //return item return item; } /** * Returns the item at the top of the stack. * Does not modify the stack or the item at the top. - * + * time complexity: O(N)(Constant) simple operations (finding head of stack) * @return item at the top of the stack. */ @Override - public E peek() - { + public E peek() { //throw exception if stack is empty - if(isEmpty()) - { + if(isEmpty()) { throw new IllegalStateException("Stack is empty, cannot peek()"); } //finds and returns the top of the stack @@ -68,21 +77,23 @@ public E peek() } /** * Checks to see if the stack is empty. - * + * time complexity: O(N)(Constant) comparing size to 0 * @return true if the stack is empty, false otherwise */ @Override public boolean isEmpty() { + //compare size to 0 and return boolean value return size == 0; } /** * Returns a count of the number of items in the stack. - * + * time complexity: O(N)(Constant) returning size variable * @return the number of items in the stack */ @Override public int size() { + //return size variable return size; } diff --git a/src/QueueTestClient.java b/src/QueueTestClient.java index 77aed75..dc473a5 100644 --- a/src/QueueTestClient.java +++ b/src/QueueTestClient.java @@ -10,8 +10,7 @@ */ public class QueueTestClient { - public static void main(String[] args) - { + public static void main(String[] args) { Queue s = new LinkedQueue<>(); Scanner in = new Scanner("to be or not to - be - - that - - - is"); // checks next string for "-" if it doesn't contain it places it at the end of the queue diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java index d7aed7e..cdc7246 100644 --- a/src/ResizingArrayStack.java +++ b/src/ResizingArrayStack.java @@ -9,11 +9,13 @@ * @param */ public class ResizingArrayStack implements Stack { - //fields + //Array Stack Fields private E[] buffer; private int size; private static final int INITIAL_SIZE = 10; + + //Array Stack Constructor public ResizingArrayStack() { this.buffer = (E[]) new Object[INITIAL_SIZE]; this.size = 0; @@ -22,7 +24,7 @@ public ResizingArrayStack() { /** * doubles the size of the buffer to be used in the push method - * @param increaseBuffer + * (HELPER METHOD) */ private void increaseBuffer(int increaseBuffer) { //helper for push/pop doubles size of array and copies it over to the new one @@ -34,6 +36,7 @@ private void increaseBuffer(int increaseBuffer) { * Add an item to the stack. * checks to see if buffer is of size then pushes the item to the stack * @param item the item to be added + * Time Complexity: is O(1)(constant) becuase of simple indexing (could take O(N)(Linear) if array needs to be resized Worst case) */ @Override @@ -49,7 +52,7 @@ public void push(E item) { /** * Removes the most recently added item from the stack. - * + * Time Complexity O(1)(constant) due to simple indexing * @return the item that was removed */ @Override @@ -72,7 +75,7 @@ public E pop() { /** * Returns the item at the top of the stack. * Does not modify the stack or the item at the top. - * + * Time Complexity O(1)(constant) due to simple indexing * @return item at the top of the stack. */ @Override @@ -88,7 +91,7 @@ public E peek() { /** * Checks to see if the stack is empty. - * + * Time Complexity O(1)(constant) due to checking value of size compared to 0 * @return true if the stack is empty, false otherwise */ @Override @@ -98,7 +101,7 @@ public boolean isEmpty() { /** * Returns a count of the number of items in the stack. - * + * Time Complexity O(1) (constant) due to just checking value of arrau * @return the number of items in the stack */ @Override diff --git a/src/StackTestClient.java b/src/StackTestClient.java index 6bb4245..6ac21db 100644 --- a/src/StackTestClient.java +++ b/src/StackTestClient.java @@ -9,14 +9,14 @@ public class StackTestClient { public static void main(String[] args) { - - Stack s = new LinkedStack(); +//create new stack to hold "-" items + Stack s = new LinkedStack<>(); Scanner in = new Scanner("to be or not to - be - - that - - - is"); while(in.hasNext()) { String item = in.next(); - + //push "-" items to stack if(item.equals("-")) { s.push("item"); } else if(!s.isEmpty()) { diff --git a/src/Stats.java b/src/Stats.java index aeb1479..910157a 100644 --- a/src/Stats.java +++ b/src/Stats.java @@ -1,2 +1,34 @@ +import java.util.Scanner; + +/** + * Ryder Dettloff + * test for linked bag implementation + */ public class Stats { -} + public static void main(String[] args) { + //create new bag and add numbers into the bag + LinkedBag numbers = new LinkedBag<>(); + Scanner in = new Scanner("100 99 101 120 98 107 109 81 101 90"); + while(in.hasNextDouble()) { + numbers.add(in.nextDouble()); + } + // get the sum of all the numbers to prepare to find the mean + int bagSize = numbers.size(); + double sum = 0.0; + for (double x : numbers) { + sum += x; + } + //find mean but dividing total of numbers bu the number of items in the bag + double mean = sum / bagSize; + //reset sum + sum = 0.0; + //now use mean to find the standard deviation + for (double x : numbers) { + sum += (x - mean)*(x - mean); + } + + double standardDev = Math.sqrt(sum/(bagSize-1)); + System.out.printf("Mean: %.2f\n", mean); + System.out.printf("Standard deviation: %.2f\n", standardDev); + } +} \ No newline at end of file From bca9611b44ee195482a72401eeb1e1e5c34032c5 Mon Sep 17 00:00:00 2001 From: Ryder Dettloff <114546927+rldettloff@users.noreply.github.com> Date: Sun, 25 Feb 2024 14:06:34 -0800 Subject: [PATCH 19/20] Update ArrayList.java finished runtime analysis --- src/ArrayList.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index f273ec0..8e02a20 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -4,7 +4,6 @@ import java.util.Iterator; import java.util.NoSuchElementException; /* -Need to finish methods and runtime analysis @ Ryder Dettloff @ version 1.0 @ 02-13-2024 From f2519c5c02217e9863e78c52530fe768f2407c6f Mon Sep 17 00:00:00 2001 From: ryderdettloff Date: Sun, 25 Feb 2024 14:08:22 -0800 Subject: [PATCH 20/20] changed comments --- src/ResizingArrayStack.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java index cdc7246..ae30912 100644 --- a/src/ResizingArrayStack.java +++ b/src/ResizingArrayStack.java @@ -120,7 +120,6 @@ public Iterator iterator() { } - //copied from my ArrayList class , need to update private class ArrayListIterator implements Iterator { private int currentIndex = 0;