B.Tech S3 (S) Examinations May 2026 (2024 Scheme)QP 06PCCST303112504Part A · Q23 marksCO1Module 1
Question 2
Write algorithm to insert and delete operations of a queue using array.
Topics: Queue ADT (linear queue)
Watch this exact question run
Loading visualizer…
Solution
Assumptions:
- Array queue[0 … MAX−1]; front = rear = −1 when empty; front indexes the first element.
- 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.
| Operation | Result | front, rear | Array (index 0 … 2) | 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 |
| insert(40) | OVERFLOW | 0, 2 | [10 20 30] | 10 20 30 |
| delete | removed 10 | 1, 2 | [_ 20 30] | 20 30 |
| insert(50) | OVERFLOW | 1, 2 | [_ 20 30] | 20 30 |
| delete | removed 20 | 2, 2 | [_ _ 30] | 30 |
| delete | removed 30 | -1, -1 | [_ _ _] | empty |
| delete | UNDERFLOW | -1, -1 | [_ _ _] | empty |
MAX = 3. “_” marks a slot that holds no live element.
How to write it in the exam
- Insert at rear: if rear == MAX − 1 → "Queue Overflow"; otherwise if front == −1 set front = 0; rear++; queue[rear] = item.
- Delete at front: if front == −1 or front > rear → "Queue Underflow"; otherwise item = queue[front]; front++; if front > rear, reset front = rear = −1.
- O(1) each. Mention the limitation: freed front slots are not reused (→ circular queue).
How this answer was verified
- tests/linear-expression.test.ts — reference-model property tests
- linear_queue.c (menu program) — exact output tested; every insert/delete outcome matches the engine