views:

354

answers:

7

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?

+2  A: 

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.

Péter Török
+2  A: 

I use a queue to prioritise my replying to SO questions. I tried multiprocessing them, but made a complete hash of it.

High Performance Mark
@High-Performance Mark Maybe with two monitors you could use your two processors (aka brain hemispheres) to process two posts in parallel... but I/O would still suck, for various reasons :-)
Péter Török
@Péter Török: how did you know I'd had a lobotomy ?
High Performance Mark
+2  A: 

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.

Il-Bhima
+1  A: 

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.

Zaki
A: 

in computer science queues are mainly used in routers where the incoming packets are kept according to their arrival or sometimes even priority.

CadetNumber1
Message passing, message processing, request servicing (web, router, or complex application), command processing, or any other sequence dependent operation is a candidate for the use of a queue. This is a core algorithm in CS because its used (in one form or another) in many aspects of programming, not just hardware routers.
Jason D
@Jason D i never said they are only used for routers.i said they are used in them.
CadetNumber1
Ashish, you said "queues are mainly used in routers". I take that to mean "predominantly" or "to the extent that anything else is marginal". So the criticism from Jason D is quite valid. Also, don't bother trotting out "I am not a native speaker of English", neither am I.
Vatine
mainly doesn't mean fully...
CadetNumber1
Mainly does not mean fully, but it means predominantly.
Vatine
+2  A: 

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

Radian
A: 

In a breadth-first ("shallowest"-first) search of a graph, you would use a queue to store nodes as you discover them.

James M.