---
title: "Linear queue using array"
subject: "Data Structure and Algorithm"
chapter: 2
key: "semester_4/data_structure_and_algorithm:linear-queue-array-implementation"
source: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/linear-queue-array-implementation
site: BCT Engineering
---

# Linear queue using array

Data Structure and Algorithm · Chapter 2: The Stack and Queue

- Asked in: 2073 Magh, 6 marks; 2071 Magh, 6 marks (asked 2 times)
- Video: [Linear Queue Implementation - Array](https://www.youtube.com/watch?v=v9BMdz5m5Vo) — Blue Tree Code
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/data_structure_and_algorithm/linear-queue-array-implementation
- Whole chapter: https://www.bctengineer.com/md/semester_4/data_structure_and_algorithm/chapter-2

## Question

Implement a linear queue as a list by using static (array) implementation. Write pseudo code for this implementation.

## Short answer

A linear queue uses an array with front and rear indices. Initialize front=0, rear=-1. Insert increments rear and stores; remove increments front. Overflow when rear==MAX-1, underflow when front>rear. Main drawback: wasted space after deletions.

## Answer

A linear queue can be stored in an array with two integer indices, `front` and `rear`.

#### Structure

```c
#define MAX 100

struct queue {
    int items[MAX];
    int front, rear;
};
```

#### Pseudo code

```text
Initialize(q):           q.front = 0;  q.rear = -1

Empty(q):                return (q.front > q.rear)        /* TRUE if no element */

Full(q):                 return (q.rear == MAX - 1)

Insert(q, x):            /* enqueue */
   1. if Full(q):  print "Queue overflow";  return
   2. q.rear = q.rear + 1
   3. q.items[q.rear] = x

Remove(q):               /* dequeue */
   1. if Empty(q): print "Queue underflow"; return ERROR
   2. x = q.items[q.front]
   3. q.front = q.front + 1
   4. return x

Display(q):
   for i = q.front to q.rear:  print q.items[i]
```

#### C implementation

```c
#include <stdio.h>
#define MAX 5
struct queue { int items[MAX]; int front, rear; };

void init(struct queue *q) { q->front = 0; q->rear = -1; }
int  empty(struct queue *q) { return q->front > q->rear; }
int  full(struct queue *q)  { return q->rear == MAX - 1; }

void insert(struct queue *q, int x) {
    if (full(q)) { printf("Queue overflow\n"); return; }
    q->items[++q->rear] = x;
}
int removeq(struct queue *q) {
    if (empty(q)) { printf("Queue underflow\n"); return -1; }
    return q->items[q->front++];
}
```
**Note:** after many deletions `rear` may reach `MAX - 1` while the front part is free; this wasted space is the main weakness of the linear queue (a circular queue removes it).

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Initially for the linear array queue:
   A. front = -1 and rear = 0
   B. front = rear = MAX
   C. front = rear = 1
   D. front = 0 and rear = -1
   Answer: D. This gives front > rear, i.e. empty. (Key point: Initial values)

2. The Insert (enqueue) operation does:
   A. Check full, increment rear, store item
   B. Check empty, decrement rear, store item
   C. Increment front, store item
   D. Store item then check full
   Answer: A. rear = rear + 1; items[rear] = x. (Key point: Insert steps)

3. Remove (dequeue) returns:
   A. items[front] and then increments front
   B. items[rear] and decrements rear
   C. items[0] and shifts all
   D. items[front] and decrements front
   Answer: A. Underflow is checked first. (Key point: Remove steps)

4. What is the main weakness of the linear queue?
   A. It cannot be empty
   B. It cannot store integers
   C. Wasted space at the front after deletions
   D. It uses a linked list
   Answer: C. rear may reach MAX - 1 although front cells are free. (Key point: Weakness)

## Related in Data Structure and Algorithm

- [Dynamic implementation of queue](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adynamic-queue-linked-algorithm) — Data Structure and Algorithm, chapter 4
- [Array list; static or dynamic choice](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aarray-list-static-dynamic-reason) — Data Structure and Algorithm, chapter 3
- [Static and dynamic data structures; array list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Astatic-dynamic-ds-array-list) — Data Structure and Algorithm, chapter 3
- [Array implementation of list in C](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aarray-list-c-limitations) — Data Structure and Algorithm, chapter 3
- [Circular queue vs linear queue](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Acircular-vs-linear-queue) — Data Structure and Algorithm, chapter 2
- [Linear and circular queue operations](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Alinear-circular-queue-algorithms) — Data Structure and Algorithm, chapter 2

## Related in other subjects

- [Algorithm/pseudo-code for software PID](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Apid-software-algorithm) — Embedded System, chapter 7, asked 3×
- [Gauss-Jordan pseudo-code](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Agauss-jordan-pseudocode) — Numerical Method, chapter 3, asked 4×
- [Bisection method pseudo-code](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Abisection-pseudocode) — Numerical Method, chapter 2, asked 3×
- [False position pseudo-code](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Afalse-position-pseudocode) — Numerical Method, chapter 2, asked 2×
