UNIT 2 • MODULE 1 • LESSON 04
Think of the line at a bank, a supermarket checkout or a taxi stand. The first person to arrive is the first person served. Newcomers join at the back, and nobody pushes in from the side. That orderly line is exactly what computer scientists call a queue.
A queue is the natural partner of the stack. A stack serves the newest item first; a queue serves the oldest item first. In this lesson you will learn the queue ADT and its operations, trace queues on diagrams, write the algorithms in pseudocode, and build two different linear queues in C. You will also meet the weakness of the simple linear queue, which sets up Lesson 05 on circular queues.
What you will learn
- The FIFO rule, and where items join (rear) and leave (front).
- The queue operations: Enqueue, Dequeue, Empty, Full and Peek (front).
- How to trace a queue diagram with front and rear indexes.
- Two array designs for a linear queue: moving front, and shift up with a fixed front.
- How to write both designs in pseudocode and in C, catering for overflow and underflow.
- Why a linear queue wastes space, and where queues are used in computing.
Part 1: The queue ADT (FIFO)
A queue is a linear abstract data type in which items are added at one end, called the rear (or tail), and removed from the other end, called the front (or head). Because the item that has waited longest is always at the front, a queue obeys the rule FIFO: First-In, First-Out.
| Feature | Stack | Queue |
| Order rule | LIFO | FIFO |
| Items added at | Top | Rear |
| Items removed from | Top (same end) | Front (other end) |
| Add operation | Push | Enqueue |
| Remove operation | Pop | Dequeue |
| Indexes tracked | top | front and rear |
Because a queue changes at both ends, it needs two markers instead of one. Here is a queue with capacity 5 after three items joined and one was served:
The queue operations
| Operation | What it does | Safety check |
| Enqueue(item) | Adds item at the rear | Queue must NOT be full, else overflow |
| Dequeue() | Removes and returns the front item | Queue must NOT be empty, else underflow |
| isEmpty() | TRUE when the queue holds no items | None |
| isFull() | TRUE when no more items can be added | None |
| Peek() or Front() | Returns the front item without removing it | Queue must NOT be empty |
The words overflow and underflow mean the same here as for stacks: enqueueing onto a full queue is overflow, and dequeueing from an empty one is underflow. A well-written queue checks before it acts, every time.
Part 2: Two ways to store a queue in an array
An array gives a queue fixed storage. The interesting question is what happens to the front of the array when someone is served. There are two classic answers.
Design A: moving front
Keep two indexes. front holds the index of the item to be served next, and rear holds the index of the item that joined last. Start with front = 0 and rear = -1. Enqueue moves rear up and stores; dequeue reads the item at front and moves front up. No item ever moves inside the array, so both operations take the same short time however long the queue is.
front ← 0
rear ← -1
PROCEDURE enqueue(item)
IF rear = MAX - 1 THEN
OUTPUT "Queue full"
ELSE
rear ← rear + 1
queue[rear] ← item
ENDIF
END PROCEDURE
FUNCTION dequeue()
IF rear < front THEN
OUTPUT "Queue empty"
RETURN a signal value
ELSE
item ← queue[front]
front ← front + 1
RETURN item
ENDIF
END FUNCTION
Empty is rear < front: after the same number of enqueues and dequeues, front has moved one place past rear. Full is rear = MAX - 1, because rear cannot move beyond the last index. Some versions also keep a count of items, which makes Empty (count = 0) and Full (count = MAX) very clear.
Design B: shift up (front fixed at 0)
Keep only rear. The front item is always at index 0. To dequeue, check the queue is not empty, take queue[0], then move every remaining item up one place and reduce rear by one. Exam options often describe this design in words, so learn its three parts in order: check not empty, return the first element, shift the rest up.
FUNCTION dequeue()
IF rear = -1 THEN
OUTPUT "Queue empty"
RETURN a signal value
ENDIF
item ← queue[0]
FOR i ← 0 TO rear - 1
queue[i] ← queue[i + 1]
ENDFOR
rear ← rear - 1
RETURN item
END FUNCTION
| Question | Design A: moving front | Design B: shift up |
| Indexes needed | front and rear | rear only |
| Work per dequeue | One step | Moves every remaining item |
| Wasted slots? | Yes: freed slots at the start are never reused | No: all slots can be used |
| Full when | rear = MAX – 1 (even if front > 0) | rear = MAX – 1 (truly full) |
| Best for | Short-lived queues, then circular queues | Small queues where simplicity matters |
Try both designs in the animator. In Moving front mode, notice that front and rear only ever move right. Keep enqueueing and dequeueing and you will reach a point where Enqueue is refused even though the queue holds only one or two items.
Try it 1Queue animator: two linear designs
Capacity 5 (slots 0 to 4). F marks front and R marks rear. Switch designs and complete the four challenges.
Did you notice the trick the animator uses when the last person leaves? If the queue becomes empty, it resets front = 0 and rear = -1. That small improvement recovers all the space, but only when the queue empties completely. In a busy system it may never empty, so the real cure is the circular queue in Lesson 05.
Part 3: Tracing queue diagrams
Exam questions often give a list of operations and ask for the final contents, or the values of front and rear. Use a table. Here MAX = 5 with the moving-front design:
| Step | Operation | front | rear | Queue (front → rear) |
| Start | Initialise | 0 | -1 | (empty) |
| 1 | Enqueue(P) | 0 | 0 | P |
| 2 | Enqueue(Q) | 0 | 1 | P, Q |
| 3 | Enqueue(R) | 0 | 2 | P, Q, R |
| 4 | Dequeue() | 1 | 2 | Q, R (P served) |
| 5 | Enqueue(S) | 1 | 3 | Q, R, S |
| 6 | Dequeue() | 2 | 3 | R, S (Q served) |
Two things to notice. Rear only changes on enqueue and front only changes on dequeue. And the number of items is always rear - front + 1 (here 3 - 2 + 1 = 2). Use that formula to check your answer in an exam.
Now step through two C programs. The first uses the moving-front design and runs into the wasted-space problem. The second uses shifting.
Try it 2Trace linear queues in C
Choose a design, then step through it. Predict each answer before you reveal it.
Output
Part 4: Linear queues in C
Worked example 1: moving-front queue in a struct
This version bundles the array with its two indexes in a struct, and passes the queue by pointer, just like the struct stack in Lesson 03. enqueue and dequeue return 1 for success and 0 for failure, and dequeue hands back the served value through a pointer.
#include <stdio.h>
#define MAX 4
typedef struct {
int items[MAX];
int front;
int rear;
} Queue;
void init(Queue *q)
{
q->front = 0;
q->rear = -1;
}
int isEmpty(const Queue *q)
{
return q->rear < q->front;
}
int isFull(const Queue *q)
{
return q->rear == MAX - 1;
}
int enqueue(Queue *q, int item)
{
if (isFull(q))
return 0;
q->rear++;
q->items[q->rear] = item;
return 1;
}
int dequeue(Queue *q, int *item)
{
if (isEmpty(q))
return 0;
*item = q->items[q->front];
q->front++;
return 1;
}
int main(void)
{
Queue q;
int v, i;
init(&q);
for (i = 1; i <= 3; i++)
enqueue(&q, i * 11);
dequeue(&q, &v);
printf("Served %d\n", v);
dequeue(&q, &v);
printf("Served %d\n", v);
enqueue(&q, 44);
printf("front=%d rear=%d\n",
q.front, q.rear);
if (!enqueue(&q, 55))
printf("55 refused: rear at end\n");
while (dequeue(&q, &v))
printf("Served %d\n", v);
if (!dequeue(&q, &v))
printf("Underflow: queue empty\n");
return 0;
}
Output:
Served 11 Served 22 front=2 rear=3 55 refused: rear at end Served 33 Served 44 Underflow: queue empty
Read the output carefully. After serving 11 and 22, front is 2. Enqueueing 44 moves rear to 3, the last index. The queue then holds just two items (33 and 44), yet enqueueing 55 is refused because rear has reached MAX - 1. Slots 0 and 1 are empty but unreachable. Finally the loop serves 33 and 44 in arrival order, and the last dequeue reports underflow.
Worked example 2: shift-up queue
This version keeps the front at index 0 and counts how many times an item had to be moved, to show the hidden cost of shifting.
#include <stdio.h>
#define MAX 5
int queue[MAX];
int rear = -1;
int moves = 0;
void enqueue(int item)
{
if (rear == MAX - 1) {
printf("Overflow: %d lost\n", item);
return;
}
rear++;
queue[rear] = item;
}
int dequeue(void)
{
int item, i;
if (rear == -1) {
printf("Underflow\n");
return -1;
}
item = queue[0];
for (i = 0; i < rear; i++) {
queue[i] = queue[i + 1];
moves++;
}
rear--;
return item;
}
int main(void)
{
int i;
for (i = 1; i <= 6; i++)
enqueue(i * 5);
printf("Out: %d\n", dequeue());
enqueue(30);
printf("Out: %d\n", dequeue());
printf("Front is now %d\n", queue[0]);
printf("Items moved: %d\n", moves);
return 0;
}
Output:
Overflow: 30 lost Out: 5 Out: 10 Front is now 15 Items moved: 8
The sixth enqueue overflows because all five slots are in use. Each dequeue then moves the remaining four items up one place, so two dequeues cost eight moves. With five items that is nothing, but with a queue of 10,000 print jobs, every single dequeue would move up to 9,999 items. That is why large systems prefer moving indexes, arranged in a circle.
Queues of records
Real queues rarely hold plain integers. A school registration system might queue whole student records. In C you simply change the item type: declare a struct for the record, then use an array of that struct inside the queue.
typedef struct {
char name[30];
int formLevel;
} Student;
typedef struct {
Student items[200];
int front, rear;
} StudentQueue;
Enqueue and dequeue work exactly as before; only the type of item changes. Struct assignment (q->items[q->rear] = s;) copies every field in one go.
Part 5: Where queues are used
- Print spooling: documents sent to a shared printer wait in a queue and are printed in the order they arrived.
- Process scheduling: an operating system keeps processes that are ready to run in a ready queue, and gives each a turn on the processor.
- Keyboard buffer: keys you press while the computer is busy are stored in order and processed first-come, first-served, so letters never come out scrambled.
- Network routers and web servers: packets and requests wait in queues until they can be handled.
- Simulations: customers at a bank, cars at a toll booth or calls to a help line are modelled with queues to predict waiting times.
Whenever fairness matters, and items should be handled in the order they arrived, the queue is the ADT to choose.
Part 6: Common mistakes with queues
Queues have two moving parts, so there are more ways to slip than with a stack. Watch for these when you write or mark queue code:
- Mixing up the ends. Enqueue changes rear; dequeue changes front (or shifts). If your enqueue touches front, or your dequeue touches rear in the moving-front design, something is wrong.
- Wrong empty test. In the moving-front design, front = 0 does not mean empty. Use rear < front, or keep a count and test count = 0.
- Shifting in the wrong direction. The loop must copy queue[i + 1] into queue[i], working from the front towards the rear. Copying the other way overwrites items before they are moved.
- Forgetting to reduce rear after shifting. Without it, the last item appears twice and the queue never shrinks.
- Missing checks. Every dequeue needs an empty check and every enqueue needs a full check, exactly as with push and pop.
A good habit is to keep a count variable alongside front and rear. It costs one integer, makes Empty and Full obvious, and is the key idea that makes the circular queue in the next lesson easy to write. When you trace your own code, always test the two boundaries: the very first enqueue into an empty queue, and the dequeue that removes the last item. Most queue bugs show up at one of those two moments.
Try it 3Queue trace and rules drill
Pick a mode. "Trace" gives a run of Enqueue and Dequeue calls and asks about the result. "Rules" checks queue facts.
Summary
| Idea | Remember |
| Rule | FIFO: join at the rear, leave from the front. |
| Operations | Enqueue, Dequeue, isEmpty, isFull, Peek. |
| Moving front | front = 0, rear = -1 to start; empty when rear < front; full when rear = MAX - 1. |
| Shift up | Front fixed at 0; dequeue: check not empty, take queue[0], shift the rest up. |
| Items stored | rear - front + 1 (moving front). |
| Weakness | Linear queues waste freed slots, or waste time shifting. |
Review questions
1. Define a queue and state the rule it follows.
Show answer
A queue is a linear ADT where items are added at the rear and removed from the front. It follows FIFO: the first item in is the first item out.
2. Name the operations that add and remove items in a queue, and the error each can cause.
Show answer
Enqueue adds at the rear and can cause overflow if the queue is full. Dequeue removes from the front and can cause underflow if the queue is empty.
3. A moving-front queue has MAX = 6. Starting empty, perform Enqueue(A), Enqueue(B), Enqueue(C), Dequeue(), Enqueue(D). Give front, rear and the contents.
Show answer
front = 1, rear = 3, contents from front to rear: B, C, D.
4. Describe the dequeue operation for a queue whose front is always at index 0.
Show answer
Make sure the queue holds at least one item. Copy the first element (index 0) to return it. Move every remaining element up one place. Reduce rear by one. Return the copied element.
5. Explain why a moving-front linear queue can report full while holding only one item.
Show answer
Front and rear only move towards the end of the array. Once rear reaches MAX - 1, no more items can be added, even if dequeues have freed slots at the start of the array.
6. State a benefit and a drawback of the shift-up design.
Show answer
Benefit: every slot can be reused, so no space is wasted. Drawback: each dequeue moves every remaining item, which is slow for long queues.
7. Write a C function int isEmpty(const Queue *q) for the moving-front struct queue.
Show answer
int isEmpty(const Queue *q)
{
return q->rear < q->front;
}
8. Give two uses of a queue in a computer system and explain why FIFO is right for each.
Show answer
For example: a print spooler, so jobs print in the order they were sent; a keyboard buffer, so characters are processed in the order typed; a ready queue for processes, so each waits its turn fairly.
Homework
- Draw the array, front and rear after each step (MAX = 4, moving front): Enqueue(7), Enqueue(3), Dequeue(), Enqueue(9), Enqueue(1), Enqueue(6). Which step fails, and why?
- Add a peek function to queue_move.c that passes back the front item without changing front.
- Add a count field to the struct queue and rewrite isEmpty and isFull to use it.
- Write a C program for a queue of Student records (name and form level). Enqueue four students and print them as they are served.
- Change queue_move.c so that when the queue becomes empty, front and rear are reset. Which test from Worked example 1 now behaves differently?
Mastery quiz: Queues
This 10-question quiz is separate from the short Quick quiz at the end of the lesson. Score 8/10 or higher to earn this lesson’s badge. Your best score is saved in this browser on this device.
Mastery quiz: Queues: FIFO, Enqueue, Dequeue and Linear Queues in C
Your course badges: loading saved progress…
View badges
No course badges earned yet. Score 8/10 in a lesson quiz to earn its badge.
Saved in this browser on this device. Clearing browser data may remove your badges.
Answer all 10 questions. Earn this lesson’s badge at 8/10 or higher. Read the explanations, revisit the Try it activities and retry as often as you need. These are self-study badges, not formal grades.
Exam tips
Specimen Unit 2 Paper 01 (2021), Item 1. A scenario describes requests that must be handled in the order they arrive, and asks which structure to use. Look for words like "in turn", "in order received" or "waiting": they point to a queue. Words like "most recent" or "undo" point to a stack.
Specimen Unit 2 Paper 01 (2021), Item 15. The options describe dequeue for an array queue in words. The correct description must include all three parts in the right order: check the queue is NOT empty first, return the first element, then shift the remaining elements up. Options that skip the check, or check the wrong condition, are traps.
Written questions. If you are asked to write an enqueue function for a queue struct, include: the full check, increasing rear, storing the item at rear, and a return value or message. For dequeue: the empty check, reading the front item, moving front (or shifting), and returning the item.
More worked answers are on the CS Solutions page.
Previous: Unit 2 • Module 1 • Lesson 03 – Building a Stack in C with an Array. Next: Unit 2 • Module 1 • Lesson 05 – Circular Queues: Wrapping Around, Full vs Empty, in C.
Syllabus link: CAPE Computer Science (CXC A19/U2/21), Data Structures (Unit 2, Module 1). Specific Objective 2: distinguish among ADTs (queues). Specific Objective 3: perform basic operations on queues using diagrams and algorithms (Enqueue, Dequeue, Empty, Full). Specific Objective 4: write programs to implement queues using one-dimensional arrays in C.
Video search terms
Search YouTube for: "queue data structure enqueue dequeue", "linear queue array implementation C", "FIFO queue explained".
Try it 4Quick quiz
Tap an answer. All eight questions are self-checks on this lesson.
Updates
2026/12/01
- First version of the lesson.
© 2026 islandclass.org
