algoritmauzmanı
Sign in
TREN
5.3Queue with a Linked List
Lesson 5.3.5

Array or linked list?

You have coded a queue in three ways: with a linear array, with a circular array and with a linked list. All three offer the same operations and all of them take . This lesson shows where they differ: wasted cells, capacity, memory and real-world speed. By the end you will know which one to pick and when.

What you will learn
  • Why all three implementations are the same and why the calling code does not change
  • How they differ in wasted cells, capacity, memory and cache behaviour
  • When a circular array fits better and when a linked list does
6 min

1One abstract data type, three structures

A is an : it says which operations it offers in order, not how they are coded. All three versions use the same function names: create, isEmpty, enqueue, dequeue, peek. Only the inside of struct Queue changes.

// With a linear array (5.1)struct Queue {    int items[CAPACITY];    int front;          // index of the front    int rear;           // index of the rear, -1 when empty}; // With a circular array (5.2)struct Queue {    int items[CAPACITY];    int front;    int rear;           // wraps: (rear + 1) % CAPACITY    int count;          // number of elements}; // With a linked list (5.3)struct Queue {    struct Node* front; // the front node, NULL when empty    struct Node* rear;  // the rear node, NULL when empty};

The code that uses the queue does not change at all. The snippet below compiles with any of the three versions and gives the same result:

struct Queue q;int x;create(&q);enqueue(&q, 10);enqueue(&q, 20);if (peek(&q, &x)) printf("%d\n", x);      // 10while (dequeue(&q, &x)) printf("%d ", x);  // 10 20

The only difference is isFull: both array versions have it, the linked-list version does not need it. As long as the calling code does not rely on a fixed capacity, switching implementations means changing a single file.

2Time

, , and isEmpty are in all three versions. In an array and are indexes; in a linked list they are pointers. No operation shifts elements or has to the list.

This depends on building the queue correctly. If you shift the elements left on every dequeue in an array, dequeue becomes . If you pick the wrong ends in a linked list, adding at the head and removing at the end, dequeue has to search for the node before the last one and again becomes O(n).

3Wasted cells

The linear array has a big flaw. Dequeue moves front ahead, but the freed cells are never used again. Once rear reaches the end of the array, enqueue fails even if the queue holds a single element. The lesson on the problem with the linear queue shows this step by step.

The solves this: when rear reaches the end of the array it wraps to the start and reuses the freed cells. A linked list never has this problem; the node that leaves is freed and a new node is allocated for each new element.

4Capacity and memory

Both array versions have a fixed capacity. When the queue is full, enqueue fails; you have to guess the capacity correctly up front. Unused cells take space too: a queue with capacity 1000 holding 3 elements has 997 empty cells. A linked list only takes space for the elements that exist, and enqueue fails only when memory is completely used up and returns .

On the other hand, in an array each element takes only its own size: 4 bytes for an int. In a linked list each node carries a pointer next to the value. On a 64-bit system this usually comes to 16 bytes per node with alignment, plus the record malloc keeps for each allocation.

5Cache behaviour

Array elements sit next to each other in memory. When the processor reads one cell it brings its neighbours into the cache too, so enqueues and dequeues in a row are very fast. Linked-list nodes can be scattered across memory, and every enqueue calls malloc and every dequeue calls . Despite the same Big-O, the circular array is usually faster in practice.

6Summary table

PropertyLinear arrayCircular arrayLinked list
enqueue, dequeue, peek, isEmptyO(1)O(1)O(1)
Freed cellsNever used againReused after wrappingNo problem
CapacityFixedFixedUntil memory runs out
isFullYesYesNot needed
Memory per element (int)4 bytes4 bytesAbout 16 bytes, plus the malloc record
Unused spaceEmpty cells take spaceEmpty cells take spaceNone
Cache behaviourGoodGoodWeak: nodes scattered
Memory management (C)NoneNonefree on every dequeue

7Which one to choose?

  • Linear array: almost never chosen in a real program. It is there to learn the idea of a queue and to see why the circular queue is needed.
  • Circular array: when you know the maximum number of elements or can set a sensible upper limit. It is fast, and in C there is no free to worry about. Fixed-size buffers such as keyboard and network buffers work this way. Java's ArrayDeque and Python's collections.deque are array-based too and grow when they fill up.
  • Linked list: when the number of elements varies a lot and you do not want an upper limit, or when you need every enqueue to be truly O(1) (no copy while growing).

There is one more version of the linked-list queue: the queue on a circular linked list, which keeps only rear instead of two pointers. The front node is rear->next; with one pointer less, the same operations still take O(1).

In an exam, the answer to "which one is better" is not a single word. Say that time is O(1) in all three, then explain the difference through wasted cells, capacity, memory and the cache.

8Try it

A linear array queue with capacity 5 takes 5 elements, then 4 are removed. One element is left. What happens on a new enqueue?

Show the answer

The answer is B. In a linear queue rear only moves forward. Once it reaches the end of the array the queue counts as full. A circular queue and a linked list keep adding in this case.

You replaced a circular array queue with a linked-list queue. What must change in the code that uses the queue?

Show the answer

The answer is B. A queue is an . The calling code only knows create, isEmpty, enqueue, dequeue and peek; these stay the same even when the structure inside changes.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.