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.

11 changes: 10 additions & 1 deletion src/Main.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
public class Main {
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("hi");
d1.addFirst("there ");
//d1.addLast("nope");
d1.removeLast();
System.out.println(d1);

Deque<String> d2 = new SinglyLinkedDeque<>();
// some test code here
d2.addFirst("Hello");
d2.addFirst("Hello");

System.out.println(d2);

}
}
76 changes: 66 additions & 10 deletions src/ResizingArrayDeque.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,23 @@ public int size() {
@Override
public void addFirst(ItemType item) {
// consider the case of adding to an empty list
if(size == 0) {
data[0]=item;
}
// consider the case of adding to a non-empty list
else{
//ItemType[] temp = (ItemType[]) new Object[size];

for (int i = 0; i < size; i++) {
data[i] = data[i++];
}
//data = temp;
data[0] = item;

}
size++;
// There is a private helper method checkSize() defined below to check/resize
checkSize();
// that you can call as needed to check if the array is full and resize it.
}

Expand All @@ -46,10 +60,17 @@ public void addFirst(ItemType item) {
@Override
public void addLast(ItemType item) {
// consider the case of adding to an empty list
if(size == 0) {
data[0]=item;
}
// consider the case of adding to a non-empty list

else {
data[size + 1] = 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.
checkSize();
size++;
}

/**
Expand All @@ -61,15 +82,28 @@ public void addLast(ItemType item) {
public ItemType removeFirst() {
// check if empty
// if empty: do nothing and return null

if(size == 0) {
return null;
}
// if there's only one item: is this a special case?

else if (size == 1) {
data[size] = null;
}
// 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
else {
ItemType[] temp = (ItemType[]) new Object[size];

for (int i = 0; i < size; i++) {
temp[i] = data[i++];
}
data = temp;
}

size--;
return null;
}

Expand All @@ -82,14 +116,28 @@ public ItemType removeFirst() {
public ItemType removeLast() {
// check if empty
// if empty: do nothing and return null

if (size == 0) {
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
else if (size >= 1) {
data[size] = null;
} else {
// 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
// ItemType[] temp = (ItemType[]) new Object[size];
//
// for (int i = 0; i < size; i++) {
// temp[i] = data[i];
// }
// temp[size] = null;
// data = temp;
//data[size] = null;
}
size--;

return null;
}
Expand All @@ -116,4 +164,12 @@ private void checkSize() {
temp = null;
} // end of if (need to resize)
}

public String toString() {
String result = "";
for (int i = 0; i < size; i++) {
result += data[i] + "";
}
return result;
}
}
66 changes: 63 additions & 3 deletions src/SinglyLinkedDeque.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ public int size() {
@Override
public void addFirst(ItemType item) {
// consider the case of adding to an empty list
if(size ==0) {
head.data = item;
head.next = null;
}
// consider the case of adding to a non-empty list
else {
Node newFirst = new Node();
newFirst.data = item;
newFirst.next = head;
}
size++;
}

/**
Expand All @@ -46,9 +56,22 @@ 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;
}
// consider the case of adding to a non-empty list
else {
Node newLast = new Node();
while (head.data != null) {


}

} size++;
}


/**
* Remove and return the item from the front, does nothing if empty.
*
Expand All @@ -58,15 +81,27 @@ public void addLast(ItemType item) {
public ItemType removeFirst() {
// check if empty
// if empty: do nothing and return null

if(size == 0) {
return null;
}
// if there's only one item: is this a special case?

else if (size == 1) {
head = null;
}
// 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
else {
Node current = head;
while(current.next != null) {
current = current.next;
current.next = current.next.next;
}

}
size --;
return null;
}

Expand All @@ -80,14 +115,39 @@ 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(size == 0) {
return null;
}
// if there's only one item: is this a special case?
else if (size == 1) {
head = null;
}

// 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
else {
Node current = head;
while (current.data != null) {
current.next = current;
current = current.next ;
}
}

size --;
return null;
}

public String toString() {
String result = "";
Node current = head;
while (current!=null) {
result += current.data + " ";
current = current.next;
}
return result;
}

}