I am studying this part of collections in Java, but I'm still confused about when I should use them? What are some valid use cases where they are the best option to use?
-
1. What do you mean "instad of data structures"? They are a way to structure data. 2. "Why do i use them" <- How should we know that? If you have no use for them then don't use them. – OH GOD SPIDERS Jul 06 '17 at 10:31
-
I am voting down this question as off topic as it is not related to any kind of programming where the user is asking to find him a tutorial – Madhusudan chowdary Jul 06 '17 at 10:31
-
Your question is not clear. But it is also too general; an answer would be a textbook chapter. What reference(s) were you told to use? What is an exact point in a reference that you are stuck on? – philipxy Jul 10 '17 at 19:37
-
Similar question with a more narrow scope: https://stackoverflow.com/questions/2074970/stack-and-queue-why – James Drinkard Aug 19 '22 at 15:25
3 Answers
You've been to a cafeteria, right? and seen a stack of plates? When a clean plate is added to the stack, it is put on top. When a plate is removed, it is removed from the top. So it is called Last-In-First-Out (LIFO). A computer stack is like that, except it holds numbers, and you can make one out of an array or a list, if you like. (If the stack of plates has a spring underneath, they say you "push" one onto the top, and when you remove one you "pop" it off. That's where those words come from.)
In the cafeteria, go in back, where they wash dishes. They have a conveyor-belt where they put plates to be washed in one end, and they come out the other end, in the same order. That's a queue or First-In-First-Out (FIFO). You can also make one of those out of an array or a list if you like.
What are they good for? Well, suppose you have a tree data structure (which is like a real tree made of wood except it is upside down), and you want to write a program to walk completely through it, so as to print out all the leaves.
One way is to do a depth-first walk. You start at the trunk and go to the first branch, and then go to the first branch of that branch, and so on, until you get to a leaf, and print it. But how do you back up to get to the next branch? Well, every time you go down a branch, you "push" some information in your stack, and when you back up you "pop" it back out, and that tells you which branch to take next. That's how you keep track of which branch to do next at each point.
Another way is a breadth-first walk. Starting from the trunk, you number all the branches off the trunk, and put those numbers in the queue. Then you take a number out the other end, go to that branch, and for every branch coming off of it, you again number them (consecutively with the first) and put those in the queue. As you keep doing this you are going to visit first the branches that are 1 branch away from the trunk. Then you are going to visit all the branches that are 2 branches away from the trunk, and so on. Eventually you will get to the leaves and you can print them.
These are two very basic concepts in programming. [Copied]

- 101
- 6
Queues and Stacks can be used when you need to work with data in a first-in-first-out / last-in-first-out (respectively) order and you want to be able discard every item you polled out of the queue / popped out of the stack after processing it.
Of course you can also use an ArrayList, but then you will have to maintain it and delete every item you processed it yourself
Example of logic questions you can solve with these structures:
- Queue - Level order traversal on a tree ( see: http://www.geeksforgeeks.org/print-level-order-traversal-line-line/)
- Stack - Post order traversal on a tree ( see: http://www.geeksforgeeks.org/iterative-postorder-traversal-using-stack/)
- Stack - check if an expression is balanced - {[()]}: Java balanced expressions check {[()]}
Also see this nice answer here: https://stackoverflow.com/a/17436821/2940903

- 53
- 3
- 5

- 414
- 2
- 11
Applications of stacks and Queues:
Stacks (
Compiler
designers like checking balanced parenthesis,)Queues - In serving the
HTTP
requests likeFIFO

- 2,747
- 2
- 28
- 33

- 9
- 3