codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🧮

Data Structures & Algorithms

8 / 65 topics
7Stacks8Queues9Deques (Double-Ended Queues)10Priority Queues
Tutorials/Data Structures & Algorithms/Queues
🧮Data Structures & Algorithms

Queues

Updated 2026-05-15
10 min read

Queues

Introduction

In the world of computer science and programming, data structures are fundamental building blocks that help organize and manage data efficiently. One such essential data structure is the queue. A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle, meaning that the first element added to the queue will be the first one to be removed.

Queues are widely used in various applications, including scheduling tasks, managing resources, and handling requests in operating systems. Understanding queues is crucial for developing efficient algorithms and solving real-world problems.

Concept

A queue can be visualized as a line of people waiting at a checkout counter. The person who arrives first gets served first, and the last person to arrive waits until everyone else has been served. This FIFO behavior is what makes queues unique and useful in many scenarios.

Basic Operations

  1. Enqueue: Add an element to the end of the queue.
  2. Dequeue: Remove an element from the front of the queue.
  3. Peek/Front: Get the element at the front of the queue without removing it.
  4. IsEmpty: Check if the queue is empty.

Implementation

Queues can be implemented in various ways, but the most common methods are using arrays or linked lists. Here, we'll explore a simple implementation using an array.

Array-Based Queue

Let's implement a basic queue using an array in JavaScript:

JavaScript
1class Queue {
2constructor() {
3 this.items = [];
4}
5
6enqueue(element) {
7 this.items.push(element);
8}
9
10dequeue() {
11 if (this.isEmpty()) {
12 return "Underflow";
13 }
14 return this.items.shift();
15}
16
17front() {
18 if (this.isEmpty()) {
19 return "No elements in Queue";
20 }
21 return this.items[0];
22}
23
24isEmpty() {
25 return this.items.length === 0;
26}
27
28printQueue() {
29 let str = "";
30 for (let i = 0; i < this.items.length; i++) {
31 str += this.items[i] + " ";
32 }
33 return str;
34}
35}
36
37// Example usage:
38const queue = new Queue();
39queue.enqueue(1);
40queue.enqueue(2);
41queue.enqueue(3);
42console.log(queue.printQueue()); // Output: 1 2 3
43console.log(queue.dequeue()); // Output: 1
44console.log(queue.front()); // Output: 2
45console.log(queue.isEmpty()); // Output: false

Linked List-Based Queue

Using a linked list can be more efficient in terms of memory management, especially when dealing with large queues. Here's how you might implement a queue using a singly linked list:

JavaScript
1class Node {
2constructor(element) {
3 this.element = element;
4 this.next = null;
5}
6}
7
8class LinkedListQueue {
9constructor() {
10 this.head = null;
11 this.tail = null;
12 this.size = 0;
13}
14
15enqueue(element) {
16 const node = new Node(element);
17 if (this.isEmpty()) {
18 this.head = node;
19 this.tail = node;
20 } else {
21 this.tail.next = node;
22 this.tail = node;
23 }
24 this.size++;
25}
26
27dequeue() {
28 if (this.isEmpty()) {
29 return "Underflow";
30 }
31 const element = this.head.element;
32 this.head = this.head.next;
33 if (this.isEmpty()) {
34 this.tail = null;
35 }
36 this.size--;
37 return element;
38}
39
40front() {
41 if (this.isEmpty()) {
42 return "No elements in Queue";
43 }
44 return this.head.element;
45}
46
47isEmpty() {
48 return this.size === 0;
49}
50
51printQueue() {
52 let str = "";
53 let current = this.head;
54 while (current) {
55 str += current.element + " ";
56 current = current.next;
57 }
58 return str;
59}
60}
61
62// Example usage:
63const linkedListQueue = new LinkedListQueue();
64linkedListQueue.enqueue(1);
65linkedListQueue.enqueue(2);
66linkedListQueue.enqueue(3);
67console.log(linkedListQueue.printQueue()); // Output: 1 2 3
68console.log(linkedListQueue.dequeue()); // Output: 1
69console.log(linkedListQueue.front()); // Output: 2
70console.log(linkedListQueue.isEmpty()); // Output: false

Examples

Let's explore some practical examples to solidify our understanding of queues.

Example 1: Simulating a Printer Queue

Imagine you have a printer that can only print one document at a time. You need to manage the documents in the order they are submitted.

JavaScript
1class PrinterQueue {
2constructor() {
3 this.queue = new Queue();
4}
5
6addDocument(document) {
7 this.queue.enqueue(document);
8 console.log(`Added document: ${document}`);
9}
10
11printNextDocument() {
12 if (this.queue.isEmpty()) {
13 console.log("No documents to print.");
14 } else {
15 const document = this.queue.dequeue();
16 console.log(`Printing document: ${document}`);
17 }
18}
19}
20
21// Example usage:
22const printer = new PrinterQueue();
23printer.addDocument("Document1");
24printer.addDocument("Document2");
25printer.printNextDocument(); // Output: Printing document: Document1
26printer.printNextDocument(); // Output: Printing document: Document2

Example 2: Implementing a Circular Queue

A circular queue is a variation of the queue where the last element points back to the first, forming a circle. This can be useful in scenarios where you need to manage a fixed-size buffer.

JavaScript
1class CircularQueue {
2constructor(size) {
3 this.items = new Array(size);
4 this.front = -1;
5 this.rear = -1;
6 this.size = size;
7}
8
9enqueue(element) {
10 if ((this.rear + 1) % this.size === this.front) {
11 console.log("Queue is full");
12 return false;
13 } else if (this.isEmpty()) {
14 this.front = 0;
15 this.rear = 0;
16 } else {
17 this.rear = (this.rear + 1) % this.size;
18 }
19 this.items[this.rear] = element;
20 console.log(`Enqueued: ${element}`);
21 return true;
22}
23
24dequeue() {
25 if (this.isEmpty()) {
26 console.log("Queue is empty");
27 return false;
28 } else if (this.front === this.rear) {
29 const element = this.items[this.front];
30 this.front = -1;
31 this.rear = -1;
32 console.log(`Dequeued: ${element}`);
33 return true;
34 }
35 const element = this.items[this.front];
36 this.front = (this.front + 1) % this.size;
37 console.log(`Dequeued: ${element}`);
38 return true;
39}
40
41isEmpty() {
42 return this.front === -1;
43}
44}
45
46// Example usage:
47const circularQueue = new CircularQueue(3);
48circularQueue.enqueue(1); // Enqueued: 1
49circularQueue.enqueue(2); // Enqueued: 2
50circularQueue.enqueue(3); // Enqueued: 3
51circularQueue.enqueue(4); // Queue is full
52circularQueue.dequeue(); // Dequeued: 1
53circularQueue.enqueue(4); // Enqueued: 4

What's Next?

In the next section, we'll dive into another fundamental data structure called Hash Tables. Hash tables provide efficient ways to store and retrieve data using keys, making them essential for many applications.


PreviousStacksNext Deques (Double-Ended Queues)

Recommended Gear

StacksDeques (Double-Ended Queues)