-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleQueue.js
52 lines (43 loc) · 968 Bytes
/
SimpleQueue.js
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
52
function SimpleQueue(){
}
SimpleQueue.prototype.push=function(e){
this.tail=this.head?this.tail[1]=[e,null]:this.head=[e,null];
}
SimpleQueue.prototype.shift=function(){
return this.head?[this.head[0],this.head=this.head[1]][0]:null;
}
SimpleQueue.prototype.clear=function(){
this.head=this.tail=null;
}
SimpleQueue.prototype.isEmpty=function(){
return !this.head;
}
//if you want to use it as nodejs module
//module.exports=SimpleQueue;
/*
//create
var queue=new SimpleQueue();
//push/add/enqueue
for(i=0;i<5;i++){
queue.push(i);
}
//isEmpty
console.log(queue.isEmpty());
//shift/poll/dequeue
for(;!queue.isEmpty();){
console.log(queue.shift());
}
console.log(queue.isEmpty());
//clear
queue.clear();
//benchmark
for(i=0;i<1024*1024;i++){
queue.push(i);
}
var st=new Date().getTime();
for(i=0;i<1024*1024;i++){
queue.push(i);
queue.shift();
}
console.log("cost "+(new Date().getTime()-st)+" ms for "+1024*1024+" ops");
*/