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.

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

Deque<String> d1 = new ResizingArrayDeque<>();
// some test code here
d1.addFirst("1");
d1.addFirst("2");
d1.addFirst("3");
d1.addFirst("4");

d1.addLast("last");
d1.addLast("next");
d1.removeLast();



Deque<String> d2 = new SinglyLinkedDeque<>();
// some test code here
Expand Down
66 changes: 64 additions & 2 deletions src/ResizingArrayDeque.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public ResizingArrayDeque() {
*/
@Override
public int size() {

return size;
}

Expand All @@ -36,6 +37,25 @@ public void addFirst(ItemType 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();

if ( size == 0 ) {
data[0] = item;
}
else {
// I want to start at Index[1] and assign it the value at Index[0]
// I then want to move to Index[2] and assign it the value at Index[1]

// Definitely not going about it correctly. Stumped!
for (int i = 1; i < size; i++) {
data[i] = data[i - 1];
}
data[0] = item;
}

size++;

}

/**
Expand All @@ -50,6 +70,12 @@ public void addLast(ItemType 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();

data[size] = item;
size++;

}

/**
Expand All @@ -62,6 +88,14 @@ public ItemType removeFirst() {
// check if empty
// if empty: do nothing and return null

// value at first index
ItemType result = data[0];

// if empty, return null and not result variable
if ( size == 0 ) {
return null;
}

// if there's only one item: is this a special case?

// if not empty:
Expand All @@ -70,7 +104,25 @@ public ItemType removeFirst() {
// 2. remove the item at the front
// 3. return the variable that has the saved copy of the item at the front

return null;

// starting at index 1, give every index the value of the next index
else {
for (int i = 0; i < size; i++) {
data[i] = data[i + 1];
}
size++;
}

// return the value of the first item
return result;
}

public String toString() {
String result = "";
for (int i = 0; i < size; i++) {
result += data[i] + " ";
}
return result;
}

/**
Expand All @@ -82,6 +134,9 @@ 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?

Expand All @@ -91,7 +146,14 @@ public ItemType removeLast() {
// 2. remove the item at the back
// 3. return the variable that has the saved copy of the item at the back

return null;
// save the value of the item at the last index
ItemType result = data[size];

// then make the last item null
data[size] = null;

// return the saved value - not the now null value
return result;
}

// helper method to check to see if the size has reached the capacity
Expand Down