queue.js
588 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
"use strict";
function Queue (){
this.arr = new Array();
this.leng = 0;
this.push = push;
this.pop = pop;
}
function push(x) {
this.arr[this.leng++] = x;
}
function pop (){
var ret;
ret = this.arr[0];
for (var i = 0 ; i < this.leng; i++){
this.arr[i] = this.arr[i+1];
}
this.leng--;
return ret;
}
var queue = new Queue();
queue.push(1);
queue.push(2);
queue.push(3);
queue.push(4);
queue.push(5);
console.log(queue.pop());
console.log(queue.pop());
console.log(queue.pop());
console.log(queue.pop());
console.log(queue.pop());
console.log(queue.pop());