Queue
A queue is an ordered collection of items from which items may be deleted at one end called the Front of the queue and into which items may be inserted at the other end called the Rear of the queue.
Queue Operations
The Queue is an Abstract Data Type (ADT) that allows performing the following operations:
- Enqueue: It is used to add an item to the rear of the queue
- De queue: It is used to remove an item from the front of the queue
- IsFull: Check if the queue is Full
- IsEmpty: Check if the queue is Empty
- Peek: Get the element at the front of the queue
The initial value of Front is 0 and Rear is -1.
ENQUEUE
1. Start
2. If (rear = = MaxSize -1)
Printf(“queue overflow”);
3. Else
If (front = = -1)
Front = 0;
Rear = rear + 1;
Queue [rear] =item;
4. Stop
DEQUEUE
1. Start
2. If (front = = -1)|| (front >rear)
Printf (“Queue under flow”);
Return;
3. Else
Printf (“Element detected from queue”, queue [front]);
front = front +1;
4. Stop
2. If (front = = -1)|| (front >rear)
Printf (“Queue under flow”);
Return;
3. Else
Printf (“Element detected from queue”, queue [front]);
front = front +1;
4. Stop
Working of Queue
Queue operations work as follows:
- Two pointers FRONT and REAR
- FRONT track the first element of the queue
- REAR track the last element of the queue
- Initially, set the value of FRONT and REAR to -1
Problems with Linear Queue
As items are removed from the queue, both front and rear indices are increased. The storage space at the beginning of the queue is discarded and never used again. Hence, the queue is considered full even though there is space at the beginning of the queue.
The complexity of en queue and dequeue operations in a queue using an array is O(1).
Applications of Queue
- CPU scheduling, Disk Scheduling
- When data is transferred asynchronously between two processes. The queue is used for synchronization. For example IO Buffers, pipes, file IO, etc
- Handling of interrupts in real-time systems.
- Call Center phone systems use Queues to hold people calling them in order.
Tags:
Circular Queue