Question

What are the conventions to display a Stack and a Queue on a console screen? For Example, if I am in the following situation:

    Stack<String> s = new Stack<String>();
    s.push("Hello");
    s.push("there");

So when I print the Stack, should Hello come before there or vice versa?

Same goes in the case of a Queue.

Keep in mind I'm working on a console screen and not an applet window

Was it helpful?

Solution

There are no strong conventions on how to write a stack to the screen. The only convention is that the last item pushed to a stack is normally called the "top" of the stack. If you are writing the elements vertically, write the most recently pushed item at the top. (It should be said that even that convention is not universal).

For a queue there isn't even that convention. Do whatever seems like a good idea.

OTHER TIPS

Stacks are LIFO(Last In, First Out) and Queues are FIFO(First In, First Out).

In the case of a stack I think it is better to call s.pop() repeatedly and print them to the console.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top