# Queue

## What is the Queue?

> *A Queue is a linear structure which follows a particular order in which the operations are performed. The order is **FIFO**(First In First Out)* -geeksforgeeks.org

*A real-world example of a queue can be people standing at the bus stop where the first standing in the line will be the first person to get out of the line i.e. first in first out. If you compared it to a* [***stack***](https://dev.to/swarup260/data-structures-algorithms-in-javascript-stack-1ilb)*, the last person will be the first to leave.*

This article will go through a list of following Queue DS,

* Queue.
* Deque(Double-ended queue).

## List Of Operations Available

* **Enqueue**   : Insert an element at the end of the queue.
* **Dequeue**    : Remove an element from the front of the queue.&#x20;
* **Front**    : Return the first element of the queue.
* **Size**    : Return Size of the queue.
* **isEmpty** : Check if the queue is empty if empty return true else false.
* **Clear**   : Reset the queue.

### Implementation of Queue in Javascript

let's define **ES6** class name ***Queue***, with properties, *count* which will keep track of the number of elements in the queue and *items* object which will store the elements. since we will be removing elements from the front of the queue, we also need a variable to help us track the first element. For this purpose, we declare the *lowestCount* variable

```javascript
class Queue {
    constructor() {
        this.count = 0;
        this.lowestCount = 0;
        this.items = {};
    }
}
```

#### **Enqueue**

Insert an element in the queue is same as the [push](https://dev.to/swarup260/data-structures-algorithms-in-javascript-stack-1ilb#push) method of the stack or as Array *push* method.

```javascript
 enqueue(element){
         this.items[this.count] = element;
         this.count ++;
     }
```

#### **Dequeue**

To remove an element from the **Queue**, we first check if the Queue is empty if empty return *undefined* else store the *lowestCount* property element in a variable, To return the element after deletion, Delete the *lowestCount* item & increment the count by one. Dequeue method is same as the Array *shift* method which removes the first element.

```javascript
   dequeue(){
         if (this.isEmpty()) {
             return undefined;
         }
         let result = this.items[this.lowestCount]; 
         delete this.items[this.lowestCount]; 
         this.lowestCount ++; 
         return result; 

     }
```

#### **Front**

This method will return the item from the front of the queue (using the *lowestCount* as a key to retrieve the element value)

```javascript
   front(){
         if (this.isEmpty()) {
             return undefined;
         }
         return this.items[this.lowestCount];

     }
```

#### **Size**

This method will return the size of the queue which is *count* minus the *lowestCount*.

```javascript
size() {
        return this.count - this.lowestCount;
    }
```

Example:-In the below queue items object, If the zeroth element was removed from the front of the queue, the lowest count will be one. The total count of the element will be two, therefore, the size of the queue will count-lowest count

```javascript
let queue = {
   1: "1",
   2: "2",
}
```

#### **isEmpty**

**isEmpty** will return true if the queue is empty.

```javascript
 isEmpty() {
         return this.size() === 0;
    }
```

#### **Clear**

To **clear** all the elements from the queue, we can evoke the dequeue method until it returns undefined or we can simply reset the value of the Queue class properties to the same values as declared in its constructor

```javascript
 clear() {
    this.items = {}
    this.count = 0;
    this.lowestCount = 0;
    return this.items;
    }
```

you get the full source [here](https://github.com/swarup260/Learning_Algorithms/blob/master/data_structure/Queue.js)

### Conclusion :

| Methods | Complexity |
| ------- | :--------: |
| equeue  |    O(1)    |
| dequeue |    O(1)    |
| front   |    O(1)    |
| size    |    O(1)    |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://swarup260.gitbook.io/learning-algorithms/queue.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
