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..acb5be6 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -4,9 +4,49 @@ public static void main(String[] args) {
Deque d1 = new ResizingArrayDeque<>();
// some test code here
+ d1.addFirst("Hi");
+ System.out.println(d1);
+ d1.removeFirst();
+ System.out.println(d1);
+ d1.addLast("hi");
+ System.out.println(d1);
+ d1.removeLast();
+ System.out.println(d1);
+ d1.addFirst("no");
+ d1.addFirst("yes");
+ d1.addLast("how");
+ System.out.println(d1);
+ d1.removeFirst();
+ System.out.println(d1);
+ d1.removeLast();
+ System.out.println(d1);
+
+ //
+ System.out.println("END OF ARRAY TESTS");
Deque d2 = new SinglyLinkedDeque<>();
// some test code here
+ d2.addFirst("Hi");
+ System.out.println(d2);
+ d2.removeFirst();
+ System.out.println(d2);
+ d2.addLast("hi");
+ System.out.println(d2);
+ d2.removeLast();
+ System.out.println(d2);
+ d2.addFirst("no");
+ d2.addFirst("yes");
+ d2.addLast("how");
+ System.out.println(d2);
+ d2.removeFirst();
+ System.out.println(d2);
+ d2.removeLast();
+ System.out.println(d2);
+ d2.addLast("know");
+ d2.addLast("not");
+ d2.addLast("where");
+ d2.removeLast();
+ System.out.println(d2);
}
}
\ No newline at end of file
diff --git a/src/ResizingArrayDeque.java b/src/ResizingArrayDeque.java
index d1bca63..91f18d5 100644
--- a/src/ResizingArrayDeque.java
+++ b/src/ResizingArrayDeque.java
@@ -32,7 +32,21 @@ public int size() {
@Override
public void addFirst(ItemType item) {
// consider the case of adding to an empty list
- // consider the case of adding to a non-empty list
+ if (size == 0) {
+ data[0] = item;
+ size++;
+ }else { // consider the case of adding to a non empty list
+ // check if the array needs to be resized
+ checkSize();
+ // loop from the back of the array to the front
+ for(int i=size-1; i>=0; i--) {
+ // set the next index to the current index
+ data[i+1] = data[i];
+ }
+ // set the first index to the item
+ data[0] = item;
+ size++;
+ }
// 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.
@@ -46,7 +60,16 @@ public void addFirst(ItemType item) {
@Override
public void addLast(ItemType item) {
// consider the case of adding to an empty list
- // consider the case of adding to a non-empty list
+ if (size == 0) {
+ data[0] = item;
+ size++;
+ } else { // consider the case of adding to a non-empty list
+ // check for a resize
+ checkSize();
+ // set the last (empty) index to the item
+ data[size] = item;
+ size++;
+ }
// 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.
@@ -60,17 +83,27 @@ public void addLast(ItemType item) {
@Override
public ItemType removeFirst() {
// check if empty
- // if empty: do nothing and return null
-
- // if there's only one item: is this a special case?
-
- // if not empty:
- // 0. figure out a way to access the item in the front
- // 1. make a variable to save a copy of the item at the front
- // 2. remove the item at the front
- // 3. return the variable that has the saved copy of the item at the front
-
- return null;
+ if (size == 0) {
+ // if empty: do nothing and return null
+ return null;
+ } else if (size == 1) {// if there's only one item: is this a special case?
+ ItemType removed = data[0];
+ data[0] = null;
+ size--;
+ return removed;
+ } else {// if not empty:
+ // 0. figure out a way to access the item in the front
+ ItemType removed = data[0];
+ // 1. make a variable to save a copy of the item at the front
+ // 2. remove the item at the front
+ for (int i=0; i