-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathQueue.js
More file actions
50 lines (42 loc) · 859 Bytes
/
Queue.js
File metadata and controls
50 lines (42 loc) · 859 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
38
39
40
41
42
43
44
45
46
47
48
49
50
export default class Queue {
constructor() {
this.queue = [];
this.front = null;
this.rear = null;
}
enqueue(element) {
this.queue.push(element);
this.rear = element;
this.front = this.queue[0];
}
dequeue() {
if (this.queue.length > 0) {
if (this.queue.length === 1) {
this.front = null;
} else {
this.front = this.queue[this.queue.length - 2];
}
return this.queue.shift();
} else {
throw new Error("Queue Underflow Exception: The queue is empty.");
}
}
getFront() {
if (this.queue.length === 0) {
return null;
}
return this.front;
}
getRear() {
if (this.queue.length === 0) {
return null;
}
return this.Rear;
}
size() {
return this.queue.length;
}
isEmpty() {
return this.queue.length === 0;
}
}