Queues

Queues are opposite of how stacks operate, although they can both be implemented using either array or a linked list. Once again, they are pretty similar to what queues are in the real world:

queue

How does the Queue work?

How do you add a new item to the Queue?

You append one to the queue’s end. If you refer to the image, a new person always gets behind the last one in the queue.

How do you remove an item from the queue?

You remove the first one in the queue. In the real world, a person is removed from the queue after you service the first one standing at the counter!

Rules of the Queue

The queue has two operations as well:

  1. Add an element: You always add a new element at the end of the queue.
  2. Remove an element: You always remove an element from the beginning of the queue.

Because of this behavior, a Queue is often called a FIFO (First In First Out) data structure. The element which enters the queue first is serviced first. It is also referred to as the first come first served data structure.

Usage

Queue is one of the most useful data strutures. You want to process a number of elements. You place them one after another and send them into the function or program which can process them one after another. In this case (and such cases are pretty common in computing) you are placing elements in a queue and removing them one after another. Most the time, an array acts as the queue but it can also be a linked list.

Updated: