-
-
Notifications
You must be signed in to change notification settings - Fork 5
Chapter 17 Problems #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<E> { | ||
|
||
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<Integer> myQueue = new MyQueueV2<>(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<E> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
|
||
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<Integer> myQueue = new MyQueue<>(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
the remove method either throws an exception, or recursively pops from the stack until the first element inserted | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
is isolated. After the first element is returned, the stack of Es is restored as the recursive call stack shrinks | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
*/ | ||
|
||
class MyQueueV2<E> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
|
||
Stack<E> stack; | ||
|
||
public MyQueueV2() { | ||
stack = new Stack<>(); | ||
} | ||
|
||
public void add(E e) { | ||
stack.push(e); | ||
} | ||
|
||
public E remove() throws NoSuchElementException { | ||
if (empty()) throw new NoSuchElementException( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
"no element in this queue left to remove" | ||
); | ||
return removeHelper(); | ||
} | ||
|
||
private E removeHelper() { | ||
if (stack.size() == 1) return stack.pop(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
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<Integer> 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"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
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<E> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
|
||
Stack<E> stack1; | ||
Stack<E> 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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
"no element in this queue left to remove" | ||
); else if (stack2.empty()) { | ||
while (!stack1.empty()) stack2.push(stack1.pop()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [reviewdog] reported by reviewdog 🐶 |
||
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<Integer> 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"); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[reviewdog] reported by reviewdog 🐶
Top-level class MyQueueV2 has to reside in its own source file.