Queue ADT (linear queue)
A first-in, first-out list: insert at the rear, delete from the front.
Syllabus says: “Queues”
On this page · 18 sections
FIFO — First In, First Out. Insert at REAR, delete at FRONT. Empty: front = rear = −1.
The queue at the Supplyco counter: whoever came first is served first; newcomers join at the back.
A queue is an ordered list in which insertions take place at one end (rear) and deletions at the other end (front); it follows First-In-First-Out order.
Learning objectives
- Define the queue ADT and FIFO order.
- Write insert (enqueue) and delete (dequeue) for an array queue (KTU May 2026 Q2).
- Explain why a linear array queue wastes space and how a circular queue fixes it.
Prerequisites
Arrays; the stack lesson (same style of overflow/underflow checks).
Introduction
A queue at a ticket counter: newcomers join at the back, and the person at the front is served first. Whoever arrived first leaves first.
Definition
A queue is an ordered list in which insertions take place at one end (the rear) and deletions at the other end (the front). The first element inserted is the first removed — First In, First Out (FIFO).
| Operation | Meaning | Error condition |
|---|---|---|
| insert(x) / enqueue | add x at the rear | overflow — no room |
| delete() / dequeue | remove and return the front element | underflow — queue empty |
| isEmpty(), isFull() | test the state | — |
Array implementation
Use q[0 … MAX−1] with two indices, front (first element) and rear (last element). front = rear = −1 means the queue is empty.
- Insert(x): if rear = MAX − 1 report overflow. If the queue is empty (front = −1) set front = rear = 0; otherwise rear ← rear + 1. Then q[rear] ← x.
- Delete(): if front = −1 report underflow. item ← q[front]. If front = rear (it was the only element) set front = rear = −1; otherwise front ← front + 1. Return item.
rear only moves forward. After a few deletions the slots before front are free, but insert still reports overflow once rear reaches MAX − 1. In the worked example the queue holds just 3 elements when insert 50 overflows, even though slot 0 is empty. A circular queue reuses those slots.
Some textbooks (including Horowitz) let front point one position before the first element and never reset the indices. The behaviour is the same; just say which convention you are using in the exam.
Where queues are used
- CPU and printer scheduling (jobs served in arrival order).
- Keyboard and network buffers.
- Breadth-first search in graphs (Module 3).
- Call-centre and ticket systems.
Worked example
| Operation | Result | front, rear | Array (index 0 … 3) | Front → rear |
|---|---|---|---|---|
| insert(10) | ok | 0, 0 | [10 _ _ _] | 10 |
| insert(20) | ok | 0, 1 | [10 20 _ _] | 10 20 |
| insert(30) | ok | 0, 2 | [10 20 30 _] | 10 20 30 |
| delete | removed 10 | 1, 2 | [_ 20 30 _] | 20 30 |
| insert(40) | ok | 1, 3 | [_ 20 30 40] | 20 30 40 |
| insert(50) | OVERFLOW | 1, 3 | [_ 20 30 40] | 20 30 40 |
| delete | removed 20 | 2, 3 | [_ _ 30 40] | 30 40 |
| delete | removed 30 | 3, 3 | [_ _ _ 40] | 40 |
| delete | removed 40 | -1, -1 | [_ _ _ _] | empty |
| insert(60) | ok | 0, 0 | [60 _ _ _] | 60 |
MAX = 4. “_” marks a slot that holds no live element.
Now watch it run
Every frame comes from really running the algorithm on your input — change it and break it! Tap ▶ Play, or step one frame at a time with ◀ ▶. Keys: Space play/pause, ← → step.
Algorithm (queue using array)
- Step 1:Start.
- Step 2:Set front = -1 and rear = -1.
- Step 3:Display the menu: 1. Insert 2. Delete 3. Display 4. Exit.
- Step 4:Read the choice.
- Step 5:If choice == 1, call Enqueue.
- Step 6:If choice == 2, call Dequeue.
- Step 7:If choice == 3, call Display.
- Step 8:If choice == 4, go to Step 11.
- Step 9:If the choice is not 1, 2, 3 or 4, display "Invalid choice".
- Step 10:Repeat Steps 3 to 9.
- Step 11:Stop.
- Step 1:Start.
- Step 2:If rear == MAX-1, display "Queue Overflow" and go to Step 7.
- Step 3:Read the element to insert, item.
- Step 4:If front == -1, set front = 0.
- Step 5:Increment rear by 1 and set queue[rear] = item.
- Step 6:Display "Element inserted successfully".
- Step 7:Stop.
- Step 1:Start.
- Step 2:If front == -1 or front > rear, display "Queue Underflow" and go to Step 6.
- Step 3:Display queue[front] as the deleted element.
- Step 4:Increment front by 1.
- Step 5:If front > rear, set front = -1 and rear = -1 (the queue is empty again).
- Step 6:Stop.
- Step 1:Start.
- Step 2:If front == -1, display "Queue is empty" and go to Step 4.
- Step 3:For i = front to rear, display queue[i].
- Step 4:Stop.
Pseudocode used by the animated lab · the lines highlighted in the visualizer
- 1front ← −1; rear ← −1
- 2Insert(x):
- 3if rear = MAX − 1 then
- 4print "Queue overflow"; return
- 5if front = −1 then front ← 0; rear ← 0
- 6else rear ← rear + 1
- 7q[rear] ← x
- 8Delete():
- 9if front = −1 then
- 10print "Queue underflow"; return
- 11item ← q[front]
- 12if front = rear then front ← −1; rear ← −1
- 13else front ← front + 1
- 14return item
- 15end
C implementation
linear_queue.ctested ✓ 220/220 cases/* Queue using Array — KTU PCCST303, Module 1 (lab-record style)
* front = rear = -1 means the queue is empty. */
#include <stdio.h>
#define MAX 5
int queue[MAX], front = -1, rear = -1;
void enqueue()
{
int item;
if (rear == MAX - 1) {
printf("Queue Overflow\n");
return;
}
printf("Enter the element to insert: ");
scanf("%d", &item);
if (front == -1)
front = 0;
rear++;
queue[rear] = item;
printf("Element inserted successfully\n");
}
void dequeue()
{
if (front == -1 || front > rear) {
printf("Queue Underflow\n");
return;
}
printf("Deleted element: %d\n", queue[front]);
front++;
if (front > rear) /* queue became empty */
front = rear = -1;
}
void display()
{
int i;
if (front == -1) {
printf("Queue is empty\n");
return;
}
printf("Queue elements: ");
for (i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("\n");
}
int main()
{
int choice;
while (1) {
printf("\n1. Insert 2. Delete 3. Display 4. Exit\n");
printf("Enter your choice: ");
if (scanf("%d", &choice) != 1) /* end of input */
break;
if (choice == 1)
enqueue();
else if (choice == 2)
dequeue();
else if (choice == 3)
display();
else if (choice == 4)
break;
else
printf("Invalid choice\n");
}
return 0;
}
Sample input / output · MAX = 5 · real run, compiled with gcc
1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 1 Enter the element to insert: 10 Element inserted successfully 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 1 Enter the element to insert: 20 Element inserted successfully 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 1 Enter the element to insert: 30 Element inserted successfully 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 3 Queue elements: 10 20 30 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 2 Deleted element: 10 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 3 Queue elements: 20 30 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 5 Invalid choice 1. Insert 2. Delete 3. Display 4. Exit Enter your choice: 4
Bold = what you type. Everything else is printed by the program.
Complexity
| Operation | Time |
|---|---|
| insert | O(1) |
| delete | O(1) |
Edge cases
- Delete from an empty queue → underflow.
- Deleting the last element resets front = rear = −1, so the whole array is usable again.
- front = rear ≠ −1 means exactly one element.
Common mistakes
- Testing rear = MAX instead of rear = MAX − 1.
- Forgetting to set front = 0 on the first insert, so front stays −1.
- Using front > rear as the empty test with the reset convention (mixing two conventions).
- Deleting from the rear — that is a stack or deque operation.
Asked in KTU exams (1)
Quick check — can you get them all?
Practice
Every visualizer has a Practice mode: predict the next state before the animation reveals it. You can also work through the topic's question set on the practice page.
Exam-ready summary
- FIFO: insert at rear, delete at front.
- Empty: front = −1. Full (linear): rear = MAX − 1.
- Insert: check full → (first? front = rear = 0 : rear++) → q[rear] = x.
- Delete: check empty → item = q[front] → (only one? reset to −1 : front++).
- Weakness: space before front is wasted → circular queue.
References
- Horowitz, Sahni & Anderson-Freed, Fundamentals of Data Structures in C, 2/e — Ch. 3 (Stacks and Queues) §3.2
- Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 3/e — §10.1 (Stacks and queues)
Explanations on this page are original. Textbook references point to where the standard treatment can be read; no text is reproduced from them.
Prefer revising from notes? Module-wise notes and question papers on Azhimathi Mayam (opens in a new tab)