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
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.

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

System.out.println();
System.out.println("-------------------------- Testing ResizingArrayDeque --------------------------");
System.out.println();
Deque<String> d1 = new ResizingArrayDeque<>();
// some test code here

// Testing addFirst() on empty/non-empty list - I implemented toString() to ease this ...
d1.addFirst("a");
d1.addFirst("b");
d1.addFirst("c");
d1.addFirst("d");
d1.addFirst("e");
d1.addFirst("f");
d1.addFirst("g");
d1.addFirst("h");
d1.addFirst("i");
d1.addFirst("j");
d1.addFirst("k");
d1.addFirst("l");

// should print in reverse order of addition ...
System.out.println("Testing addFirst on empty/non-empty, a..l, should print in reverse order");
System.out.println(d1);

/// test addLast on non-empty list
d1.addLast("m");
d1.addLast("n");
d1.addLast("o");
d1.addLast("p");
d1.addLast("q");
d1.addLast("r");
d1.addLast("s");
d1.addLast("t");
d1.addLast("u");
d1.addLast("v");

// should append these letters to prior in added order
System.out.println();
System.out.println("Testing addLast() on non-empty list, m..v, should display in order added");
System.out.println(d1);

d1 = new ResizingArrayDeque<>();

// testing addLast on empty list
d1.addLast("m");
d1.addLast("n");
d1.addLast("o");
d1.addLast("p");
d1.addLast("q");
d1.addLast("r");
d1.addLast("s");
d1.addLast("t");
d1.addLast("u");
d1.addLast("v");

System.out.println();
System.out.println("Testing addLast() on empty list, should display m..v in order");
System.out.println(d1);

System.out.println("Removing / printing 1st item in list");
System.out.println(d1.removeFirst());
System.out.println("Removing / printing 1st item in list");
System.out.println(d1.removeFirst());
System.out.println("Resulting list");
System.out.println(d1);

d1 = new ResizingArrayDeque<>();
System.out.println("Removing / printing item from empty list");
System.out.println(d1.removeFirst());
d1.addLast("a");
System.out.println("Removing / printing item from list that contains just 'a'");
System.out.println(d1.removeFirst());
System.out.println("Resulting list (should be empty/blank)");
System.out.println(d1);
System.out.println("d1.size: " + d1.size());

d1.addLast("a");
d1.addLast("b");
d1.addLast("c");
d1.addLast("d");
d1.addLast("e");

System.out.println();
System.out.println("Recreated list, added letters a..e");
System.out.println(d1);
System.out.println();

System.out.println("Testing removeLast(), should remove in reverse order, i.e. (e, d, c, b, a)");
System.out.println(d1.removeLast());
System.out.println(d1.removeLast());
System.out.println(d1.removeLast());
System.out.println(d1.removeLast());
System.out.println(d1.removeLast());
System.out.println("Resulting list - should be empty/blank");
System.out.println(d1);
System.out.println("d1.size: " + d1.size());
System.out.println();

System.out.println();
System.out.println();
System.out.println("-------------------------- Testing SinglyLinkedDeque --------------------------");
System.out.println();
Deque<String> d2 = new SinglyLinkedDeque<>();
// some test code here

// Testing addFirst() on empty/non-empty list - I implemented toString() to ease this ...
d2.addFirst("a");
d2.addFirst("b");
d2.addFirst("c");
d2.addFirst("d");
d2.addFirst("e");
d2.addFirst("f");
d2.addFirst("g");
d2.addFirst("h");
d2.addFirst("i");
d2.addFirst("j");
d2.addFirst("k");
d2.addFirst("l");

// should print in reverse order of addition ...
System.out.println("Testing addFirst on empty/non-empty, a..l, should print in reverse order");
System.out.println(d2);

/// test addLast on non-empty list
d2.addLast("m");
d2.addLast("n");
d2.addLast("o");
d2.addLast("p");
d2.addLast("q");
d2.addLast("r");
d2.addLast("s");
d2.addLast("t");
d2.addLast("u");
d2.addLast("v");

