-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.js
More file actions
75 lines (63 loc) · 1.72 KB
/
queue.js
File metadata and controls
75 lines (63 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class Patient {
constructor(id, name, age, phone) {
this.id = id;
this.name = name;
this.age = age;
this.phone = phone;
}
}
//i have defined the schema of the patient
//Logic for the circularQueue
class CircularQueue {
constructor(size) {
this.size = size;
this.queue = new Array(size);
this.front = -1;
this.rear = -1;
}
isFull() {
return (this.front === (this.rear + 1) % this.size);
}
isEmpty() {
return this.front === -1;
}
enqueue(patient) {
if (this.isFull()) {
console.log("Queue is full!");
return false;
}
if (this.isEmpty()) {
this.front = 0;
}
this.rear = (this.rear + 1) % this.size;
this.queue[this.rear] = patient;
return true;
}
dequeue() {
if (this.isEmpty()) {
console.log("Queue is empty!");
return null;
}
const removed = this.queue[this.front];
this.queue[this.front] = null; // Clear slot
if (this.front === this.rear) {
// Only one element was present
this.front = this.rear = -1;
} else {
this.front = (this.front + 1) % this.size;
}
return removed;
}
getQueue() {
if (this.isEmpty()) return [];
let result = [];
let i = this.front;
while (true) {
if (this.queue[i]) result.push(this.queue[i]);
if (i === this.rear) break;
i = (i + 1) % this.size;
}
return result;
}
}
module.exports = { CircularQueue, Patient };