What Is Queue in Data Structure?

//

Angela Bailey

What Is Queue in Data Structure?

A queue is a fundamental data structure in computer science that follows the principle of FIFO (First-In-First-Out). It is an ordered collection of elements in which an element is inserted at one end, known as the rear, and removed from the other end, known as the front.

This makes a queue an ideal choice when we want to process elements in the same order they were added.

The Queue Operations

There are two primary operations performed on a queue:

  • Enqueue: This operation adds an element to the rear of the queue. The newly added element becomes the last element in the queue.
  • Dequeue: This operation removes and returns the front element from the queue. After this operation, the second element becomes the new front.

The Queue Implementation

Queues can be implemented using various data structures such as arrays or linked lists. However, linked lists are preferred for dynamic implementation since they allow easy insertion and deletion at both ends.

A Simple Queue Example using Linked List:

To illustrate how a queue works, let’s consider an example of a queue implemented using a linked list:

<pre>
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

class Queue {
  constructor() {
    this.front = null;
    this.rear = null;
  }

  enqueue(data) {
    const newNode = new Node(data);

    if (!this.front) {
      this.front = newNode;
      this.rear = newNode;
    } else {
      this.rear.next = newNode;
      this.rear = newNode;
    }
  }

  dequeue() {
    if (!this.front) {
      return null;
    }

    const removedNode = this.front;
    this.front = this.front.next;

    if (!this.rear = null;
    }

    return removedNode.data;
  }
}

const queue = new Queue();

queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);

console.log(queue.dequeue()); // Output: 10
</pre>

In the above example, we implemented a simple queue using a linked list. The enqueue() function adds elements to the rear of the queue, and the dequeue() function removes and returns elements from the front of the queue.

Common Applications of Queues

Queues find applications in various scenarios where processing elements in a specific order is essential. Some common examples include:

  • Operating Systems: Queues are used for process scheduling, managing I/O requests, and handling interrupts.
  • Data Structures: Queues serve as building blocks for other data structures like stacks and trees.
  • Networks: Queues are used for managing network packets and handling network congestion.

In Conclusion

A queue is an important data structure that follows the FIFO principle. It allows efficient insertion at one end and deletion from the other end.

Understanding queues and their operations is crucial for solving various computer science problems efficiently.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy