Queue
A Queue is a linear data structure that follows the FIFO (First In First Out)
principle.
The element inserted first is removed first.
Example:
People standing in a line at a ticket counter.
Basic Terms
• Front – Points to the first element of the queue
• Rear – Points to the last element of the queue
Operations on Queue
[Link] (Insertion)
Adds an element at the rear end of the queue.
Condition: Queue should not be full.
If rear == MAX − 1
Print “Queue Overflow” and Stop
If front == −1
Set front = 0
Set rear = rear + 1
Insert item into queue[rear]
[Link] (Deletion)
Removes an element from the front end of the queue.
Condition: Queue should not be empty.
If front == −1 OR front > rear
Print “Queue Underflow” and Stop
Store queue[front] in item
Set front = front + 1
If front > rear
Set front = rear = −1
3. Peek / Front Element
• Returns the element at the front without removing it.
If front == −1 OR front > rear
Print “Queue is Empty” and Stop
Return queue[front]
4. isEmpty
• Checks whether the queue is empty.
If front == −1 OR front > rear
Return TRUE
Else
Return FALSE
5. isFull
• Checks whether the queue is full.
If rear == MAX − 1
Return TRUE
Else
Return FALSE
Advantages
• Simple structure
• Efficient insertion and deletion
• Useful in scheduling and buffering
• Queues are useful when a particular service is used by multiple
consumers.
• Queues are fast in speed for data inter-process communication.
Disadvantages
• Wastage of memory in array implementation
• Fixed size (cannot grow dynamically)
Applications of Queue
• Shared Resource: Queues are widely used as waiting lists for a
single shared resource like printer, disk, CPU.
• Network: Queues are used in asynchronous transfer of data
(where data is not being transferred at the same rate between two
processes) for eg. pipes, file IO, sockets.
• Queues are used as buffers in most of the applications like MP3
media player, CD player, etc.
• Queue are used to maintain the play list in media players in order
to add and remove the songs from the play-list.
• Interrupt Handling: Queues are used in operating systems for
handling interrupts.
• Used in Breadth First Search (BFS)
• Ticket reservation systems