Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Midterm Part 2 #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
52 changes: 52 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,58 @@ public static void main(String[] args) {

Deque<String> d2 = new SinglyLinkedDeque<>();
// some test code here
d2.addFirst("one");
d2.addFirst("two");
d2.addFirst("three");
d2.addFirst("four");
System.out.println(d2.removeFirst());
System.out.println(d2.removeFirst());
System.out.println(d2.removeFirst());
System.out.println(d2.removeFirst());
d2.addLast("one");
d2.addLast("two");
d2.addLast("three");
d2.addLast("four");
System.out.println(d2.removeFirst());
System.out.println(d2.removeFirst());
System.out.println(d2.removeFirst());
System.out.println(d2.removeFirst());
d2.addLast("one");
d2.addLast("two");
d2.addLast("three");
d2.addLast("four");
System.out.println(d2.removeLast());
System.out.println(d2.removeLast());
System.out.println(d2.removeLast());
System.out.println(d2.removeLast());

System.out.println(System.lineSeparator());
System.out.println(System.lineSeparator());

d1.addFirst("one");
d1.addFirst("two");
d1.addFirst("three");
d1.addFirst("four");
System.out.println(d1.removeFirst());
System.out.println(d1.removeFirst());
System.out.println(d1.removeFirst());
System.out.println(d1.removeFirst());
d1.addLast("one");
d1.addLast("two");
d1.addLast("three");
d1.addLast("four");
System.out.println(d1.removeFirst());
System.out.println(d1.removeFirst());
System.out.println(d1.removeFirst());
System.out.println(d1.removeFirst());
d1.addLast("one");
d1.addLast("two");
d1.addLast("three");
d1.addLast("four");
System.out.println(d1.removeLast());
System.out.println(d1.removeLast());
System.out.println(d1.removeLast());
System.out.println(d1.removeLast());

}
}
38 changes: 34 additions & 4 deletions src/ResizingArrayDeque.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ public int size() {
*/
@Override
public void addFirst(ItemType item) {
checkSize();
ItemType[] temp = (ItemType[]) new Object[this.size + 1];
temp[0] = item;
if(this.size != 0){
for (int i = 0; i < size; i++) {
temp[i+1] = data[i];
}
}
this.data = temp;
this.size++;


// consider the case of adding to an empty list
// consider the case of adding to a non-empty list

Expand All @@ -45,6 +57,9 @@ public void addFirst(ItemType item) {
*/
@Override
public void addLast(ItemType item) {
checkSize();
this.data[this.size] = item;
this.size++;
// consider the case of adding to an empty list
// consider the case of adding to a non-empty list

Expand All @@ -59,6 +74,18 @@ public void addLast(ItemType item) {
*/
@Override
public ItemType removeFirst() {
if(this.size == 0){
return null;
}else{
ItemType[] temp = (ItemType[]) new Object[this.size];
ItemType toReturn = this.data[0];
for (int i = 1; i < size; i++) {
temp[i-1] = data[i];
}
this.data = temp;
this.size--;
return toReturn;
}
// check if empty
// if empty: do nothing and return null

Expand All @@ -69,8 +96,6 @@ 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;
}

/**
Expand All @@ -83,15 +108,20 @@ public ItemType removeLast() {
// check if empty
// if empty: do nothing and return null

if(this.size == 0){
return null;
}else{
this.size--;
return this.data[this.size];
}

// 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;
}

// helper method to check to see if the size has reached the capacity
Expand Down
61 changes: 58 additions & 3 deletions src/SinglyLinkedDeque.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ public int size() {
*/
@Override
public void addFirst(ItemType item) {
//First commit test
if(this.size == 0){
Node looseNode = new Node();
looseNode.next = null;
looseNode.data = item;
this.head = looseNode;
this.size++;
}else{
Node looseNode = new Node();
looseNode.next = this.head;
looseNode.data = item;
this.head = looseNode;
this.size++;
}
// consider the case of adding to an empty list
// consider the case of adding to a non-empty list
}
Expand All @@ -45,6 +59,23 @@ public void addFirst(ItemType item) {
*/
@Override
public void addLast(ItemType item) {
if(this.size == 0){
Node looseNode = new Node();
looseNode.next = null;
looseNode.data = item;
this.head = looseNode;
this.size++;
}else{
Node looseNode = new Node();
looseNode.data = item;
looseNode.next = null;
Node currentNode = this.head;
while(currentNode.next != null){
currentNode = currentNode.next;
}
currentNode.next = looseNode;
this.size++;
}
// consider the case of adding to an empty list
// consider the case of adding to a non-empty list
}
Expand All @@ -58,6 +89,14 @@ public void addLast(ItemType item) {
public ItemType removeFirst() {
// check if empty
// if empty: do nothing and return null
if(this.size == 0){
return null;
}else{
Node looseNode = this.head;
this.head = this.head.next;
this.size--;
return looseNode.data;
}

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

Expand All @@ -66,8 +105,6 @@ 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;
}

/**
Expand All @@ -77,6 +114,25 @@ public ItemType removeFirst() {
*/
@Override
public ItemType removeLast() {

if(this.size == 0){
return null;
}else if(this.size == 1){
Node looseNode = this.head;
this.head = null;
this.size--;
return looseNode.data;
}else{
Node currentNode = this.head;
Node previousNode = new Node();
while(currentNode.next != null){
previousNode = currentNode;
currentNode = currentNode.next;
}
previousNode.next = null;
this.size--;
return currentNode.data;
}
// check if empty
// if empty: do nothing and return null

Expand All @@ -88,6 +144,5 @@ 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;
}
}