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.

40 changes: 40 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,49 @@ public static void main(String[] args) {

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

}
}
94 changes: 71 additions & 23 deletions src/ResizingArrayDeque.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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<size; i++) {
//shift all items in the array back one index
data[i] = data[i+1];
}
size--;
// 3. return the variable that has the saved copy of the item at the front
return removed;
}
}

/**
Expand All @@ -82,16 +115,23 @@ 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) {// 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, 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
ItemType removed = data[size-1];
// 2. remove the item at the back
data[size-1] = null;
size--;
// 3. return the variable that has the saved copy of the item at the back
return removed;
}
}

// helper method to check to see if the size has reached the capacity
Expand All @@ -116,4 +156,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;
}
}
110 changes: 89 additions & 21 deletions src/SinglyLinkedDeque.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,23 @@ 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) {
Node newNode = new Node();
newNode.data = item;
newNode.next = null;
head = newNode;
size++;
} else {// consider the case of adding to a non-empty list
// create a new node
Node newNode = new Node();
// give the newNode the item as its data
newNode.data = item;
// set newNode's next equal to the head
newNode.next = head;
// set the head equal to the newNode
head = newNode;
size++;
}
}

/**
Expand All @@ -46,7 +62,28 @@ 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) {
Node newNode = new Node();
newNode.data = item;
newNode.next = null;
head = newNode;
size++;
} else {// consider the case of adding to a non-empty list
// create a new node with the item as its data
Node newNode = new Node();
newNode.data = item;
newNode.next = null;
// loop until the end of the list, keeping track of the current node
Node current = head;
while (current.next != null) {
// move current to the next node
current = current.next;
}
// at the end of the list....
// set the next node equal to the new node
current.next = newNode;
size++;
}
}

/**
Expand All @@ -58,16 +95,22 @@ 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) {// if there's only one item: is this a special case?
ItemType removed = head.data;
head = null;
size--;
return removed;
} else { // if not empty:
// 1. make a variable to save a copy of the item at the front
ItemType removed = head.data;
// 2. remove the item at the front
head = head.next;
// 3. return the variable that has the saved copy of the item at the front
size--;
return removed;
}
}

/**
Expand All @@ -79,15 +122,40 @@ public ItemType removeFirst() {
public ItemType removeLast() {
// check if empty
// if empty: do nothing and return null
if (size == 0) {
return null;
} else if (size == 1) {// if there's only one item: is this a special case?
ItemType removed = head.data;
head = null;
size--;
return removed;
} else { // if not empty:
// 0. figure out a way to access the item in the back
// create nodes to keep track of current and previous
Node previous = null;
Node current = head;
// loop to the end of the list, keeping track of current
while (current.next != null) {
previous = current;
current = current.next;
}
// 1. make a variable to save a copy of the item at the back
ItemType removed = current.data;
// 2. remove the item at the back
previous.next = null;
size--;
// 3. return the variable that has the saved copy of the item at the back
return removed;
}
}

// 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;
public String toString() {
String result = "";
Node current = head;
while (current != null) {
result += current.data + " ";
current = current.next;
}
return result;
}
}