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

Chapter 17 Problems #21

wants to merge 3 commits into from

Conversation

Krishna-Saxena
Copy link

Full Name of Contributor (if not apparent from GitHub profile)

Make Queue with 2 Stacks

  • Chapter # 17
  • Stacks and Queues

Make Queue with 1 Stack

  • Chapter # 17
  • Stacks and Queues

/*
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).

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.

}

public E remove() throws NoSuchElementException {
if (empty())

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.

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

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.

while (!stack1.empty())
stack2.push(stack1.pop());
return stack2.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 🐶
'}' at column 9 should be on the same line as the next part of a multi-block statement (one that directly contains multiple blocks: if/else-if/else, do/while or try/catch/finally).

}

public E remove() throws NoSuchElementException {
if (empty())

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.

}

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

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.

System.out.println(a); // 4
try {
a = myQueue.remove();
}

Choose a reason for hiding this comment

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

[reviewdog] reported by reviewdog 🐶
'}' at column 9 should be on the same line as the next part of a multi-block statement (one that directly contains multiple blocks: if/else-if/else, do/while or try/catch/finally).


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.


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

if (empty()) throw new NoSuchElementException(
"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.

}

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.

}

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.

@phrdang phrdang requested a review from AlveeraMunshi July 1, 2021 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants