Friday, July 17, 2009

Java Tutorial: Queues

Java Tutorial: Queues

The queue is another example of an ADT. It is a linearly ordered collection of elements which follow the discipline of “first-in, first-out”. Its applications include job scheduling in operating system, topological sorting and graph traversal.

Java Tutorial: Queues

Enqueue and dequeue are the operations associated with queues. Enqueue refers to inserting at the end of the queue whereas dequeue means removing front element of the queue. To remember how a queue works, think of the queue’s literal meaning – a line. Imagine yourself lining up to get the chance meeting your favorite star up close. If a new person would like to join the line, this person would have to go to the end of the line. Otherwise, there would probably be some fight. This is how enqueue works. Who gets to meet his/her favorite star first among those who are waiting in the line? It should have been the first person in the line. This person gets to leave the line first. Relate this to how dequeue works.

Java Tutorial: Queues

The queue is another example of an ADT. It is a linearly ordered collection of elements which follow the discipline of “first-in, first-out”. Its applications include job scheduling in operating system, topological sorting and graph traversal.

Enqueue and dequeue are the operations associated with queues. Enqueue refers to inserting at the end of the queue whereas dequeue means removing front element of the queue. To remember how a queue works, think of the queue’s literal meaning – a line. Imagine yourself lining up to get the chance meeting your favorite star up close. If a new person would like to join the line, this person would have to go to the end of the line. Otherwise, there would probably be some fight. This is how enqueue works. Who gets to meet his/her favorite star first among those who are waiting in the line? It should have been the first person in the line. This person gets to leave the line first. Relate this to how dequeue works.

Java Tutorial: Queues