5.3Queue with a Linked List
Creating a linked-list queue
An array queue works with a fixed capacity. A queue built on a linked list allocates a separate node 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.
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: enqueue adds at the back and dequeue removes from the front. So the structure keeps two pointers: 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 O(1), but deleting from the end is O(n), because the node before the last one is found by walking 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 NULL. 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 enqueue 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.
createwrites NULL intofrontandrear. There are no nodes yet; this is the empty queue.isEmptychecks whetherfrontis NULL. Right now it is, so the result is true.- The state after three enqueue calls. 10, which came first, is at the head and
frontpoints to it. 30, which came last, is at the tail andrearpoints to it. - It is 10's turn: it leaves first. A new element will be added after 30. One end handles removal, the other handles insertion.
isEmptynow returns false, becausefrontpoints 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 NoneThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
Each function does one or two assignments or a single comparison.
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?
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.
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?
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.
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.