Queues use FIFO (First In First Out) principle, which means the first one to queue gets to leave the queue first. The act of queueing in data structures is called enqueue while the act of leaving the queue is called dequeue. Adding elements in a queue takes place in the rear while removing elements take place in the front.
Common operations in queues are:
-
Enqueue: adding elements to the end of the queue
-
Dequeue: removing elements from the front of the queue
-
IsEmpty: which checks if the queue is empty,
-
IsFull: checks if the queue is full,
-
Peek: gets the value of the first item in the queue without removing it
Types of queues
-Simple queue: items are added in the rear and removed in the front
-Circular queue: the last element points to the first element hence making it a circular link. E.g if in the bus terminal the bus is full, the person left behind becomes the first person and the cycle continues.
-Priority queue: items are given priorities and served according to their level of priority. e.g in airports where some passengers get shorter lines and faster service
-Double-ended queue: items can be added and removed from either front or rear
Queue Application
-
CPU scheduling which is determining which process will own the CPU while the other holds.
-
Data synchronization across devices to update changes automatically
-
Handling of interruptions in real-time systems
-
Call support centers to put the callers on hold and serve them as per the queue