From f14733985c79a837c3950f8692bcc952ef03b970 Mon Sep 17 00:00:00 2001 From: Paul W Date: Thu, 9 Feb 2023 12:16:52 -0800 Subject: [PATCH] Added four required methods plus toString() for debuggin/output to both SinglyLinkedDeque and ResizingArrayDeque. --- .idea/misc.xml | 2 +- .idea/vcs.xml | 6 ++ src/Main.java | 194 +++++++++++++++++++++++++++++++++++- src/ResizingArrayDeque.java | 78 ++++++++++----- src/SinglyLinkedDeque.java | 116 +++++++++++++++++---- 5 files changed, 348 insertions(+), 48 deletions(-) create mode 100644 .idea/vcs.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 07115cd..05b1176 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index c502941..9247314 100644 --- a/src/Main.java +++ b/src/Main.java @@ -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 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 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(); + } } \ No newline at end of file diff --git a/src/ResizingArrayDeque.java b/src/ResizingArrayDeque.java index d1bca63..4fdf615 100644 --- a/src/ResizingArrayDeque.java +++ b/src/ResizingArrayDeque.java @@ -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 implements Deque { // constants public static int DEFAULT_CAPACITY = 10; @@ -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; } /** @@ -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; } /** @@ -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; } /** @@ -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 @@ -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; + } } diff --git a/src/SinglyLinkedDeque.java b/src/SinglyLinkedDeque.java index 5440edf..1cbe01f 100644 --- a/src/SinglyLinkedDeque.java +++ b/src/SinglyLinkedDeque.java @@ -1,3 +1,9 @@ +/** + * 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 SinglyLinkedDeque implements Deque { // private helper classes private class Node { @@ -27,6 +33,23 @@ public int size() { return size; } + @Override + public String toString() { + + if (head == null) + return ""; + + String s = ""; + Node n = head; + + while (n != null) { + s += n.data + " "; + n = n.next; + } + + return s; + } + /** * Adds an item to the front of the deque. * @@ -34,8 +57,21 @@ 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 + + // Create our new node + Node n = new Node(); + n.data = item; + + if (head == null) { + // assign head to point to new node + head = n; + size = 1; + } else { + // insert new node at head of list + n.next = head; + head = n; + size++; + } } /** @@ -45,8 +81,26 @@ 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 + + // create new node + Node n = new Node(); + n.data = item; + + if (head == null) { + // list is empty, assign new node at head of list + head = n; + size = 1; + } else { + // go to last node + Node h = head; + while (h.next != null) { + h = h.next; + } + + // add new node at end + h.next = n; // assign null h.next at last node to equal new node n + size++; + } } /** @@ -56,18 +110,20 @@ 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 head is null / list is empty, return null + if (head == null) { + return null; + } else { + // save 1st item + ItemType item = head.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 + // re-assign head to 2nd item, and decrement size + head = head.next; + size--; - return null; + return item; + } } /** @@ -77,17 +133,33 @@ 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? + // if empty list, return null + if (head == null) { + return null; + } else if (size == 1) { + // if size = 1, return that item, set list to null + ItemType item = head.data; + head = null; + --size; + return item; + } else { + // list has > 1 node, test using node.next.next .. + + Node h = head; + + // want to maintain reference to new/next-to last node so we can 'remove' the last one + while (h.next.next != null) { + h = h.next; + } + + + ItemType item = h.next.data; + h.next = 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; + } } }