In what scenarios is a queue necessary? What is the advantage of using this algorithm?
In what scenarios is a queue necessary? What is the advantage of using this algorithm?
As said by @RHERWOLF in your answer , queue is a data structure. What's the difference between a data structure and an algorithm?
Well, an algorithm is a finite set of steps to do a computation. A data structure composes algorithms (yes, in the plural) and also a place to store the current state of such structure.
Normally, an algorithm is only concerned with its current execution. He simply has to do that processing and quit. A data structure is already concerned with subsequent uses of it. Its status is variable.
For example, to answer what is commonly called a queue, you must implement a rule called FIFO , first in, first out exit).
Only by reading this rule do you notice that the data structure "queue" has at least two operations: push elements into it, remove elements from inside. At no point in this "contract" is spoken of in queue size, then that would be your addition. You could even call it by another name, "measurable queue."
On queue operations, you usually find the following terms:
What is the best way to work with queues in Java?
So the best way to use a queue is when it fits your problem. It is common for you to already use the classes already coming from the default library because they are "good enough". But it is always possible to have your own solution for queues if you encounter any scenario that would make the system queues bad.
I do not even use a lot of queues at all, they're not usually in my world of trouble at work. Batteries are more common to me.
If you do not need this guarantee of order of arrival and departure, then you do not need a queue. In my work, the most common structure I use is the bag , it's like a set of math but may contain repeated elements.
In what scenarios does a queue be necessary?
When you need to store data and make subsequent redemption while maintaining the FIFO constraint.
Addendum : In which situations are they not needed?
LIFO
, "last in, first out" What is the advantage of using rows?
When you are dealing with a problem that requires queuing, solving it becomes easier.