Skip to content

Commit 5ac6420

Browse files
authored
Merge pull request #14 from num-dev/numdev-queue
Circular-Queue with Class
2 parents cae6e7f + 3804d20 commit 5ac6420

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<title>Queue</title>
9+
</head>
10+
11+
<body>
12+
<div align="center">
13+
<h3>Circular-Queue with Class</h3>
14+
<p>(Open console to check output)</p>
15+
</div>
16+
17+
<script>
18+
class Queue {
19+
constructor(size) {
20+
this.maxSize = size;
21+
this.items = new Array(size);
22+
this.currentIndex = 0;
23+
this.rear = -1;
24+
this.front = -1;
25+
}
26+
27+
isEmpty() {
28+
return this.currentIndex <= 0 ? true : false;
29+
}
30+
31+
isFull() {
32+
return this.currentIndex == this.maxSize ? true : false;
33+
}
34+
35+
enQueue(newVal) {
36+
if (this.isFull()) {
37+
console.error("Queue is Full");
38+
return false;
39+
}
40+
41+
if (this.rear == this.maxSize - 1) {
42+
this.rear = 0;
43+
} else {
44+
this.rear++;
45+
}
46+
47+
this.items[this.rear] = newVal;
48+
this.currentIndex++;
49+
50+
if (this.front == -1) {
51+
this.front = this.rear;
52+
}
53+
}
54+
55+
deQueue() {
56+
if (this.isEmpty()) {
57+
console.error("Queue is Empty");
58+
return false;
59+
}
60+
61+
this.items[this.front] = null;
62+
if (this.front == this.maxSize - 1) {
63+
this.front = 0;
64+
} else {
65+
this.front++;
66+
}
67+
this.currentIndex--;
68+
}
69+
}
70+
71+
let queue1 = new Queue(4);
72+
queue1.enQueue(50);
73+
queue1.enQueue(10);
74+
queue1.enQueue(20);
75+
console.log(queue1);
76+
</script>
77+
</body>
78+
79+
</html>

0 commit comments

Comments
 (0)