-
Notifications
You must be signed in to change notification settings - Fork 52
/
queue_circular.html
51 lines (49 loc) · 1.23 KB
/
queue_circular.html
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
<html>
<head>
<title>Circular Queue in Javascript</title>
<script>
class Queue {
constructor(size) {
this.max = size;
this.items = new Array(size);
this.currentSize = 0;
this.rear = -1;
this.front = -1;
}
enqueue(val) {
if (this.currentSize != this.max) {
if (this.rear == this.max - 1) {
this.rear = 0;
} else {
this.rear++;
}
this.items[this.rear] = val;
this.currentSize++;
if ((this.front == -1)) {
this.front = this.rear;
}
}
}
dequeue(){
if(this.currentSize!=0){
this.items[this.front]=null;
if(this.front==this.max-1){
this.front=0;
}else{
this.front++;
}
this.currentSize--;
}else{
this.front=-1;
this.rear=-1;
alert("queue is empty")
}
}
}
let queue = new Queue(5);
</script>
</head>
<body>
<h1>Circular Queue in Javascript</h1>
</body>
</html>