0

For some strange reason, whenever I print the size of the reversed stack (for testing purposes), I keep on getting 7. Why is that the case? Shouldn't the reversed stack be empty since nothing was pushed to it? Thanks!

public class QueueViaStacks {

    static Stack original;
    static Stack reversed;

    // push everything into the first stack
    public void enqueue(Object item) {

        // create stack if it is not yet created
        if (original == null)
            original = new Stack();

        original.push(item);

    }

    public Object dequeue() {

        if (reversed == null)
            reversed = new Stack();

        System.out.println(reversed.size);

        Object temp;

        // reverse stack by copying everything from first stack to second stack
        if (reversed.isEmpty()) {
            while (original != null) {
                temp = original.pop();
                reversed.push(temp);
            }
        }

        return reversed.pop();

    }

    public int size() {

        return original.getSize() + reversed.getSize();

    }

    public static void main(String[] args) {

        QueueViaStacks stack = new QueueViaStacks();

        stack.enqueue('T');
        stack.enqueue('E');
        stack.enqueue('S');
        stack.enqueue('T');
        stack.enqueue('I');
        stack.enqueue('N');
        stack.enqueue('G');

        stack.dequeue();

    }

}

Below is my Stack class:

public class Stack {

static Node top;
static int size = 0;

public void push(Object item) {

    Node t = new Node(item);
    t.next = top;
    top = t; 
    size++;

}

public Object pop() {

    while (top != null) {
        Object temp = top.item;
        top = top.next;
        size--;
        return temp;
    }
    return null;

}

public int getSize() {

    return size;

}

public boolean isEmpty() {

    if (size == 0)
        return true;
    return false;

}

public void print(Stack stack) {

    Node temp = top;
    while (temp != null) {
        System.out.println(temp.item);
        temp = temp.next;
    }

}

}

4
  • 1
    We can't answer that without seeing Stack (and how size is initialized). Commented May 17, 2016 at 21:09
  • Are you using java.util.Stack? Commented May 17, 2016 at 21:16
  • In any case, although I don't see anything that explains the size issue you described, I do see some serious deficiencies in both enqueue() and dequeue(). And your code does not even compile if Stack is supposed to be java.util.Stack. Commented May 17, 2016 at 21:25
  • I implemented my own stack class, which I added just now. Also, thanks John for pointing that out. Commented May 18, 2016 at 3:46

1 Answer 1

1

I tried your code by using java.util.Stack and it worked fine with java.util.Stack. Also, you are accessing size variable of Stack directly from dequeue method(which is not possible if it were java.util.Stack) and using getSize() to get the size of the stacks (for java.util.Stack you have to use size() method instead).With these evidences, I assume you have implemented your own Stack class. Most probably the error is from the implementation of your Stack class. Please do have a look into your Stack class. If you want more specific answer you should specify if you have implemented your own Stack class or not. If yes you should provide your implementation of Stack and other related classes as well.

P.S: Your dequeue method does not work properly. Please follow the suggestion of Alin Gabriel. If you do not have isEmpty() method in your stack you can use original.getSize()>0 instead of !original.isEmpty()

Sign up to request clarification or add additional context in comments.

1 Comment

You're correct that I implemented my own Stack, and that is why I got the error. I switched over to java.util.Stack and it is fine now. Thanks for pointing that out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.