What Is a Queues in Data Structure?

//

Scott Campbell

In data structures, a queue is an abstract data type that follows the First-In-First-Out (FIFO) principle. It is a linear collection of elements where elements are added at one end and removed from the other end. Queues are widely used in computer science and can be implemented using arrays, linked lists, or other data structures.

Basic Operations on Queues

Queues support the following fundamental operations:

  • Enqueue: The process of adding an element to the queue is called enqueue. The new element is always added at the rear or tail of the queue.
  • Dequeue: The process of removing an element from the queue is called dequeue.

    The element that has been in the queue for the longest time is removed from the front or head of the queue.

  • Front: This operation returns the value of the front element without removing it from the queue.
  • Rear: This operation returns the value of the rear element without removing it from the queue.
  • IsEmpty: This operation checks whether the queue is empty or not. It returns true if there are no elements in the queue, otherwise false.

Real-Life Examples

The concept of queues can be related to real-life scenarios like waiting in line at a grocery store or a ticket counter. In both cases, people join at one end and leave from another end, following a first-come-first-serve approach.

Grocery Store Example

Say you are waiting in line at a grocery store. New customers arrive and join at one end (enqueue), while customers at the front of the line get their groceries and leave (dequeue). The customer who has been waiting the longest gets served first, maintaining the fairness of the queue.

Ticket Counter Example

Consider a ticket counter at a cinema hall. People arrive to buy tickets and join the queue.

The person at the front of the queue gets their ticket (dequeue), while new people join at the back of the line (enqueue). This ensures that everyone gets served in a systematic and organized manner.

Implementation

Queues can be implemented using various data structures. One common implementation is using an array. Another popular option is using a linked list, where each element contains a value and a reference to the next element in the queue.

Here’s an example of implementing a queue using an array:


class Queue {
  constructor() {
    this.queue = [];
  }
  
  enqueue(element) {
    this.queue.push(element);
  }
  
  dequeue() {
    if (!this.isEmpty()) {
      return this.shift();
    }
    return "Underflow";
  }
  
  front() {
    if (!this.queue[0];
    }
    return "Queue is empty";
  }
  
  rear() {
    if (!this.queue[this.length - 1];
    }
    return "Queue is empty";
  }
  
  isEmpty() {
    return this.length === 0;
  }
}

In conclusion, queues are an essential concept in computer science and are used in various applications. Understanding how queues work and their basic operations is crucial for designing efficient algorithms and data structures.

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

Privacy Policy