-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleQueueWithSize.js
More file actions
62 lines (48 loc) · 1.07 KB
/
SimpleQueueWithSize.js
File metadata and controls
62 lines (48 loc) · 1.07 KB
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
53
54
55
56
57
58
59
60
61
62
function SimpleQueue(){
this.length=0;
}
SimpleQueue.prototype.push=function(e){
this.tail=this.length++?this.tail[1]=[e,null]:this.head=[e,null];
}
SimpleQueue.prototype.shift=function(){
return this.length?[this.head[0],this.head=this.head[1],this.length--][0]:null;
}
SimpleQueue.prototype.clear=function(){
this.head=this.tail=null;
this.length=0;
}
SimpleQueue.prototype.isEmpty=function(){
return this.length==0;
}
SimpleQueue.prototype.size=function(){
return this.length;
}
//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");
*/