The most direct way to store a queue is an array with two indices: front for the next item to leave and rear for the last item that arrived. This page builds that “linear” queue and shows its weakness, which the circular queue later fixes.
The representation
front = index of the first item; rear = index of the last item.
Empty queue: front = 0, rear = -1 (so front > rear).
Add: rear = rear + 1, then store at element[rear].
Delete: read element[front], then front = front + 1.
The queue cannot grow when rear == QUEUE_LENGTH - 1.
Intuition
Think of a long bench where people sit from left to right and the person on the far left leaves first. The left end of the occupied part moves right as people leave, and the right end moves right as people arrive. When the occupied part reaches the right edge of the bench, nobody else can sit down, even if the left part of the bench is empty.
Example 5.3 A linear array queue and the wasted cells
Goal. Fill a queue of length 5, delete two items, then try to add one more.
Program. add and delete_front return 1 for success and 0 for overflow or underflow. When the queue becomes empty, both indices are reset to the start.
C · queue_array.c Copy #include <stdio.h>
#define QUEUE_LENGTH 5
typedef struct {
int element[QUEUE_LENGTH];
int front; /* index of the first element */
int rear; /* index of the last element (-1: none) */
} Queue;
void create(Queue * q) { q->front = 0 ; q->rear = - 1 ; }
int is_empty( const Queue * q) { return q->front > q->rear; }
/* Add x at the rear. Returns 1 on success, 0 if rear reached the end. */
int add(Queue * q, int x)
{
if (q->rear == QUEUE_LENGTH - 1 )
return 0 ; /* no cell after rear */
q->element[ ++ q->rear] = x;
return 1 ;
}
/* Delete from the front into *x. Returns 0 if the queue is empty. */
int delete_front(Queue * q, int * x)
{
if (is_empty(q))
return 0 ;
* x = q->element[q->front ++ ];
if (is_empty(q)) /* queue became empty: start again at 0 */
create(q);
return 1 ;
}
void show( const Queue * q)
{
printf( "front=%d rear=%d queue:" , q->front, q->rear);
for ( int i = q->front; i <= q->rear; i ++ )
printf( " %d" , q->element[i]);
printf( " \n " );
}
int main( void )
{
Queue q;
int x;
create( & q);
for ( int v = 10 ; v <= 50 ; v += 10 )
add( & q, v);
show( & q);
delete_front( & q, & x);
printf( "deleted %d \n " , x);
delete_front( & q, & x);
printf( "deleted %d \n " , x);
show( & q);
/* Only 3 elements are stored, but rear is at the last cell: */
printf( "add 60: %s \n " , add( & q, 60 ) ? "ok" : "no room after rear (cells 0-1 are wasted)" );
return 0 ;
}
Run it.
Output Copy front=0 rear=4 queue: 10 20 30 40 50
deleted 10
deleted 20
front=2 rear=4 queue: 30 40 50
add 60: no room after rear (cells 0-1 are wasted)
Result. After two deletions the queue holds only 3 items, yet 60 cannot be added: rear is at the last cell and cells 0 and 1 are unusable until the queue empties completely.
This drift is the main problem of the linear array queue: every element moves the queue’s “window” to the right, so space at the start of the array is wasted. Two fixes exist: shift all elements to the start (costs O ( n ) O(n) O ( n ) per deletion), or wrap around to the start of the array, which is the circular queue later in this unit.
Correction · Program 5.1 (array queue)
Study material says: Program 5.1 (§5.3.1) leaves out several semicolons (after the struct and after printf calls), writes retrun x; and if q.front > q.rear without parentheses, calls add(&q, x) but defines add(y), and uses x in delete() without declaring it. With the global q starting at front = rear = 0, the first add stores at element[1], and on overflow rear has already been increased.
Correct version: Declare the functions with types, e.g. int add(Queue *q, int x) and int delete_front(Queue *q, int *x); start with front = 0, rear = -1; check for overflow before changing rear; return a success flag and the value through a pointer. Use int main(void).
Why: The missing semicolons, the misspelt return, the condition without parentheses and the undeclared x are syntax errors, so the program does not compile. With front = rear = 0, the “empty” test front > rear is false at the start, so a delete on a new queue returns the unused element[0]; and an overflowing add leaves rear pointing past the array.
Key idea
A linear array queue adds at rear and deletes at front, both O ( 1 ) O(1) O ( 1 ) , but both indices only move forward, so cells freed at the start of the array cannot be reused.
TEE Exam note
For “implement a queue using an array”: declare the array with front and rear, state the empty and full conditions, write the add and delete algorithms with their checks, and explain the wasted-space problem with a small diagram. This motivates the circular queue, which examiners often ask about next.
Common mistake
Testing “queue full” with rear == front. In a linear queue, rear == front means exactly one item; full is rear == QUEUE_LENGTH - 1.
Common mistake
Moving rear before checking for overflow. If the check fails, rear is left pointing outside the array.
Key takeaways
Array queue: front = first item, rear = last item; empty when front > rear.
Add: check full, ++rear, store. Delete: check empty, read element[front], front++.
Both operations are O ( 1 ) O(1) O ( 1 ) .
Space freed at the start is wasted in a linear queue; the circular queue solves this.
Practice questions Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 1 · multiple choice A linear queue has length 6, front = 3 and rear = 5. How many items does it hold, and can a new item be added?
A 2 items; yes B 3 items; no C 3 items; yes D 6 items; no
Check answer
Show answer Answer: B. 3 items; no
Indices 3, 4 and 5 hold items (5 − 3 + 1 = 3). rear is already the last index, so a linear queue cannot add more, although cells 0–2 are free.
Question 2 · multiple choice With front = 0 and rear = -1 for an empty queue, which test means the queue is empty?
A front == rearB rear == QUEUE_LENGTH - 1C front > rearD front == 0
Check answer
Show answer Answer: C. front > rear
Each delete moves front past rear when the last item leaves. front == rear means one item is left.
Question 3 · multiple choice What does this program print?
C · q5-linear.c Copy #include <stdio.h>
int main( void )
{
int q[ 5 ], front = 0 , rear = - 1 ;
q[ ++ rear] = 3 ;
q[ ++ rear] = 6 ;
q[ ++ rear] = 9 ;
int a = q[front ++ ];
q[ ++ rear] = 12 ;
int b = q[front ++ ];
printf( "%d %d front=%d rear=%d \n " , a, b, front, rear);
return 0 ;
} A 3 6 front=2 rear=3 B 9 12 front=2 rear=3 C 3 6 front=0 rear=1 D 3 9 front=1 rear=3
Check answer
Show answer Answer: A. 3 6 front=2 rear=3
Items leave in the order 3, 6. Both indices only increase: front is 2 after two deletions and rear is 3 after four additions. “9 12” would be stack (LIFO) behaviour.
Question 4 · multiple choice What is the main drawback of the linear array queue?
Check answer
Show answer Answer: D. Cells freed at the front cannot be reused, so the queue can report “full” while space is free.
Both indices move only forward. Shifting the elements would fix it but makes deletion O ( n ) O(n) O ( n ) ; the circular queue fixes it in O ( 1 ) O(1) O ( 1 ) .
Question 5 · multiple choice In a correct add, when should the overflow check be done?
A Before rear is changed B After rear is incremented C After the item is stored D Only when front is 0
Check answer
Show answer Answer: A. Before rear is changed
If rear is incremented first and the queue is full, rear points outside the array even though the item is rejected.
Question 6 · short answer
Write C functions to add and delete an item in a linear array queue, with overflow and underflow checks.
Show worked answer #define N 50
int q[N], front = 0, rear = -1;
int add(int x) {
if (rear == N - 1) { printf("Queue overflow\n"); return 0; }
q[++rear] = x; return 1;
}
int delete_front(int *x) {
if (front > rear) { printf("Queue empty\n"); return 0; }
*x = q[front++];
if (front > rear) { front = 0; rear = -1; } /* reset when empty */
return 1;
}
Both are O ( 1 ) O(1) O ( 1 ) .