chesergon / Ques

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Overview A queue is a fundamental data structure that follows the First In, First Out (FIFO) principle. In a queue, elements are added (enqueue) at the rear and removed (dequeue) from the front. It's analogous to a line of people waiting for a service where the person who joined the line first gets served first.

Features FIFO Order: Elements are processed in the order they are added, maintaining a strict first-in, first-out order.

Operations:

Enqueue: Adds an element to the rear of the queue. Dequeue: Removes and returns the element from the front of the queue. Front: Returns the element at the front without removing it. isEmpty: Checks if the queue is empty. Use Cases:

Task scheduling in operating systems. Print queue in printers. Breadth-first search (BFS) in graph algorithms. Implementation:

Implemented using arrays or linked lists in JavaScript. Arrays provide a straightforward implementation with methods like push and shift. Linked lists offer dynamic size handling but require more complex node management. // Example usage of a queue using an array-based implementation class Queue { constructor() { this.elements = []; // Initialize an empty array }

enqueue(element) {
    this.elements.push(element); // Add element to the rear of the queue
}

dequeue() {
    if (this.isEmpty()) {
        return "Underflow"; // Returns Underflow if queue is empty
    }
    return this.elements.shift(); // Remove and return element from the front
}

front() {
    if (this.isEmpty()) {
        return "No elements in Queue"; // Returns No elements if queue is empty
    }
    return this.elements[0]; // Returns the element at the front
}

isEmpty() {
    return this.elements.length === 0; // Check if queue is empty
}

}

// Example usage: let queue = new Queue(); queue.enqueue(10); queue.enqueue(20); queue.enqueue(30);

console.log(queue.dequeue()); // Output: 10 console.log(queue.front()); // Output: 20 console.log(queue.isEmpty()); // Output: false This README.md provides a concise overview of queues, their operations, implementations in JavaScript, example usage, and considerations for choosing between different implementations. Adjustments can be made to add more specific details or expand on particular aspects based on the intended audience or requirements.

About


Languages

Language:JavaScript 100.0%