Add elements to a PriorityQueue using Javascript

Enqueuing elements to a PriorityQueue means adding them in the array in order of the priority of the element. We'll consider higher numbers to be higher priorities. We'll loop through the container till we find a lower priority and then add the element there. If not, then we'll push it at the end of the container.

Note that we're creating the element object with the data and priority. Hence we can implement the enqueue function as follows:

The enqueue() Method

enqueue(data, priority) {
    // Check if Queue is full
    if (this.isFull()) {
        console.log("Queue Overflow!");
        return;
    }
    let currElem = new this.Element(data, priority);
    let addedFlag = false;
    // Since we want to add elements to end, we'll just push them.
    for(let i = 0; i < this.container.length; i ++) {
        if(currElem.priority < this.container[i].priority) {
            this.container.splice(i, 0, currElem);
            addedFlag = true; 
            break;
        }
    }
    if (!addedFlag) {
        this.container.push(currElem);
    }
}

Complete PriorityQueue Implementation

class PriorityQueue {
    constructor(capacity) {
        this.container = [];
        this.capacity = capacity;
    }
    
    // Element class to store data and priority
    Element = class {
        constructor(data, priority) {
            this.data = data;
            this.priority = priority;
        }
    }
    
    isFull() {
        return this.container.length >= this.capacity;
    }
    
    enqueue(data, priority) {
        // Check if Queue is full
        if (this.isFull()) {
            console.log("Queue Overflow!");
            return;
        }
        let currElem = new this.Element(data, priority);
        let addedFlag = false;
        
        // Find correct position to insert based on priority
        for(let i = 0; i < this.container.length; i ++) {
            if(currElem.priority < this.container[i].priority) {
                this.container.splice(i, 0, currElem);
                addedFlag = true; 
                break;
            }
        }
        
        // If no position found, add at the end
        if (!addedFlag) {
            this.container.push(currElem);
        }
    }
    
    display() {
        console.log(this.container);
    }
}

// Test the PriorityQueue
let q = new PriorityQueue(4);
q.enqueue("Hello", 3);
q.enqueue("World", 2);
q.enqueue("Foo", 8);
q.display();
[ { data: 'World', priority: 2 },
  { data: 'Hello', priority: 3 },
  { data: 'Foo', priority: 8 } ]

How It Works

The enqueue function works like insertion sort's insertions:

  • It creates a new element with the given data and priority
  • Iterates through existing elements to find the correct position
  • Inserts the element where its priority is higher than the next element
  • If no such position is found, adds it to the end
  • Lower priority numbers are placed first in the queue

Testing with Different Priorities

let priorityQ = new PriorityQueue(5);

// Add elements with different priorities
priorityQ.enqueue("Low Priority Task", 1);
priorityQ.enqueue("High Priority Task", 5);
priorityQ.enqueue("Medium Priority Task", 3);
priorityQ.enqueue("Critical Task", 1);

console.log("Queue after adding elements:");
priorityQ.display();
Queue after adding elements:
[ { data: 'Low Priority Task', priority: 1 },
  { data: 'Critical Task', priority: 1 },
  { data: 'Medium Priority Task', priority: 3 },
  { data: 'High Priority Task', priority: 5 } ]

Conclusion

The enqueue method maintains sorted order by inserting elements at their correct priority position. Elements with lower priority numbers are processed first, making this suitable for task scheduling systems.

Updated on: 2026-03-15T23:18:59+05:30

268 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements