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..85c3907 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter17/practice/MakeQueueWithOneStack.java @@ -0,0 +1,33 @@ +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..180cb31 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter17/practice/MakeQueueWithTwoStacks.java @@ -0,0 +1,33 @@ +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<>(); + } +} 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..d520f07 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter17/solutions/MakeQueueWithOneStack.java @@ -0,0 +1,69 @@ +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..0f9bf01 --- /dev/null +++ b/src/com/codefortomorrow/advanced/chapter17/solutions/MakeQueueWithTwoStacks.java @@ -0,0 +1,68 @@ +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"); + } +}