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/Main.java b/src/Main.java index c502941..e86c682 100644 --- a/src/Main.java +++ b/src/Main.java @@ -5,6 +5,19 @@ public static void main(String[] args) { Deque d1 = new ResizingArrayDeque<>(); // some test code here + // Max: both remove methods on if array is empty, print out nothing to remove + d1.removeFirst(); + System.out.println(d1); + d1.removeLast(); + System.out.println(d1); + + //Max: received an error + //Max: attempted to fix, received a null instead of error + //Max: third attempt to fix, received same error + + d1.addFirst("Hello"); + System.out.println(d1); + Deque d2 = new SinglyLinkedDeque<>(); // some test code here diff --git a/src/ResizingArrayDeque.java b/src/ResizingArrayDeque.java index d1bca63..e27fd78 100644 --- a/src/ResizingArrayDeque.java +++ b/src/ResizingArrayDeque.java @@ -1,3 +1,5 @@ +import org.w3c.dom.Node; + public class ResizingArrayDeque implements Deque { // constants public static int DEFAULT_CAPACITY = 10; @@ -34,6 +36,31 @@ public void addFirst(ItemType item) { // consider the case of adding to an empty list // consider the case of adding to a non-empty list + ItemType[] temp = data; + //Max: Step 1 - Check if it is empty + + if (size == 0){ + // Max: if it is empty, add item to the arraylist + + // Max: Increase the size of array + size++; + } else{ // Max: If it is not empty + // Max: Create a temporary variable to store the current first item in array list + + //Max: go through the arraylist and store the latest item in the temporary + for (int i = 0; i < data.length; i++) { + temp = data; + } + + //Max: store the temp so that it will still be in the list and increase the size + + //Max: Add the item to the latest size which is currently null + + //Max: successfully increase size, keeping recent addition and adding the newest one + + + } + // There is a private helper method checkSize() defined below to check/resize // that you can call as needed to check if the array is full and resize it. } @@ -48,6 +75,17 @@ public void addLast(ItemType item) { // consider the case of adding to an empty list // consider the case of adding to a non-empty list + // Max: if the list is empty, just add to the first spot + if (size == 0){ + // Max: add the item to the array and increase the size + + } else { //Max: if it is not empty... + // Max: backtrack the array adding an index to each spot + + //Max: replace index 0 with the item + + } + // There is a private helper method checkSize() defined below to check/resize // that you can call as needed to check if the array is full and resize it. } @@ -62,6 +100,13 @@ public ItemType removeFirst() { // check if empty // if empty: do nothing and return null + // Max: if the list is empty, send a message saying there is nothing to remove + if (size == 0){ + System.out.println("This list is empty, nothing to remove"); + return null; + } else { // Max: if the list does have something... + //Max: go through the array find the most recent index and remove it + } // if there's only one item: is this a special case? // if not empty: @@ -83,6 +128,14 @@ public ItemType removeLast() { // check if empty // if empty: do nothing and return null + // Max: if the list is empty, send a message saying there is nothing to remove + if (size == 0){ + System.out.println("This list is empty, nothing to remove"); + return null; + } else { // Max: if the list does have something... + + } + // if there is only one item: is this a special case? // if not empty, has more than one item: @@ -116,4 +169,13 @@ private void checkSize() { temp = null; } // end of if (need to resize) } + + // Max: adding the toString method to test the code + public String toString(){ + String result = ""; + for (int i = 0; i < size; i++) { + result += data[i] + " "; + } + return result; + } }