What are practical applications of Queues in Computer Science. Where do we use them and why? I heard that we use them in Video Games and Computer Simulation programs, is that true? Why? Apart from these two areas what are other practical applications of Queues as a data structure?
All kinds of systems, where requests / jobs / clients are processed by one or more handlers: the incoming items are stored in a queue and when a handler is free, it pops a new item from the queue and starts processing it. This pattern is used in all web servers, among others. See producer-consumer problem.
I use a queue to prioritise my replying to SO questions. I tried multiprocessing them, but made a complete hash of it.
Queues are used for any situation where you want to efficiently maintain a First-in-first out order on some entities. These situations arise literally in every type of software development.
Imagine you have a web-site which serves files to thousands of users. You cannot service all requests, you can only handle say 100 at once. A fair policy would be first-come-first serve: serve 100 at a time in order of arrival. A Queue would definitely be the most appropriate data structure.
Similarly in a multitasking operating system, the CPU cannot run all jobs at once, so jobs must be batched up and then scheduled according to some policy. Again, a queue might be a suitable option in this case.
Say you have a number of documents to be printed at once. Your OS puts all of these docs in a queue and sends them to the printer. The printer takes and prints each document in the order the docs are put in the queue, ie, First In, First Out.
In the situation where there are multiple users or a networked computer system, you probably share a printer with other users. When you request to print a file, your request is added to the print queue. When your request reaches the front of the print queue, your file is printed. This ensures that only one person at a time has access to the printer and that this access is given on a first-come, first-served basis.
in computer science queues are mainly used in routers where the incoming packets are kept according to their arrival or sometimes even priority.
Algorithms and data-structures are strongly tight together
So , usually using a Stack depends on the Algorithm which will manipulate the Stack Enqueuing and Dequeuing
Which in turn . depends on the application
for example , if you are making an appreciation , which will accept input from multiple users , and you have to server them on a "First Come First Service" basis .. which mean your app will serve first request First
instead of every time you check the time stamp of each request , and see who is the oldest
you should Enqueue each incoming request to a stack
and you have only to Dequeue the Stack every time to see the next request
In a breadth-first ("shallowest"-first) search of a graph, you would use a queue to store nodes as you discover them.