Algorithm
Implementation of Linear Queue using Array:
- Initialize the queue with a fixed size and set
front
andrear
to -1. - To enqueue an element:
- Check if the queue is full (i.e.,
rear == size - 1
). - If full, display an overflow message and exit.
- If not, increment
rear
and insert the element atqueue[rear]
.
- Check if the queue is full (i.e.,
- To dequeue an element:
- Check if the queue is empty (i.e.,
front == rear
). - If empty, display an underflow message and exit.
- If not, increment
front
and retrieve the element atqueue[front]
.
- Check if the queue is empty (i.e.,
- To display the queue:
- Check if the queue is empty (i.e.,
front == rear
). - If empty, display a message indicating the queue is empty.
- If not, iterate from
front + 1
torear
and display the elements.
Note: Linear queues do not reuse space of dequeued elements, leading to inefficiency. Circular queues address this limitation.