0% found this document useful (0 votes)
4 views3 pages

Node.js Queue Data Structures Guide

The document provides an overview of various types of queues in data structures, including standard queues, circular queues, double-ended queues, and priority queues, along with their real-world applications and Node.js implementations. It also explains the Breadth First Search (BFS) algorithm and includes assignments for practical implementation. Each section includes example code to illustrate the concepts discussed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Node.js Queue Data Structures Guide

The document provides an overview of various types of queues in data structures, including standard queues, circular queues, double-ended queues, and priority queues, along with their real-world applications and Node.js implementations. It also explains the Breadth First Search (BFS) algorithm and includes assignments for practical implementation. Each section includes example code to illustrate the concepts discussed.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Queue Lecturer Notes with Node.

js Examples

1. Introduction to Queues
A Queue is a linear data structure that follows the FIFO (First In, First Out) principle. This means
that the first element inserted into the queue is the first one to be removed.
■ Real-world example: Ticket Booking Counter — people are served in the order they arrive.

[Link] Implementation:
class Queue {
constructor() {
[Link] = [];
}

enqueue(element) {
[Link](element);
}

dequeue() {
if ([Link]()) return 'Underflow';
return [Link]();
}

front() {
return [Link]() ? 'No elements' : [Link][0];
}

isEmpty() {
return [Link] === 0;
}
}

// Example usage
let q = new Queue();
[Link]("Person1");
[Link]("Person2");
[Link]([Link]()); // Person1

2. Circular Queue
A Circular Queue connects the end of the queue back to the front, forming a circle. This is useful for
memory optimization.
■ Real-world example: CPU scheduling, printer buffer.
class CircularQueue {
constructor(size) {
[Link] = new Array(size);
[Link] = size;
[Link] = -1;
[Link] = -1;
}

enqueue(element) {
if (([Link] + 1) % [Link] === [Link]) {
return 'Queue is Full';
}
if ([Link] === -1) [Link] = 0;
[Link] = ([Link] + 1) % [Link];
[Link][[Link]] = element;
}

dequeue() {
if ([Link] === -1) return 'Queue is Empty';
let element = [Link][[Link]];
if ([Link] === [Link]) {
[Link] = [Link] = -1;
} else {
[Link] = ([Link] + 1) % [Link];
}
return element;
}
}

// Example usage
let cq = new CircularQueue(3);
[Link](10);
[Link](20);
[Link]([Link]()); // 10

3. Double-Ended Queue (Deque)


A Deque allows insertion and deletion at both ends.
■ Real-world example: Browser history navigation (back and forward).
class Deque {
constructor() {
[Link] = [];
}

addFront(element) {
[Link](element);
}

addRear(element) {
[Link](element);
}

removeFront() {
return [Link]();
}

removeRear() {
return [Link]();
}
}

// Example usage
let dq = new Deque();
[Link]("Page1");
[Link]("Page2");
[Link]("Page0");
[Link]([Link]()); // Page2

4. Priority Queue
A Priority Queue assigns priority to elements. Higher priority elements are dequeued first.
■ Real-world example: Hospital emergency ward, ride-hailing app (VIP gets faster service).
class PriorityQueue {
constructor() {
[Link] = [];
}

enqueue(element, priority) {
let qElement = { element, priority };
let added = false;
for (let i = 0; i < [Link]; i++) {
if ([Link] < [Link][i].priority) {
[Link](i, 0, qElement);
added = true;
break;
}
}
if (!added) [Link](qElement);
}
dequeue() {
return [Link]();
}
}

// Example usage
let pq = new PriorityQueue();
[Link]("Patient A", 2);
[Link]("Patient B", 1);
[Link]([Link]()); // Patient B (higher priority)

5. BFS (Breadth First Search)


BFS is a graph traversal algorithm that explores neighbors level by level using a queue.
■ Real-world example: Finding the shortest path in maps.
function bfs(graph, start) {
let visited = new Set();
let queue = [start];

while ([Link] > 0) {


let node = [Link]();
if (![Link](node)) {
[Link](node);
[Link](node);
[Link](...graph[node]);
}
}
}

// Example usage
let graph = {
A: ["B", "C"],
B: ["D", "E"],
C: ["F"],
D: [],
E: ["F"],
F: []
};

bfs(graph, "A");
// Output: A B C D E F

6. Assignments
Q1. Implement a queue for a supermarket billing counter.
Q2. Implement a circular queue for printer spooler.
Q3. Implement a priority queue for hospital patients.
Q4. Implement BFS for friend suggestions in a social network.

■ Solutions for all assignments are provided in the examples above.

You might also like