-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.js
More file actions
41 lines (34 loc) · 711 Bytes
/
queue.js
File metadata and controls
41 lines (34 loc) · 711 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
class Queue{
constructor(size){
this.item=new Array(size);
this.size=size;
this.length=0
this.rear=-1;
this.front=-1
}
insert(data){
if(this.length<this.size){
this.item.push(data)
this.length++;
this.rear=(this.rear+1)%this.size;
if(this.front===-1){
this.front=this.rear
}
}
}
remove(){
this.item.shift();
this.front=(this.front+1)%this.size;
this.length--;
}
}
const q=new Queue(5);
q.insert(10);
q.insert(20);
q.insert(30);
q.insert(40);
q.insert(10);
q.insert(20);
q.insert(30);
q.insert(40);
console.log(q.item)