-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcircular-queue-array.js
More file actions
37 lines (30 loc) · 891 Bytes
/
circular-queue-array.js
File metadata and controls
37 lines (30 loc) · 891 Bytes
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
const QueueFullException = "QueueFullException";
const QueueEmptyException = "QueueEmptyException";
class Queue {
constructor(size) {
this.front = 0;
this.rear = 0;
this.data = new Array(size);
}
}
Queue.prototype.enqueue = function(item) {
if (this.isFull()) throw QueueFullException;
this.data[this.rear] = item;
this.rear = (this.rear + 1) % this.data.length;
};
Queue.prototype.dequeue = function() {
if (this.isEmpty()) throw QueueEmptyException;
delete this.data[this.front];
this.front = (this.front + 1) % this.data.length;
};
Queue.prototype.isEmpty = function() {
return this.front === this.rear;
};
Queue.prototype.isFull = function() {
return (this.rear + 1) % this.data.length === this.front;
}
Queue.prototype.front = function() {
if (this.isEmpty()) throw QueueEmptyException;
return this.data[this.front];
}
module.exports = Queue;