Queue
Data Structure · Starter
Step through it: Breadth-First Search pattern
queue: 7
Step 1 of 5
Goal: find the fewest steps from the start to everything else. Breadth-first search explores in rings: everything one step away, then everything two steps away. It uses a queue, a line where the first one in is the first one out.
Step 2 of 5
Take 7 out and add its children to the back of the line. Both are one step from the start.
Step 3 of 5
Take 9 out and add its children. Take 11 out: nothing to add. 5 and 2 are two steps away.
Step 4 of 5
Nodes are visited ring by ring, so the first time you reach a node is by the shortest route. That is why breadth-first search finds shortest paths when every step costs the same.
Step 5 of 5
Your turn.
In computer science, a queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principle (or only) operations on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed. Often a peek or front operation is also entered, returning the value of the front element without dequeuing it. A queue is an example of a linear data structure, or more abstractly a sequential collection.
Representation of a FIFO (first in, first out) queue

Made with okso.app
Code
import LinkedList from '../linked-list/LinkedList';
export default class Queue {
constructor() {
// We're going to implement Queue based on LinkedList since the two
// structures are quite similar. Namely, they both operate mostly on
// the elements at the beginning and the end. Compare enqueue/dequeue
// operations of Queue with append/deleteHead operations of LinkedList.
this.linkedList = new LinkedList();
}
/**
* @return {boolean}
*/
isEmpty() {
return !this.linkedList.head;
}
/**
* Read the element at the front of the queue without removing it.
* @return {*}
*/
peek() {
if (this.isEmpty()) {
return null;
}
return this.linkedList.head.value;
}
/**
* Add a new element to the end of the queue (the tail of the linked list).
* This element will be processed after all elements ahead of it.
* @param {*} value
*/
enqueue(value) {
this.linkedList.append(value);
}
/**
* Remove the element at the front of the queue (the head of the linked list).
* If the queue is empty, return null.
* @return {*}
*/
dequeue() {
const removedHead = this.linkedList.deleteHead();
return removedHead ? removedHead.value : null;
}
/**
* @param [callback]
* @return {string}
*/
toString(callback) {
// Return string representation of the queue's linked list.
return this.linkedList.toString(callback);
}
}References
Practice
Breadth-First Search
- Minimum Depth of Binary TreeEasy
- Average of Levels in Binary TreeEasy
- Binary Tree Level Order TraversalMedium
- Binary Tree Zigzag Level Order TraversalMedium
- Binary Tree Right Side ViewMedium
- Populating Next Right Pointers in Each NodeMedium
- Rotting OrangesMedium
- Shortest Path in Binary MatrixMedium
- Open the LockMedium
- Walls and GatesMedium
- 01 MatrixMedium
- Perfect SquaresMedium
- Word LadderMedium
- Jump Game IIIMedium
- Nearest Exit from Entrance in MazeMedium
- Snakes and LaddersMedium
- Word Ladder IIHard
- Shortest Path Visiting All NodesHard
Spotted a mistake or something unclear? Report it on GitHub.