Queue is the concept used in data structures. It is also referred to the linear data structure as the object or item can be added in one terminal and the item can be retrieved from the other end. Item, which is added to one end, is called as REAR and the item that is retrieved from the other end is called as FRONT. Queue is used in array as well as in linked list of the computer programs mainly used by the programmers. Queue follows the concept of FIFO (First-In-First-Out) . It means that the items, which are enters first will be accessed or retrieved first.
The best example to represent the queue is the print jobs. Consider the systems are connected in the network with the common print using the print share methodology. So, whenever the users give their documents for printing, the jobs will be stored in a queue and the job which first enters the print queue will be printed first, which complies the concept of queue FIFO.
The following are operations performed by queue in data structures
This operation is used to add an item to the queue at the rear end. So, the head of the queue will be now occupied with an item currently added in the queue. Head count will be incremented by one after addition of each item until the queue reaches the tail point. This operation will be performed at the rear end of the queue.
This operation is used to remove an item from the queue at the front end. Now the tail count will be decremented by one each time when an item is removed from the queue until the queue reaches the head point. This operation will be performed at the front end of the queue.
This operation is used to initialize the queue by representing the head and tail positions in the memory allocation table (MAT).
The other primitive operations of the queue are
Let us now consider the following example of using the operation Enqueue and Dequeue.
A queue of an array A is initialized and now the size of the queue is 3 which represents the queue can hold a maximum of 3 items. The enqueue operation is used to add an item to the queue
Enqueue (3) – This will add an item called 3 to the queue in the front end. (Front count will be incremented by 1)
Again we are adding another item to the queue
Enqueue (5) – This will add an item called 5 to the queue (Front count will be incremented by 1)
Again we are adding the last item to the queue
Enqueue (8) – This will add an item called 8 to the queue (Now the queue has reached its maximum count and hence no more addition of an item is possible in the queue, as the queue has reached the rear end)
Now dequeue operation is performed to remove an item from the queue.
Dequeue () – This will remove the item that has been added first in the queue, i.e., the item called 3 by following the concept of FIFO. Now the queue consists of the remaining items 5 and 8 in which the object 5 will be in the front end. The dequeue operation continues until the queue is reaching its last element.
In the computer environment, the below areas where the queue concept will be implemented:
More articles: Data Structure