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..462109d 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -3,10 +3,49 @@ public static void main(String[] args) {
System.out.println("Hello world!");
Deque d1 = new ResizingArrayDeque<>();
- // some test code here
+ d1.addFirst("Hello");
+ d1.addFirst("Bye");
+ System.out.println(d1.size());
+ System.out.println(d1.removeFirst());
+ System.out.println(d1.size());
+ System.out.println(d1.removeFirst());
+ System.out.println(d1.size());
+ d1.addFirst("Hello");
+ d1.addFirst("Bye");
+ d1.addLast("Hola");
+ d1.addLast("Adios");
+ System.out.println(d1.size());
+ System.out.println(d1.removeLast());
+ System.out.println(d1.size());
+ System.out.println(d1.removeLast());
+ System.out.println(d1.size());
+ System.out.println(d1.removeLast());
+ System.out.println(d1.size());
+ System.out.println(d1.removeLast());
+ System.out.println(d1.size());
+
Deque d2 = new SinglyLinkedDeque<>();
- // some test code here
+ d2.addFirst("Hello");
+ d2.addFirst("Bye");
+ System.out.println(d2.size());
+ System.out.println(d2.removeFirst());
+ System.out.println(d2.size());
+ System.out.println(d2.removeFirst());
+ System.out.println(d2.size());
+ d2.addFirst("Hello");
+ d2.addFirst("Bye");
+ d2.addLast("Hola");
+ d2.addLast("Adios");
+ System.out.println(d2.size());
+ System.out.println(d2.removeLast());
+ System.out.println(d2.size());
+ System.out.println(d2.removeLast());
+ System.out.println(d2.size());
+ System.out.println(d2.removeLast());
+ System.out.println(d2.size());
+ System.out.println(d2.removeLast());
+ System.out.println(d2.size());
}
}
\ No newline at end of file
diff --git a/src/ResizingArrayDeque.java b/src/ResizingArrayDeque.java
index d1bca63..5840e8d 100644
--- a/src/ResizingArrayDeque.java
+++ b/src/ResizingArrayDeque.java
@@ -32,6 +32,18 @@ public int size() {
@Override
public void addFirst(ItemType item) {
// consider the case of adding to an empty list
+ if(size == 0){
+ data[0] = item;
+ } else {
+ checkSize();
+ //move everything over
+ for (int i = size + 1; i > 0 ; i--) {
+ data[i] = data[i-1];
+ }
+ data[0] = item;
+ }
+ //update size
+ size++;
// consider the case of adding to a non-empty list
// There is a private helper method checkSize() defined below to check/resize
@@ -47,6 +59,13 @@ public void addFirst(ItemType item) {
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){
+ addFirst(item);
+ } else {
+ checkSize();
+ 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.
@@ -61,16 +80,21 @@ public void addLast(ItemType item) {
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){
+ return null;
+ } else if (size == 1){
+ ItemType tempVar = data[0];
+ data[0] = null;
+ size = 0;
+ return tempVar;
+ } else {
+ ItemType tempVar = data[0];
+ for (int i = 0; i < size() - 1 ; i++) {
+ data[i] = data [i +1];
+ }
+ size --;
+ return tempVar;
+ }
}
/**
@@ -82,16 +106,19 @@ public ItemType removeFirst() {
public ItemType removeLast() {
// check if empty
// if empty: do nothing and return null
-
- // if there is only one item: is this a special case?
-
- // if not empty, has more than one item:
- // 0. figure out a way to access the item in the back
- // 1. make a variable to save a copy of the item at the back
- // 2. remove the item at the back
- // 3. return the variable that has the saved copy of the item at the back
-
- return null;
+ if( size == 0 ){
+ return null;
+ } else if ( size == 1 ){
+ ItemType tempVar = data[0];
+ data[0] = null;
+ size = 0;
+ return tempVar;
+ } else {
+ ItemType tempVar = data[size() - 1];
+ data[size()-1] = null;
+ size --;
+ return tempVar;
+ }
}
// helper method to check to see if the size has reached the capacity
diff --git a/src/SinglyLinkedDeque.java b/src/SinglyLinkedDeque.java
index 5440edf..d5e0b56 100644
--- a/src/SinglyLinkedDeque.java
+++ b/src/SinglyLinkedDeque.java
@@ -35,6 +35,17 @@ public int size() {
@Override
public void addFirst(ItemType item) {
// consider the case of adding to an empty list
+ if (size == 0 ){
+ head = new Node();
+ head.data = item;
+ head.next = null;
+ } else {
+ Node theNewOne = new Node();
+ theNewOne.data = item;
+ theNewOne.next = head;
+ head = theNewOne;
+ }
+ size ++;
// consider the case of adding to a non-empty list
}
@@ -46,6 +57,20 @@ public void addFirst(ItemType item) {
@Override
public void addLast(ItemType item) {
// consider the case of adding to an empty list
+ if (size == 0 ){
+ head.data = item;
+ head.next = null;
+ } else {
+ Node currentNode = head;
+ while (currentNode.next != null ){
+ currentNode = currentNode.next;
+ }
+ Node theNewOne = new Node();
+ theNewOne.data = item;
+ theNewOne.next = null;
+ currentNode.next = theNewOne;
+ }
+ size++;
// consider the case of adding to a non-empty list
}
@@ -57,17 +82,20 @@ public void addLast(ItemType item) {
@Override
public ItemType removeFirst() {
// check if empty
- // if empty: do nothing and return null
+ if (size == 0){
+ return null;
+ } else if ( size == 1 ){
+ ItemType temp = head.data;
+ head = null;
+ size --;
+ return temp;
+ } else {
+ ItemType temp = head.data;
+ head = head.next;
+ size --;
+ return temp;
+ }
- // 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;
}
/**
@@ -78,16 +106,23 @@ public ItemType removeFirst() {
@Override
public ItemType removeLast() {
// check if empty
- // if empty: do nothing and return null
-
- // if there is only one item: is this a special case?
-
- // if not empty, has more than one item:
- // 0. figure out a way to access the item in the back
- // 1. make a variable to save a copy of the item at the back
- // 2. remove the item at the back
- // 3. return the variable that has the saved copy of the item at the back
+ if (size == 0 ){
+ return null;
+ } else if ( size == 1 ){
+ ItemType temp = head.data;
+ head = null;
+ size --;
+ return temp;
+ } else {
+ Node currentNode = head;
+ while (currentNode.next != null ){
+ currentNode = currentNode.next;
+ }
+ ItemType temp = currentNode.data;
+ currentNode = null;
+ size --;
+ return temp;
+ }
- return null;
}
}