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..f4106d6 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -1,12 +1,39 @@
public class Main {
public static void main(String[] args) {
- System.out.println("Hello world!");
Deque d1 = new ResizingArrayDeque<>();
// some test code here
+ System.out.println(d1.size());//0
+ d1.addLast("Me");
+
+ d1.addLast("Myself");
+ d1.addLast("I");
+
+ System.out.println(d1);//okay to this point
+
+ //System.out.println(d1.removeFirst());//
+ System.out.println(d1);//
+ System.out.println(d1.removeLast());
+
+
+ System.out.println(d1);
+ System.out.println(d1.size());
+ d1.addFirst("Try");
+ d1.addFirst("again");
+ System.out.println(d1.removeLast());
+
Deque d2 = new SinglyLinkedDeque<>();
// some test code here
+ d2.addFirst("New");
+ d2.addFirst("Node");
+ System.out.println(d2.size());
+ d2.addLast("Another");
+ d2.addLast("attempt");
+ System.out.println(d2.removeFirst());
+
+ System.out.println(d2.size());
+ System.out.println(d2.removeLast());
}
}
\ No newline at end of file
diff --git a/src/ResizingArrayDeque.java b/src/ResizingArrayDeque.java
index d1bca63..b267e8d 100644
--- a/src/ResizingArrayDeque.java
+++ b/src/ResizingArrayDeque.java
@@ -26,13 +26,33 @@ public int size() {
/**
* Adds an item to the front of the deque.
+ * public void add(int index, ItemType item) {//this adds to index and moves the rest to the end
+ * (can be used for add to front by changing index to i>size and this.data[0]=item
+ * checkSize();
+ * size++;
+ * for (int i = size; i>=index; i--){
+ * data[i] = data[i-1];
+ * }
+ * this.data[index]=item;
*
* @param item item to be added
*/
@Override
public void addFirst(ItemType item) {
+ checkSize();
// consider the case of adding to an empty list
+ if(size==0){
+ data[0]=item;
+ size++;
+ }
// consider the case of adding to a non-empty list
+ else{
+ size++;
+ for (int i = size; i >0 ; i--) {
+ data[i+1]=data[i];
+ data[0]=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.
@@ -45,6 +65,12 @@ public void addFirst(ItemType item) {
*/
@Override
public void addLast(ItemType item) {
+ checkSize();
+ if(size==0){
+ data[0]=item;
+ }else{
+ data[size]=item;
+ }size++;
// consider the case of adding to an empty list
// consider the case of adding to a non-empty list
@@ -60,17 +86,17 @@ 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;
+ ItemType thingToReturn = data[0];
+ if(size==0){
+ return null;
+ }else if(size==1){
+ size--;
+ }else{
+ for (int i = 0; i < size-1; i++) {
+ data[i] = data[i+1];
+ }
+ }size--;
+ return thingToReturn;
}
/**
@@ -80,18 +106,22 @@ 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?
+ ItemType thingToReturn = data[size-1];
+ if(size==0){
+ return null;
+ }else {
+ size--;
+ }
+ return thingToReturn;
+ }
- // 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
+ public String toString(){
+ String result = "";
+ for (int i = 0; i < size; i++) {
+ result += data[i] + " ";
- return null;
+ }
+ return result;
}
// 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..2c97480 100644
--- a/src/SinglyLinkedDeque.java
+++ b/src/SinglyLinkedDeque.java
@@ -34,8 +34,18 @@ 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
+ Node newNode = new Node();
+ if(size==0){
+ newNode.data = item;
+ newNode.next = null;
+ head = newNode;
+ size++;
+ }else {
+ newNode.data = item;
+ newNode.next = head;
+ head = newNode;
+ size++;
+ }
}
/**
@@ -45,6 +55,22 @@ public void addFirst(ItemType item) {
*/
@Override
public void addLast(ItemType item) {
+ Node newNode = new Node();
+ if(size==0){
+ newNode.data = item;
+ newNode.next = head;
+ head = newNode;
+ size++;
+ }else{
+ Node current = head;
+ while (current.next !=null) {
+ current=current.next;
+ }
+ newNode.data = item;
+ newNode.next = current.next;
+ current.next = newNode;
+ size++;
+ }
// consider the case of adding to an empty list
// consider the case of adding to a non-empty list
}
@@ -56,18 +82,19 @@ public void addLast(ItemType item) {
*/
@Override
public ItemType removeFirst() {
+ ItemType thingToReturn;
// check if empty
- // if empty: do nothing and return null
-
- // if there's only one item: is this a special case?
+ if(size==0){
+ thingToReturn= null;
+ }else {
+ thingToReturn = head.data;
+ Node current = new Node();
+ current.next = head;
+ head = current.next.next;
+ size--;
+ }
- // 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;
+ return thingToReturn;
}
/**
@@ -77,17 +104,18 @@ public ItemType removeFirst() {
*/
@Override
public ItemType removeLast() {
+ ItemType thingToReturn;
// 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){
+ thingToReturn= null;
+ }else {
+ Node current = head;
+ while (current.next!=null){
+ current = current.next;
+ }
+ thingToReturn = current.data;
+ }size --;
- return null;
+ return thingToReturn;
}
}