Skip to content

Commit 8ba7f38

Browse files
committed
create Queue class
1 parent 32d912b commit 8ba7f38

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

DataStructures/Queue.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
export default class Queue {
2+
constructor() {
3+
this.queue = [];
4+
this.front = null;
5+
this.rear = null;
6+
}
7+
8+
enqueue(element) {
9+
this.queue.push(element);
10+
this.rear = element;
11+
this.front = this.queue[0];
12+
}
13+
14+
dequeue() {
15+
if (this.queue.length > 0) {
16+
if (this.queue.length === 1) {
17+
this.front = null;
18+
} else {
19+
this.front = this.queue[this.queue.length - 2];
20+
}
21+
return this.queue.shift();
22+
} else {
23+
throw new Error("Queue Underflow Exception: The queue is empty.");
24+
}
25+
}
26+
27+
getFront() {
28+
if (this.queue.length === 0) {
29+
return null;
30+
}
31+
32+
return this.front;
33+
}
34+
35+
getRear() {
36+
if (this.queue.length === 0) {
37+
return null;
38+
}
39+
40+
return this.Rear;
41+
}
42+
43+
size() {
44+
return this.queue.length;
45+
}
46+
47+
isEmpty() {
48+
return this.queue.length === 0;
49+
}
50+
}

0 commit comments

Comments
 (0)