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.

29 changes: 28 additions & 1 deletion src/Main.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");

Deque<String> d1 = new ResizingArrayDeque<>();
// some test code here
System.out.println(d1.size());//0
d1.addLast("Me");

d1.addLast("Myself");
d1.addLast("I");

System.out.println(d1);//okay to this point

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


System.out.println(d1);
System.out.println(d1.size());
d1.addFirst("Try");
d1.addFirst("again");
System.out.println(d1.removeLast());


Deque<String> d2 = new SinglyLinkedDeque<>();
// some test code here
d2.addFirst("New");
d2.addFirst("Node");
System.out.println(d2.size());
d2.addLast("Another");
d2.addLast("attempt");
System.out.println(d2.removeFirst());

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

}
}
72 changes: 51 additions & 21 deletions src/ResizingArrayDeque.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,33 @@ public int size() {

/**
* Adds an item to the front of the deque.
* public void add(int index, ItemType item) {//this adds to index and moves the rest to the end
* (can be used for add to front by changing index to i>size and this.data[0]=item
* checkSize();
* size++;
* for (int i = size; i>=index; i--){
* data[i] = data[i-1];
* }
* this.data[index]=item;
*
* @param item item to be added
*/
@Override
public void addFirst(ItemType item) {
checkSize();
// consider the case of adding to an empty list
if(size==0){
data[0]=item;
size++;
}
// consider the case of adding to a non-empty list
else{
size++;
for (int i = size; i >0 ; i--) {
data[i+1]=data[i];
data[0]=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.
Expand All @@ -45,6 +65,12 @@ public void addFirst(ItemType item) {
*/
@Override
public void addLast(ItemType item) {
checkSize();
if(size==0){
data[0]=item;
}else{
data[size]=item;
}size++;
// consider the case of adding to an empty list
// consider the case of adding to a non-empty list

Expand All @@ -60,17 +86,17 @@ 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;
ItemType thingToReturn = data[0];
if(size==0){
return null;
}else if(size==1){
size--;
}else{
for (int i = 0; i < size-1; i++) {
data[i] = data[i+1];
}
}size--;
return thingToReturn;
}

/**
Expand All @@ -80,18 +106,22 @@ public ItemType removeFirst() {
*/
@Override
public ItemType removeLast() {
// check if empty
// if empty: do nothing and return null

// if there is only one item: is this a special case?
ItemType thingToReturn = data[size-1];
if(size==0){
return null;
}else {
size--;
}
return thingToReturn;
}

// 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
public String toString(){
String result = "";
for (int i = 0; i < size; i++) {
result += data[i] + " ";

return null;
}
return result;
}

// helper method to check to see if the size has reached the capacity
Expand Down
72 changes: 50 additions & 22 deletions src/SinglyLinkedDeque.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,18 @@ 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
Node newNode = new Node();
if(size==0){
newNode.data = item;
newNode.next = null;
head = newNode;
size++;
}else {
newNode.data = item;
newNode.next = head;
head = newNode;
size++;
}
}

/**
Expand All @@ -45,6 +55,22 @@ public void addFirst(ItemType item) {
*/
@Override
public void addLast(ItemType item) {
Node newNode = new Node();
if(size==0){
newNode.data = item;
newNode.next = head;
head = newNode;
size++;
}else{
Node current = head;
while (current.next !=null) {
current=current.next;
}
newNode.data = item;
newNode.next = current.next;
current.next = newNode;
size++;
}
// consider the case of adding to an empty list
// consider the case of adding to a non-empty list
}
Expand All @@ -56,18 +82,19 @@ public void addLast(ItemType item) {
*/
@Override
public ItemType removeFirst() {
ItemType thingToReturn;
// check if empty
// if empty: do nothing and return null

// if there's only one item: is this a special case?
if(size==0){
thingToReturn= null;
}else {
thingToReturn = head.data;
Node current = new Node();
current.next = head;
head = current.next.next;
size--;
}

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

/**
Expand All @@ -77,17 +104,18 @@ public ItemType removeFirst() {
*/
@Override
public ItemType removeLast() {
ItemType thingToReturn;
// 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
if(size==0){
thingToReturn= null;
}else {
Node current = head;
while (current.next!=null){
current = current.next;
}
thingToReturn = current.data;
}size --;

return null;
return thingToReturn;
}
}