5. HL Extension β€” Abstract Data Structures

Queues

Queues in Abstract Data Structures

students, think about standing in line at a school cafeteria πŸ” or waiting for a printer job at the library πŸ–¨οΈ. The first person who arrives is the first person served. That simple idea is the heart of a queue. In Computer Science, queues are an important abstract data structure because they help manage information in a fair, organized way.

In this lesson, you will learn how queues work, why they matter, and how they fit into the broader IB Computer Science HL topic of Abstract Data Structures. By the end, you should be able to explain queue terminology, trace queue operations, and connect queues to real-world systems such as operating systems, traffic flow, and task scheduling.

What is a Queue?

A queue is a linear data structure that follows the First In, First Out principle, often written as FIFO. This means the first item added is the first item removed.

The idea is easy to remember if you think about people lining up for concert tickets 🎟️. The first person in line gets served first, and later arrivals must wait behind them. In a queue, items are inserted at the rear and removed from the front.

Important queue terminology includes:

  • Enqueue: add an item to the rear of the queue.
  • Dequeue: remove an item from the front of the queue.
  • Front: the first item in the queue.
  • Rear: the last item in the queue.
  • Peek or front value: look at the front item without removing it.
  • Overflow: trying to add an item when the queue is full, in a fixed-size implementation.
  • Underflow: trying to remove an item when the queue is empty.

A queue is an abstract data type because we care about what it does, not exactly how it is built. The same queue behavior can be implemented in different ways, such as with arrays or linked lists.

Core Queue Operations and How They Work

To understand queues properly, students, you need to know the main operations and what happens during each one.

Suppose a queue currently contains these items:

$$\text{Front} \rightarrow [A, B, C] \rightarrow \text{Rear}$$

If we enqueue $D$, it is added to the rear:

$$\text{Front} \rightarrow [A, B, C, D] \rightarrow \text{Rear}$$

If we then dequeue, item $A$ is removed from the front:

$$\text{Front} \rightarrow [B, C, D] \rightarrow \text{Rear}$$

This order matters. The oldest item is always processed first.

Some queues also support a peek operation. If we peek at the queue above, the result is $B$, because $B$ is at the front. The queue stays unchanged.

A useful way to compare queue operations is this:

  • Enqueue changes the rear end.
  • Dequeue changes the front end.
  • Peek reads the front end.

In most algorithmic discussions, these operations are designed to be efficient. A well-implemented queue should allow insertion and removal without having to shift every item each time.

Real-World Uses of Queues

Queues are not just textbook ideas. They appear everywhere in computing and daily life.

1. Printer queues πŸ–¨οΈ

When several students send files to a printer, the printer usually handles them in the order received. The first file submitted is printed first. This prevents confusion and keeps processing fair.

2. CPU task scheduling

Operating systems use queues to manage tasks. For example, if many programs want attention from the processor, the system may place them in a queue. This helps the computer decide what to run next.

3. Network packet handling

Data sent across a network often arrives in packets. Routers and devices may use queues to temporarily store packets before forwarding them. This is useful when traffic is heavy.

4. Breadth-first search 🧭

In graph theory, queues are essential for breadth-first search. The algorithm explores nearby nodes first, then moves outward level by level. This is useful in route finding, social networks, and shortest-path style problems in unweighted graphs.

5. Simulation of waiting systems

Queues are used to model real situations like bank lines, supermarket checkouts, or hospital triage systems. By studying a queue, programmers can predict delays and improve service.

These examples show that queues are a powerful tool for organizing work in a predictable way.

Queue Implementations and Efficiency

In IB Computer Science HL, you should understand that a queue is an abstract idea and can be implemented in more than one way.

Array-based queue

An array can store queue items in consecutive locations. One problem is that if the front item is removed from the start of the array, all later items may need to be shifted left. That takes time.

To improve this, many implementations use a circular queue. In a circular queue, the array is treated like a loop. When the rear reaches the end of the array, it wraps back to the beginning if there is free space.

This is often tracked using two pointers or indices:

  • $\text{front}$ points to the first valid item.
  • $\text{rear}$ points to the last valid item, or the next insertion position depending on the design.

Circular queues reduce wasted space and avoid unnecessary shifting.

Linked-list-based queue

A linked list can also implement a queue. Each node stores data and a link to the next node. The queue keeps references to both the front and rear.

This approach is flexible because the queue can grow and shrink dynamically, limited mainly by memory. Enqueue and dequeue can be efficient because you do not need to move every item.

A linked-list queue is a good example of how abstraction helps computer scientists focus on the behavior of the data structure rather than its storage details.

Efficiency idea

In Computer Science, efficiency often means how much time or memory an algorithm uses. Queue operations are commonly designed to work in constant time, written as $O(1)$, when implemented well.

That means the time needed does not grow with the number of items in the queue. For example, adding one item to the rear of a linked-list queue can be $O(1)$ because only a few pointers change.

Queue Behavior: Tracing an Example

Let’s trace a simple queue step by step, students.

Start with an empty queue:

$$[]$$

Now perform these operations:

  1. Enqueue $10$
  2. Enqueue $20$
  3. Enqueue $30$
  4. Dequeue
  5. Enqueue $40$
  6. Peek

After step 1:

$$[10]$$

After step 2:

$$[10, 20]$$

After step 3:

$$[10, 20, 30]$$

After step 4, $10$ is removed:

$$[20, 30]$$

After step 5:

$$[20, 30, 40]$$

At step 6, peek returns $20$ because it is at the front.

This kind of tracing is useful in exams because you may be asked to show the contents of a queue after a sequence of operations. Always remember the order: first in, first out.

Queue vs Other Abstract Data Structures

Queues belong to the broader family of abstract data structures, along with stacks, lists, trees, and graphs.

A queue is different from a stack. A stack follows Last In, First Out or LIFO. In a stack, the last item added is the first removed. A queue does the opposite.

Here is a quick comparison:

  • Queue: $\text{FIFO}$
  • Stack: $\text{LIFO}$

This difference changes how each structure is used. A stack is useful for backtracking and function calls. A queue is useful when fairness or order of arrival matters.

Queues also connect to larger ideas in HL abstraction. An abstract data structure hides implementation details so programmers can choose the best underlying structure for the task. This allows the same queue interface to work whether it is built with arrays, linked lists, or other methods.

Conclusion

Queues are a clear example of how abstraction helps organize computation. They store items in a strict order, with the first item added becoming the first removed. This FIFO behavior makes queues useful in printing, scheduling, networking, simulations, and graph algorithms.

For IB Computer Science HL, you should be able to explain queue vocabulary, trace enqueue and dequeue operations, and compare implementation methods. You should also understand why queues are part of the wider topic of abstract data structures: they represent behavior and purpose, while different internal designs handle the details.

Study Notes

  • A queue is a linear abstract data structure that follows $\text{FIFO}$.
  • The main operations are enqueue, dequeue, and peek.
  • Items are added at the rear and removed from the front.
  • A queue can become empty, which may cause underflow if a removal is attempted.
  • A fixed-size queue can become full, which may cause overflow if an insertion is attempted.
  • Queues are used in printer jobs, task scheduling, network traffic, and breadth-first search.
  • Circular arrays reduce wasted space in array-based queue implementations.
  • Linked lists can implement queues efficiently and flexibly.
  • Good implementations can make queue operations run in $O(1)$ time.
  • Queues are an important part of HL Extension β€” Abstract Data Structures because they show the relationship between abstract behavior and concrete implementation.

Practice Quiz

5 questions to test your understanding

Queues β€” IB Computer Science HL | A-Warded