Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
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.
AlgorithmEnqueue (insert at rear)
  1. Step 1:
    Start.
  2. Step 2:
    If rear == MAX-1, display "Queue Overflow" and go to Step 7.
  3. Step 3:
    Read the element to insert, item.
  4. Step 4:
    If front == -1, set front = 0.
  5. Step 5:
    Increment rear by 1 and set queue[rear] = item.
  6. Step 6:
    Display "Element inserted successfully".
  7. Step 7:
    Stop.
AlgorithmDequeue (delete from front)
  1. Step 1:
    Start.
  2. Step 2:
    If front == -1 or front > rear, display "Queue Underflow" and go to Step 6.
  3. Step 3:
    Display queue[front] as the deleted element.
  4. Step 4:
    Increment front by 1.
  5. Step 5:
    If front > rear, set front = -1 and rear = -1 (the queue is empty again).
  6. Step 6:
    Stop.
Example: MAX = 3 — note insert 50 overflows although slot 0 is freechecked by code
OperationResultfront, rearArray (index 0 … 2)Front → rear
insert(10)ok0, 0[10 _ _]10
insert(20)ok0, 1[10 20 _]10 20
insert(30)ok0, 2[10 20 30]10 20 30
insert(40)OVERFLOW0, 2[10 20 30]10 20 30
deleteremoved 101, 2[_ 20 30]20 30
insert(50)OVERFLOW1, 2[_ 20 30]20 30
deleteremoved 202, 2[_ _ 30]30
deleteremoved 30-1, -1[_ _ _]empty
deleteUNDERFLOW-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