Stack and Queue display conventions
-
06-03-2021 - |
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
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow