Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
Module 1 · syllabusAsked 1× in KTU papersChecked by code · teacher review pending

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
ormakkurippu ✎
Never-forget card

FIFO — First In, First Out. Insert at REAR, delete at FRONT. Empty: front = rear = −1.

Picture it

The queue at the Supplyco counter: whoever came first is served first; newcomers join at the back.

Write this in the exam

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

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).

OperationMeaningError condition
insert(x) / enqueueadd x at the rearoverflow — no room
delete() / dequeueremove and return the front elementunderflow — 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.

  1. 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.
  2. 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.
Watch outThe weakness of a linear queue

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

Worked example: MAX = 4 (note the overflow at insert 50)checked by code
OperationResultfront, rearArray (index 0 … 3)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
deleteremoved 101, 2[_ 20 30 _]20 30
insert(40)ok1, 3[_ 20 30 40]20 30 40
insert(50)OVERFLOW1, 3[_ 20 30 40]20 30 40
deleteremoved 202, 3[_ _ 30 40]30 40
deleteremoved 303, 3[_ _ _ 40]40
deleteremoved 40-1, -1[_ _ _ _]empty
insert(60)ok0, 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 ◀ ▶.

Loading visualizer…

Algorithm (queue using array)

AlgorithmQueue using Array: main menu
  1. Step 1:
    Start.
  2. Step 2:
    Set front = -1 and rear = -1.
  3. Step 3:
    Display the menu: 1. Insert 2. Delete 3. Display 4. Exit.
  4. Step 4:
    Read the choice.
  5. Step 5:
    If choice == 1, call Enqueue.
  6. Step 6:
    If choice == 2, call Dequeue.
  7. Step 7:
    If choice == 3, call Display.
  8. Step 8:
    If choice == 4, go to Step 11.
  9. Step 9:
    If the choice is not 1, 2, 3 or 4, display "Invalid choice".
  10. Step 10:
    Repeat Steps 3 to 9.
  11. Step 11:
    Stop.
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.
AlgorithmDisplay
  1. Step 1:
    Start.
  2. Step 2:
    If front == -1, display "Queue is empty" and go to Step 4.
  3. Step 3:
    For i = front to rear, display queue[i].
  4. Step 4:
    Stop.
Pseudocode used by the animated lab · the lines highlighted in the visualizer
Linear queue (array)
  1. 1front ← −1; rear ← −1
  2. 2Insert(x):
  3. 3if rear = MAX − 1 then
  4. 4print "Queue overflow"; return
  5. 5if front = −1 then front ← 0; rear ← 0
  6. 6else rear ← rear + 1
  7. 7q[rear] ← x
  8. 8Delete():
  9. 9if front = −1 then
  10. 10print "Queue underflow"; return
  11. 11item ← q[front]
  12. 12if front = rear then front ← −1; rear ← −1
  13. 13else front ← front + 1
  14. 14return item
  15. 15end

C implementation

linear_queue.ctested ✓ 220/220 cases
C
/* 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.

Open in the Code Lab →

Complexity

Space O(MAX). Shifting all elements left after each delete would avoid the wasted space, but costs O(n) per delete — a circular queue gets the same benefit in O(1).
OperationTime
insertO(1)
deleteO(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?

Question 1

Q1.Linear array queue, MAX = 5: rear = 4 and front = 3. Can you insert?

Question 2

Q2.Enqueue 5, 6, 7, dequeue, enqueue 8. Front element?

Question 3

Q3.A queue is used for…

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)