Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 41 additions & 2 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,49 @@ public static void main(String[] args) {
System.out.println("Hello world!");

Deque<String> 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<String> 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());

}
}
67 changes: 47 additions & 20 deletions src/ResizingArrayDeque.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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;
}
}

/**
Expand All @@ -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
Expand Down
75 changes: 55 additions & 20 deletions src/SinglyLinkedDeque.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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;
}

/**
Expand All @@ -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;
}
}