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.

33 changes: 33 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,38 @@ public static void main(String[] args) {
Deque<String> d2 = new SinglyLinkedDeque<>();
// some test code here

d1.addFirst("This ");
d1.addLast("is ");
d1.addFirst("resizing array!");

d2.addFirst("Hello");
d2.addLast("World");
d2.addFirst("I say to you...");

System.out.println(d1.size()); //3
System.out.println(d2.size()); //3

System.out.println(d2.removeLast());
System.out.println(d2.removeFirst());
System.out.println(d2.removeLast());
System.out.println();

System.out.println(d1.removeLast());
System.out.println(d1.removeFirst());
System.out.println(d1.removeLast());

System.out.println(d1.size()); //0
System.out.println(d2.size()); //0

d1.addLast("Add Last");
d2.addFirst("Linked Add Last");

// System.out.println(d1.size()); //0
System.out.println(d2.size()); //0

System.out.println(d1.removeFirst());
System.out.println(d1.removeFirst());
// System.out.println(d1.removeFirst());

}
}
54 changes: 51 additions & 3 deletions src/ResizingArrayDeque.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ 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 {
for (int i = size; i > 0; i--) {
data[i] = data[i - 1];
}
data[0] = item;
}
size++;
}

/**
Expand All @@ -50,6 +60,13 @@ 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();
if(size == 0){
data[0] = item;
}else{
data[size] = item;
}
size++;
}

/**
Expand All @@ -69,8 +86,24 @@ public ItemType removeFirst() {
// 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 returnThis = data[0];
data[0] = null;
size--;
return returnThis;
}
else{
ItemType returnThis = data[0];
for(int i = 0; i< size ; i++){
data[i] = data[i+1];
}
data[size] = null;
size--;
return returnThis;
}
}

/**
Expand All @@ -90,8 +123,23 @@ public ItemType removeLast() {
// 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 returnThis = data[0];
data[0] = null;
size--;
return returnThis;

}
else{
ItemType returnThis = data[size -1];
data[size -1] = null;
size--;
return returnThis;
}

return null;
}

// helper method to check to see if the size has reached the capacity
Expand Down
90 changes: 88 additions & 2 deletions src/SinglyLinkedDeque.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,28 @@ public int size() {
*/
@Override
public void addFirst(ItemType item) {
if(item == null){
throw new NullPointerException();
}
// consider the case of adding to an empty list
// consider the case of adding to a non-empty list

//case for empty list
if(head == null){
//create new node and have head point to it
Node addThis = new Node();
addThis.data = item;
addThis.next = null;
head = addThis;
}else{
//otherwise create new node, have it point to old head, then make it the head
Node insert = new Node();
insert.data = item;
insert.next = head;
head = insert;
}
//track size
size++;
}

/**
Expand All @@ -45,8 +65,33 @@ public void addFirst(ItemType item) {
*/
@Override
public void addLast(ItemType item) {
if(item == null){
throw new NullPointerException();
}
// consider the case of adding to an empty list
// consider the case of adding to a non-empty list

//case for empty list:
if(head == null){
//made a new node and have head point to it
Node addThis = new Node();
addThis.data = item;
addThis.next = null;
head = addThis;
}else{
//otherwise get to last node
Node currentNode = head;
while(currentNode.next != null){
currentNode = currentNode.next;
}
//then have it point to a new node rather than null
Node insert = new Node();
insert.data = item;
insert.next = null;
currentNode.next = insert;
}
//keep track of size
size++;
}

/**
Expand All @@ -67,7 +112,27 @@ 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;
//check empty
if(head == null){
return null;
}
//check if one item in list
else if(head.next == null){
//take head data, make head null, track size, then return data
ItemType returnThis = head.data;
head = null;
size--;
return returnThis;

}else{
//take head data, make next item head, track size, return data
ItemType returnThis = head.data;
head = head.next;
size--;
return returnThis;

}

}

/**
Expand All @@ -88,6 +153,27 @@ 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;
//if empty
if (head == null) {
return null;
}
//if one item take head data, make head null, track size, then return data
else if (head.next == null) {
ItemType returnThis = head.data;
head = null;
size--;
return returnThis;
}
//else get to one item before end of list, grab end item's data to return and make second to last item next = null, return data
else {
Node currentNode = head;
while (currentNode.next.next != null) {
currentNode = currentNode.next;
}
ItemType returnThis = currentNode.next.data;
currentNode.next = null;
size--;
return returnThis;
}
}
}