From 40bb398e24734689c6d2b13d22ff3d647c3bac2d Mon Sep 17 00:00:00 2001 From: krishna-saxena Date: Sun, 8 Nov 2020 09:49:07 -0800 Subject: [PATCH 1/2] Chapter 17 Problems --- .../practice/MakeQueueWithOneStack.java | 34 +++++++++ .../practice/MakeQueueWithTwoStacks.java | 34 +++++++++ .../solutions/MakeQueueWithOneStack.java | 68 ++++++++++++++++++ .../solutions/MakeQueueWithTwoStacks.java | 69 +++++++++++++++++++ 4 files changed, 205 insertions(+) create mode 100644 src/com/codefortomorrow/advanced/chapter17/practice/MakeQueueWithOneStack.java create mode 100644 src/com/codefortomorrow/advanced/chapter17/practice/MakeQueueWithTwoStacks.java create mode 100644 src/com/codefortomorrow/advanced/chapter17/solutions/MakeQueueWithOneStack.java create mode 100644 src/com/codefortomorrow/advanced/chapter17/solutions/MakeQueueWithTwoStacks.java diff --git a/src/com/codefortomorrow/advanced/chapter17/practice/MakeQueueWithOneStack.java b/src/com/codefortomorrow/advanced/chapter17/practice/MakeQueueWithOneStack.java new file mode 100644 index 0000000..de7deb8 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter17/practice/MakeQueueWithOneStack.java @@ -0,0 +1,34 @@ +package com.codefortomorrow.advanced.chapter17.practice; + +/* +Create a Queue using one Stack "under the hood" +Try implementing MakeQueueWithTwoStacks first. +If you are stuck try thinking of a technique from Ch. 13 that creates a call "stack" + +Problem adapted from leetcode.com +*/ + +import java.util.NoSuchElementException; + +class MyQueueV2 { + + public MyQueueV2() { + } + + public void add(E e) { + } + + public E remove() throws NoSuchElementException { + return null; + } + + public boolean empty() { + return true; + } +} + +public class MakeQueueWithOneStack { + public static void main(String[] args) { + MyQueueV2 myQueue = new MyQueueV2<>(); + } +} diff --git a/src/com/codefortomorrow/advanced/chapter17/practice/MakeQueueWithTwoStacks.java b/src/com/codefortomorrow/advanced/chapter17/practice/MakeQueueWithTwoStacks.java new file mode 100644 index 0000000..1dadbcd --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter17/practice/MakeQueueWithTwoStacks.java @@ -0,0 +1,34 @@ +package com.codefortomorrow.advanced.chapter17.practice; + +/* +Create a Queue using one Stack "under the hood" +Try implementing MakeQueueWithTwoStacks first. +If you are stuck try thinking of a technique from Ch. 13 that creates a call "stack" + +Problem adapted from leetcode.com +*/ + +import java.util.NoSuchElementException; + +class MyQueue { + + public MyQueue() { + } + + public void add(E e) { + } + + public E remove() throws NoSuchElementException { + return null; + } + + public boolean empty() { + return true; + } +} + +public class MakeQueueWithTwoStacks { + public static void main(String[] args) { + MyQueue myQueue = new MyQueue<>(); + } +} \ No newline at end of file diff --git a/src/com/codefortomorrow/advanced/chapter17/solutions/MakeQueueWithOneStack.java b/src/com/codefortomorrow/advanced/chapter17/solutions/MakeQueueWithOneStack.java new file mode 100644 index 0000000..ebda119 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter17/solutions/MakeQueueWithOneStack.java @@ -0,0 +1,68 @@ +package com.codefortomorrow.advanced.chapter17.solutions; + +import java.util.NoSuchElementException; +import java.util.Stack; + +/* +This implementation of MyQueueV2 uses the first stack as LIFO order "storage" space. +In order to get the functionality of another stack, we will use recursion +When the user adds into the MyQueueV2, the operation is instant. When the user removes from the MyQueueV2, +the remove method either throws an exception, or recursively pops from the stack until the first element inserted +is isolated. After the first element is returned, the stack of Es is restored as the recursive call stack shrinks + */ + +class MyQueueV2 { + Stack stack; + + public MyQueueV2() { + stack = new Stack<>(); + } + + public void add(E e) { + stack.push(e); + } + + public E remove() throws NoSuchElementException { + if (empty()) + throw new NoSuchElementException("no element in this queue left to remove"); + return removeHelper(); + } + + private E removeHelper() { + if (stack.size() == 1) + return stack.pop(); + E e = stack.pop(); + E val = removeHelper(); + stack.push(e); + return val; + } + + public boolean empty() { + return stack.empty(); + } +} + +public class MakeQueueWithOneStack { + public static void main(String[] args) { + MyQueueV2 myQueue = new MyQueueV2<>(); + myQueue.add(1); + Integer a = myQueue.remove(); + System.out.println(a); // 1 + myQueue.add(2); + myQueue.add(3); + myQueue.add(4); + a = myQueue.remove(); + System.out.println(a); // 2 + a = myQueue.remove(); + System.out.println(a); // 3 + a = myQueue.remove(); + System.out.println(a); // 4 + try { + a = myQueue.remove(); + } + catch (NoSuchElementException e) { + e.printStackTrace(); + } + System.out.println("tests complete"); + } +} diff --git a/src/com/codefortomorrow/advanced/chapter17/solutions/MakeQueueWithTwoStacks.java b/src/com/codefortomorrow/advanced/chapter17/solutions/MakeQueueWithTwoStacks.java new file mode 100644 index 0000000..ca58ddd --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter17/solutions/MakeQueueWithTwoStacks.java @@ -0,0 +1,69 @@ +package com.codefortomorrow.advanced.chapter17.solutions; + +import java.util.NoSuchElementException; +import java.util.Stack; + +/* +This implementation of MyQueue uses the first stack as LIFO order "storage" space. +The second stack is is always in FIFO order. +When the user adds into the MyQueue, the operation is instant. When the user removes from the MyQueue, +3 things could occur: +1. there is nothing in the MyQueue: return null +2. stack2 is empty, so copy over everything from stack1: return the top of stack2 +3. return the top of stack2 + */ +class MyQueue { + Stack stack1; + Stack stack2; + + public MyQueue() { + stack1 = new Stack<>(); + stack2 = new Stack<>(); + } + + public void add(E e) { + stack1.push(e); + } + + public E remove() throws NoSuchElementException { + if (empty()) + throw new NoSuchElementException("no element in this queue left to remove"); + else if (stack2.empty()) { + while (!stack1.empty()) + stack2.push(stack1.pop()); + return stack2.pop(); + } + else { + return stack2.pop(); + } + } + + public boolean empty() { + return stack1.empty() && stack2.empty(); + } +} + +public class MakeQueueWithTwoStacks { + public static void main(String[] args) { + MyQueue myQueue = new MyQueue<>(); + myQueue.add(1); + Integer a = myQueue.remove(); + System.out.println(a); // 1 + myQueue.add(2); + myQueue.add(3); + myQueue.add(4); + a = myQueue.remove(); + System.out.println(a); // 2 + a = myQueue.remove(); + System.out.println(a); // 3 + a = myQueue.remove(); + System.out.println(a); // 4 + try { + a = myQueue.remove(); + } + catch (NoSuchElementException e) { + e.printStackTrace(); + } + System.out.println("tests complete"); + } +} \ No newline at end of file From 72329b4f293bb82c812dbb453ccf3439538407bd Mon Sep 17 00:00:00 2001 From: Krishna-Saxena Date: Wed, 25 Nov 2020 02:12:06 +0000 Subject: [PATCH 2/2] Bot: Prettified Java code! --- .../practice/MakeQueueWithOneStack.java | 7 +++---- .../practice/MakeQueueWithTwoStacks.java | 9 ++++----- .../solutions/MakeQueueWithOneStack.java | 13 +++++++------ .../solutions/MakeQueueWithTwoStacks.java | 19 +++++++++---------- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/com/codefortomorrow/advanced/chapter17/practice/MakeQueueWithOneStack.java b/src/com/codefortomorrow/advanced/chapter17/practice/MakeQueueWithOneStack.java index de7deb8..85c3907 100644 --- a/src/com/codefortomorrow/advanced/chapter17/practice/MakeQueueWithOneStack.java +++ b/src/com/codefortomorrow/advanced/chapter17/practice/MakeQueueWithOneStack.java @@ -12,11 +12,9 @@ class MyQueueV2 { - public MyQueueV2() { - } + public MyQueueV2() {} - public void add(E e) { - } + public void add(E e) {} public E remove() throws NoSuchElementException { return null; @@ -28,6 +26,7 @@ public boolean empty() { } public class MakeQueueWithOneStack { + public static void main(String[] args) { MyQueueV2 myQueue = new MyQueueV2<>(); } diff --git a/src/com/codefortomorrow/advanced/chapter17/practice/MakeQueueWithTwoStacks.java b/src/com/codefortomorrow/advanced/chapter17/practice/MakeQueueWithTwoStacks.java index 1dadbcd..180cb31 100644 --- a/src/com/codefortomorrow/advanced/chapter17/practice/MakeQueueWithTwoStacks.java +++ b/src/com/codefortomorrow/advanced/chapter17/practice/MakeQueueWithTwoStacks.java @@ -12,11 +12,9 @@ class MyQueue { - public MyQueue() { - } + public MyQueue() {} - public void add(E e) { - } + public void add(E e) {} public E remove() throws NoSuchElementException { return null; @@ -28,7 +26,8 @@ public boolean empty() { } public class MakeQueueWithTwoStacks { + public static void main(String[] args) { MyQueue myQueue = new MyQueue<>(); } -} \ No newline at end of file +} diff --git a/src/com/codefortomorrow/advanced/chapter17/solutions/MakeQueueWithOneStack.java b/src/com/codefortomorrow/advanced/chapter17/solutions/MakeQueueWithOneStack.java index ebda119..d520f07 100644 --- a/src/com/codefortomorrow/advanced/chapter17/solutions/MakeQueueWithOneStack.java +++ b/src/com/codefortomorrow/advanced/chapter17/solutions/MakeQueueWithOneStack.java @@ -12,6 +12,7 @@ */ class MyQueueV2 { + Stack stack; public MyQueueV2() { @@ -23,14 +24,14 @@ public void add(E e) { } public E remove() throws NoSuchElementException { - if (empty()) - throw new NoSuchElementException("no element in this queue left to remove"); + if (empty()) throw new NoSuchElementException( + "no element in this queue left to remove" + ); return removeHelper(); } private E removeHelper() { - if (stack.size() == 1) - return stack.pop(); + if (stack.size() == 1) return stack.pop(); E e = stack.pop(); E val = removeHelper(); stack.push(e); @@ -43,6 +44,7 @@ public boolean empty() { } public class MakeQueueWithOneStack { + public static void main(String[] args) { MyQueueV2 myQueue = new MyQueueV2<>(); myQueue.add(1); @@ -59,8 +61,7 @@ public static void main(String[] args) { System.out.println(a); // 4 try { a = myQueue.remove(); - } - catch (NoSuchElementException e) { + } catch (NoSuchElementException e) { e.printStackTrace(); } System.out.println("tests complete"); diff --git a/src/com/codefortomorrow/advanced/chapter17/solutions/MakeQueueWithTwoStacks.java b/src/com/codefortomorrow/advanced/chapter17/solutions/MakeQueueWithTwoStacks.java index ca58ddd..0f9bf01 100644 --- a/src/com/codefortomorrow/advanced/chapter17/solutions/MakeQueueWithTwoStacks.java +++ b/src/com/codefortomorrow/advanced/chapter17/solutions/MakeQueueWithTwoStacks.java @@ -13,6 +13,7 @@ 3. return the top of stack2 */ class MyQueue { + Stack stack1; Stack stack2; @@ -26,14 +27,12 @@ public void add(E e) { } public E remove() throws NoSuchElementException { - if (empty()) - throw new NoSuchElementException("no element in this queue left to remove"); - else if (stack2.empty()) { - while (!stack1.empty()) - stack2.push(stack1.pop()); + if (empty()) throw new NoSuchElementException( + "no element in this queue left to remove" + ); else if (stack2.empty()) { + while (!stack1.empty()) stack2.push(stack1.pop()); return stack2.pop(); - } - else { + } else { return stack2.pop(); } } @@ -44,6 +43,7 @@ public boolean empty() { } public class MakeQueueWithTwoStacks { + public static void main(String[] args) { MyQueue myQueue = new MyQueue<>(); myQueue.add(1); @@ -60,10 +60,9 @@ public static void main(String[] args) { System.out.println(a); // 4 try { a = myQueue.remove(); - } - catch (NoSuchElementException e) { + } catch (NoSuchElementException e) { e.printStackTrace(); } System.out.println("tests complete"); } -} \ No newline at end of file +}