diff options
author | RossTheRoss <mstrapp@protonmail.com> | 2020-01-30 16:55:04 -0600 |
---|---|---|
committer | RossTheRoss <mstrapp@protonmail.com> | 2020-01-30 16:55:04 -0600 |
commit | 175721a63b426355274fa9e8063f762020ab8362 (patch) | |
tree | cf2c1b33233d660c9f50de5e659b9343bb264984 /OLD/csci1913/Java/lab11_strap012.java | |
parent | Make Python thing in Python (diff) | |
download | homework-175721a63b426355274fa9e8063f762020ab8362.tar homework-175721a63b426355274fa9e8063f762020ab8362.tar.gz homework-175721a63b426355274fa9e8063f762020ab8362.tar.bz2 homework-175721a63b426355274fa9e8063f762020ab8362.tar.lz homework-175721a63b426355274fa9e8063f762020ab8362.tar.xz homework-175721a63b426355274fa9e8063f762020ab8362.tar.zst homework-175721a63b426355274fa9e8063f762020ab8362.zip |
R E A R R A N G E
Diffstat (limited to 'OLD/csci1913/Java/lab11_strap012.java')
-rw-r--r-- | OLD/csci1913/Java/lab11_strap012.java | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/OLD/csci1913/Java/lab11_strap012.java b/OLD/csci1913/Java/lab11_strap012.java new file mode 100644 index 0000000..7eb146a --- /dev/null +++ b/OLD/csci1913/Java/lab11_strap012.java @@ -0,0 +1,115 @@ +class Deque<Base> { + private Node head; + private class Node { + private Base object; + private Node left; + private Node right; + private Node(Base o, Node l, Node r) { + object = o; + left = l; right = r; + } + } + public Deque() { + head = new Node(null, null, null); + head.left=head; head.right=head; + } + public void enqueueRear(Base object) { + Node tempL = head; + Node newNode = new Node(object, tempL.left, tempL); + tempL.left.right = newNode; + tempL.left = newNode; + } + public void enqueueFront(Base object) { + Node tempR = head; + Node newNode = new Node(object, tempR, tempR.right); + tempR.right.left = newNode; + tempR.right = newNode; + } + public Base dequeueRear() { + if (isEmpty()) { + throw new IllegalStateException(); + } else { + Node temp = head.left; + temp.left.right = head; + head.left = temp.left; + return temp.object; + } + } + public Base dequeueFront() { + if (isEmpty()) { + throw new IllegalStateException(); + } else { + Node temp = head.right; + temp.right.left = head; + head.right = temp.right; + return temp.object; + } + } + public Boolean isEmpty() { + return head.right==head && head.left==head; + } +} + +// OBSERVATION DEQUE. Test the class DEQUE. 40 points total. +class ObservationDeque{ +// MAIN. Test the DEQUE on various example arguments. + public static void main(String [] args) { + Deque<String> deque = new Deque<String>(); + System.out.println(deque.isEmpty()); // true 2 points. + try { + System.out.println(deque.dequeueFront()); + } catch (IllegalStateException ignore) { + System.out.println("No dequeueFront."); // No dequeueFront. 2 points. + } + try { System.out.println(deque.dequeueRear()); + } catch (IllegalStateException ignore) { + System.out.println("No dequeueRear."); // No dequeueRear. 2 points. + } +//Enqueueing to the rear and dequeueing from the rear makes the DEQUE act +// like a stack. + deque.enqueueRear("A"); + deque.enqueueRear("B"); + deque.enqueueRear("C"); + + System.out.println(deque.isEmpty()); // false 2 points. + + System.out.println(deque.dequeueRear()); // C 2 points. + System.out.println(deque.dequeueRear()); // B 2 points. + System.out.println(deque.dequeueRear()); // A 2 points. + System.out.println(deque.isEmpty()); // true 2 points. +// Enqueueing to the rear and dequeueing from the front makes the DEQUE act +// like a queue. + deque.enqueueRear("A"); + deque.enqueueRear("B"); + deque.enqueueRear("C"); + + System.out.println(deque.dequeueFront()); // A 2 points. + System.out.println(deque.dequeueFront()); // B 2 points. + System.out.println(deque.dequeueFront()); // C 2 points. + + System.out.println(deque.isEmpty()); // true 2 points. +//Enqueueing to the front and dequeueing from the front makes the DEQUE act +//like a stack. + deque.enqueueFront("A"); + deque.enqueueFront("B"); + deque.enqueueFront("C"); + + System.out.println(deque.dequeueFront()); // C 2 points. + System.out.println(deque.dequeueFront()); // B 2 points. + System.out.println(deque.dequeueFront()); // A 2 points. + + System.out.println(deque.isEmpty()); // true 2 points. + +// Enqueueing to the front and dequeueing from the rear makes the DEQUE act +// like a queue. + deque.enqueueFront("A"); + deque.enqueueFront("B"); + deque.enqueueFront("C"); + + System.out.println(deque.dequeueRear()); // A 2 points. + System.out.println(deque.dequeueRear()); // B 2 points. + System.out.println(deque.dequeueRear()); // C 2 points. + + System.out.println(deque.isEmpty()); // true 2 points. + } +}
\ No newline at end of file |