Thursday, 19 December 2024

Building a Simple Queue in JavaScript Using Array Functions

A queue is a linear data structure that follows the First In, First Out (FIFO) principle. This means that the first element added to the queue will be the first one to be removed. Queues are commonly used in scenarios where order matters, such as in task scheduling, handling requests in web servers, or managing processes in operating systems.

Key Operations of a Queue

1.   Enqueue: Add an element to the end of the queue.

2.   Dequeue: Remove and return the element from the front of the queue.

3.   Peek: Look at the element at the front of the queue without removing it.

4.   IsEmpty: Check if the queue is empty.

5.   Size: Get the number of elements in the queue.

 

Advantages of Using a Queue

1.   Order Maintenance: Ensures that the elements are processed in the exact order they were added.

2.   Simplicity: The FIFO behaviour of a queue makes it easy to manage tasks that need to be processed in a specific order.

3.   Efficiency: Queues are efficient in managing tasks like scheduling and buffering.

 

Implementing a Queue in JavaScript Using shift() and push()

In JavaScript, arrays can be used to implement a queue. The push() method is used to add an element to the end of the array, and the shift() method is used to remove an element from the front of the array. This mimics the behaviour of a queue.

 

queue.js

// Enqueue function: Adds an element to the end of the queue
function enqueue(queue, element) {
    queue.push(element);
}

// Dequeue function: Removes and returns the element at the front of the queue
function dequeue(queue) {
    if (isEmpty(queue)) {
        return "Queue is empty";
    }
    return queue.shift();
}

// Peek function: Returns the element at the front of the queue without removing it
function peek(queue) {
    if (isEmpty(queue)) {
        return "Queue is empty";
    }
    return queue[0];
}

// IsEmpty function: Checks if the queue is empty
function isEmpty(queue) {
    return queue.length === 0;
}

// Size function: Returns the number of elements in the queue
function size(queue) {
    return queue.length;
}

// PrintQueue function: Returns a string representation of the queue
function printQueue(queue) {
    return 'Elements in Queue are: ' + queue.toString();
}

// Example usage
let myQueue = [];

console.log(`Is Queue empty?: ${isEmpty(myQueue)}`);

console.log('Add 10, 20 and 30 to the Queue')
enqueue(myQueue, 10);
enqueue(myQueue, 20);
enqueue(myQueue, 30);

console.log(printQueue(myQueue));

console.log(`Removing one element from the Queue ${dequeue(myQueue)}`);
console.log(`Top element of the Queue: ${peek(myQueue)}`);

console.log(`Size of the Queue: ${size(myQueue)}`);
console.log(`Is Queue empty?: ${isEmpty(myQueue)}`);

console.log(printQueue(myQueue));

Output

Is Queue empty?: true
Add 10, 20 and 30 to the Queue
Elements in Queue are: 10,20,30
Removing one element from the Queue 10
Top element of the Queue: 20
Size of the Queue: 2
Is Queue empty?: false
Elements in Queue are: 20,30

Explanation of the Functions

enqueue(queue, element): Adds an element to the end of the queue. ‘enqueue’ function uses the push() method to add the element to the end of the queue array.

 

dequeue(queue): Removes and returns the element at the front of the queue. ‘dequeue’ function Uses the shift() method to remove and return the first element in the queue array. If the queue is empty, it returns a message "Queue is empty."

 

peek(queue): This function returns the element at the front of the queue without removing it. It just returns the first element in the queue array. If the queue is empty, it returns a message "Queue is empty."

 

isEmpty(queue): Checks whether the queue is empty or not. It returns true if the queue array has no elements, otherwise, it returns false.

 

size(queue): Returns the number of elements in the queue. It just returns the length of the queue array.

 

printQueue(queue): It prints the elements of queue.


  

Previous                                                    Next                                                    Home

No comments:

Post a Comment