A Queue is a linear data structure that works on the FIFO (First In, First Out) principle. This means the element inserted first is the one that gets removed first.
It contrasts with a Stack, which follows the LIFO (Last In, First Out) principle.
- Front → Position of the element to be removed (oldest element).
- Rear → Position where the next element will be inserted.
- Enqueue (Insertion) → Adds an element at the rear.
- Dequeue (Deletion) → Removes an element from the front.
- Display → Shows all elements in the queue.
- Sequential Processing → Elements are handled in the order they arrive.
- Restricted Operations → Insertions only at the rear, deletions only from the front.
- Overflow → Occurs when the queue is full and no more elements can be added.
- Underflow → Occurs when the queue is empty and no element can be removed.
- Simple (Linear) Queue → Insertion at rear, deletion from front.
- Circular Queue → Solves the space wastage problem of linear queues.
- Priority Queue → Elements are dequeued based on priority rather than order.
- Deque (Double-Ended Queue) → Allows insertion and deletion from both ends.
Feature | Stack (LIFO) | Queue (FIFO) |
---|---|---|
Insertion | At the top | At the rear |
Deletion | From the top | From the front |
Access Order | Last in, first out | First in, first out |
Use Cases | Function calls, undo | Scheduling, buffering |
The program implements a simple queue using arrays. It supports enqueue, dequeue, and display operations, while also handling overflow and underflow conditions.
- Start
- Initialize:
front = -1, rear = -1
.
- If
rear == SIZE - 1
→ print"Overflow"
. - Else, increment
rear
and insert value intoarr[rear]
. - If
front == -1
, setfront = 0
.
- If
front == -1 OR front > rear
→ print"Underflow"
. - Else, remove element at
arr[front]
and incrementfront
.
- If queue is empty → print
"Queue is empty"
. - Else, traverse from
front
torear
and print elements.
- Perform operations as per
main()
. - End
This experiment demonstrated the concept and implementation of a queue in C++ using arrays.
- The FIFO principle was effectively demonstrated.
- Overflow occurs when inserting into a full queue.
- Underflow occurs when removing from an empty queue.
- Class-based encapsulation in C++ simplifies queue operations and improves modularity.
- CPU scheduling in operating systems.
- Printer queue management.
- Buffering in network communications.
- Task scheduling systems.
Queues are essential for managing sequential tasks efficiently and ensuring smooth operation in both software and hardware systems.