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

Creating a linked-list queue

An array queue works with a fixed capacity. A built on a linked list allocates a separate for each element and grows for as long as memory lasts. In this lesson you set up the structure and write the empty queue and the emptiness check.

What you will learn
  • Why a linked-list queue keeps two pointers: and
  • Which end is the front and which is the rear, and why
  • How create and isEmpty are written in C, Java and Python
5 minTime O(1)Memory O(1)C · Java · Python

1Idea

In a stack all the work happens at one end, so a linked-list stack gets by with a single pointer. In a queue both ends are busy: adds at the back and removes from the front. So the structure keeps two : front points to the first node of the list and rear to the last one.

Which end should be the front? In a singly linked list deleting from the head is , but deleting from the end is , because the node before the last one is found by from the start. Adding at the end is O(1) if a pointer to the last node is kept. So removal happens at the head and insertion at the tail: the head is front and the tail is rear.

When the queue is empty, both pointers are . Checking front is enough to tell. In a queue with one element, front and rear point to the same node.

A linked-list queue has no isFull. The queue can only fail to grow when memory runs out, and finds that out by checking the result of malloc.

2Step by step

The drawing first shows an empty queue, then a queue after 10, 20 and 30 were added in that order. At each step the matching line of code lights up.

10?0x1a4020?0x1a9030?0x2c10front = NULLrear = NULL
  1. create writes into front and rear. There are no nodes yet; this is the empty queue.
  2. isEmpty checks whether front is NULL. Right now it is, so the result is true.
  3. The state after three calls. 10, which came first, is at the head and front points to it. 30, which came last, is at the tail and rear points to it.
  4. It is 10's turn: it leaves first. A new element will be added after 30. One end handles removal, the other handles insertion.
  5. isEmpty now returns false, because front points to a node.

struct Node {    int data;    struct Node* next;}; struct Queue {    struct Node* front;  // the front node    struct Node* rear;   // the rear node}; void create(struct Queue* q) {    q->front = NULL;    q->rear = NULL;} bool isEmpty(struct Queue* q) {    return q->front == NULL;}
class Node {    int data;    Node next;    Node(int data) { this.data = data; }} class LinkedQueue {    Node front = null;  // the front node    Node rear = null;   // the rear node     boolean isEmpty() {        return front == null;    }}
class Node:    def __init__(self, data):        self.data = data        self.next = None class LinkedQueue:    def __init__(self):        self.front = None  # the front node        self.rear = None   # the rear node     def is_empty(self):        return self.front is None

The snippets focus on the operation in this lesson. The C examples need stdlib.h.

3Complexity

Time
O(1)

Each function does one or two assignments or a single comparison.

Memory
O(1)

An empty queue takes only the space of two pointers.

An array queue reserves room for its full capacity up front. A linked-list queue allocates room node by node as elements arrive.

4Common mistake

A common mistake is putting the ends the wrong way round: adding at the head and removing from the tail.

// wrong layout: front at the tail, rear at the headnode->next = q->rear;   // insert at head, O(1)q->rear = node;// removing needs the node before the last: O(n)

Insertion is still quick, but every removal walks the list from the start, because in a singly linked list you cannot step back from a node. The rule is short: remove at the head, add at the tail. In C, using the queue without calling create leaves random addresses in front and rear; always call create(&q) before using the queue.

5Try it

Why is front the head of the list in a linked-list queue?

Show the answer

The answer is A. In a singly linked list, deleting the last node means searching from the start for the node before it. When removal happens at the head, only the first node is touched. Insertion is O(1) at the tail thanks to rear.

In a linked-list queue with one element, which is true of front and rear?

Show the answer

The answer is B. A single element is both the first and the last to arrive. So front and rear point to the same node. Enqueue and dequeue handle this case separately.

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