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.

37 changes: 34 additions & 3 deletions src/ResizingArrayDeque.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,28 @@ public int size() {
public void addFirst(ItemType item) {
// consider the case of adding to an empty list
// consider the case of adding to a non-empty list

// 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();

while(size != 0) {

}
int index = 0;
for (int i = size; i >= data.length + 1; i--) {
data[i] = data[i - 1];
index ++;
}

data[index] = item;
size++;

}




/**
* Adds an item to the back of the deque.
*
Expand All @@ -50,6 +67,9 @@ 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;

}

/**
Expand All @@ -61,7 +81,9 @@ 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?

// if not empty:
Expand All @@ -82,16 +104,25 @@ 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?

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

// helper method to check to see if the size has reached the capacity
Expand Down
47 changes: 42 additions & 5 deletions src/SinglyLinkedDeque.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public int size() {
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 theNewOne = new Node();
theNewOne.data = item;
theNewOne.next = head;
head = theNewOne;
}

/**
Expand All @@ -46,7 +50,23 @@ public void addFirst(ItemType item) {
@Override
public void addLast(ItemType item) {
// consider the case of adding to an empty list
if (head == null) {
Node newNode = new Node();
newNode.data = item;
newNode.next = null;
size++;
}
// consider the case of adding to a non-empty list
Node current = head;
while (current.next != null) {
current = current.next;

Node theNewOne = new Node();
theNewOne.data = item;
theNewOne.next = null;
current.next = theNewOne;
size++;
}
}

/**
Expand All @@ -58,16 +78,24 @@ 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?
Node current = head;
for (int i = 0; 1 < size - size + 1; i++){
current = current.next;
}
current.next = current.next.next;
size--;

return current.data;

// 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 @@ -79,7 +107,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?

// if not empty, has more than one item:
Expand All @@ -88,6 +118,13 @@ 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;
Node current = head;
for (int i = 0; 1 < size - 1; i++){
current = current.next;
}
current.next = current.next.next;
size--;

return current.data;
}
}