educative.io

Implement Queue Using Stacks

Why doesnt it compile ?

import java.util.*;

class MyQueue {

    private final Stack stack1;

    private final Stack stack2;

    public MyQueue() {

        stack1 = new Stack();

        stack2 = new Stack();

    }

    public void push(int x) {

        while (!stack1.empty()) {

            stack2.push(stack1.pop());

        }

        stack1.push(x);

        while (!stack2.empty()) {

            stack1.push(stack2.pop());

        }

    }

    public int pop() {

        return stack1.empty() ? -1 : stack1.pop();

    }

    public int peek() {

        return stack1.empty() ? -1 : stack1.peek();

    }

    public boolean empty() {

        return stack1.empty();

    }

}

Stacktrace:

MyQueue.java:14: error: cannot find symbol
        while (!stack1.empty()) {
                      ^
  symbol:   method empty()
  location: variable stack1 of type Stack
MyQueue.java:18: error: cannot find symbol
        while (!stack2.empty()) {
                      ^
  symbol:   method empty()
  location: variable stack2 of type Stack
MyQueue.java:24: error: cannot find symbol
        return stack1.empty() ? -1 : stack1.pop();
                     ^
  symbol:   method empty()
  location: variable stack1 of type Stack
MyQueue.java:28: error: cannot find symbol
        return stack1.empty() ? -1 : stack1.peek();
                     ^
  symbol:   method empty()
  location: variable stack1 of type Stack
MyQueue.java:28: error: cannot find symbol
        return stack1.empty() ? -1 : stack1.peek();
                                           ^
  symbol:   method peek()
  location: variable stack1 of type Stack
MyQueue.java:32: error: cannot find symbol
        return stack1.empty();
                     ^
  symbol:   method empty()
  location: variable stack1 of type Stack
6 errors

Course: https://www.educative.io/collection/10370001/4651429556125696
Lesson: https://www.educative.io/collection/page/10370001/4651429556125696/5184292218732544

1 Like

Hi @Prabhat_Jha,

The reason behind your code not getting compiled is because you are trying to call methods empty() and peek() that do not exist in the Stack class. To correct the given code, you need to fix the function calling through the Stack objects, i.e., instead of empty() there’s an isEmpty() method and similarly, instead of peek() you will call top() from the Stack class.

Here’s the updated code:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MyQueue {
    private final Stack stack1;
    private final Stack stack2;

    public MyQueue() {
        stack1 = new Stack();
        stack2 = new Stack();
    }

    public void push(int x) {
        while (!stack1.isEmpty()) {
            stack2.push(stack1.pop());
        }

        stack1.push(x);
        while (!stack2.isEmpty()) {
            stack1.push(stack2.pop());
        }
    }

    public int pop() {
        return stack1.isEmpty() ? -1 : stack1.pop();
    }

    public int peek() {
        return stack1.isEmpty() ? -1 : stack1.top();
    }

    public boolean empty() {
        return stack1.isEmpty();
    }
}

I hope this helps in figuring out what the issue was. Feel free to share more feedback and suggestions. We’d be happy to help. Thanks!

Happy learning!

1 Like

thanks, my mistake. May be better if we change the top and isEmpty to peek and empty. That will match the statement of the problem Implement Queue Using Stacks

1 Like