// should append these letters to prior in added order
System.out.println();
System.out.println("Testing addLast() on non-empty list, m..v, should display in order added");
System.out.println(d2);

// start with an empty list
d2 = new SinglyLinkedDeque<>();

// testing addLast on empty list
d2.addLast("m");
d2.addLast("n");
d2.addLast("o");
d2.addLast("p");
d2.addLast("q");
d2.addLast("r");
d2.addLast("s");
d2.addLast("t");
d2.addLast("u");
d2.addLast("v");

System.out.println();
System.out.println("Testing addLast() on empty list, should display m..v in order");
System.out.println(d2);

System.out.println("Removing / printing 1st item in list");
System.out.println(d2.removeFirst());
System.out.println("Removing / printing 1st item in list");
System.out.println(d2.removeFirst());
System.out.println("Resulting list");
System.out.println(d2);

d2 = new SinglyLinkedDeque<>();
System.out.println("Removing / printing item from empty list");
System.out.println(d2.removeFirst());
d2.addLast("a");
System.out.println("Removing / printing item from list that contains just 'a'");
System.out.println(d2.removeFirst());
System.out.println("Resulting list (should be empty/blank)");
System.out.println(d2);
System.out.println("d2.size: " + d2.size());

d2.addLast("a");
d2.addLast("b");
d2.addLast("c");
d2.addLast("d");
d2.addLast("e");

System.out.println();
System.out.println("Recreated list, added letters a..e");
System.out.println(d2);
System.out.println();

System.out.println("Testing removeLast(), should remove in reverse order, i.e. (e, d, c, b, a)");
System.out.println(d2.removeLast());
System.out.println(d2.removeLast());
System.out.println(d2.removeLast());
System.out.println(d2.removeLast());
System.out.println(d2.removeLast());
System.out.println("Resulting list - should be empty/blank");
System.out.println(d2);
System.out.println("d2.size: " + d2.size());
System.out.println();

}
}
78 changes: 54 additions & 24 deletions src/ResizingArrayDeque.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import java.util.Arrays;

/**
* This class was created by Ken Hang, four methods addFirst, addLast, removeFirst,
* and removeLast were implemented by Paul with interfaces by Ken.
*
* @author Paul Woods, Ken Hang
*/
public class ResizingArrayDeque<ItemType> implements Deque<ItemType> {
// constants
public static int DEFAULT_CAPACITY = 10;
Expand Down Expand Up @@ -31,11 +39,20 @@ 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
// Properties/Methods: DEFAULT_CAPACITY = 10, ItemType[] data, int size, checkSize()

// 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.
// Check size of underlying array, increase if necessary
checkSize();

// reposition all elements one place up
for (int i = size; i > 0; i--) {
data[i] = data[i-1];
}

// assign new data to data[0], as that has been moved to data[1]
data[0] = item;

++size;
}

/**
Expand All @@ -45,11 +62,9 @@ public void addFirst(ItemType item) {
*/
@Override
public void addLast(ItemType item) {
// consider the case of adding to an empty list
// consider the case of adding to a non-empty list
checkSize();

// 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.
data[size++] = item;
}

/**
Expand All @@ -59,18 +74,22 @@ public void addLast(ItemType item) {
*/
@Override
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?
// non-empty, save 1st element, and shift all others down 1 place
ItemType item = data[0];
for (int i = 0; i < size - 1; i++) {
data[i] = data[i + 1];
}

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

return null;
// return 1st element that was saved
return item;
}

/**
Expand All @@ -80,18 +99,18 @@ public ItemType removeFirst() {
*/
@Override
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 item = data[size-1];
data[size-1] = null;

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

return null;
return item;
}

// helper method to check to see if the size has reached the capacity
Expand All @@ -116,4 +135,15 @@ private void checkSize() {
temp = null;
} // end of if (need to resize)
}

@Override
public String toString() {
String s = "";
for (int i = 0; i < size; i++) {
s += data[i];
if (i < size-1)
s += ", ";
}
return s;
}
}
Loading