Skip to content

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> {

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.


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> {

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 MyQueue has to reside in its own source file.


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,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
Line is longer than 100 characters (found 106).

the remove method either throws an exception, or recursively pops from the stack until the first element inserted

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
Line is longer than 100 characters (found 113).

is isolated. After the first element is returned, the stack of Es is restored as the recursive call stack shrinks

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
Line is longer than 100 characters (found 113).

*/

class MyQueueV2<E> {

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.


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(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
'if' construct must use '{}'s.

"no element in this queue left to remove"
);
return removeHelper();
}

private E removeHelper() {
if (stack.size() == 1) return stack.pop();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
'if' construct must use '{}'s.

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,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
Line is longer than 100 characters (found 102).

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> {

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 MyQueue has to reside in its own source file.


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(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
'if' construct must use '{}'s.

"no element in this queue left to remove"
); else if (stack2.empty()) {
while (!stack1.empty()) stack2.push(stack1.pop());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[reviewdog] reported by reviewdog 🐶
'while' construct must use '{}'s.

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");
}
}