1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>JavaScript实现队列</title> 6 </head> 7 <body> 8 <script> 9 function Queue(){ 10 this.arr = []; 11 this.enqueue = enqueue; 12 this.dequeue = dequeue; 13 this.front = front; 14 this.back = back; 15 this.toString = toString; 16 this.empty = empty; 17 } 18 function enqueue(ele){ 19 this.arr.push(ele); 20 } 21 function dequeue(){ 22 return this.arr.shift(); 23 } 24 25 function front(){ 26 return this.arr[0]; 27 } 28 function back(){ 29 return this.arr[this.arr.length-1]; 30 } 31 function toString(){ 32 var retstr = ''; 33 for(var i=0;i<this.arr.length;i++){ 34 retstr += this.arr[i] +" "; 35 } 36 return retstr; 37 } 38 39 function empty(){ 40 return this.arr.length == 0 41 } 42 43 44 45 //测试 46 var q = new Queue(); 47 q.enqueue("zhangsan"); 48 q.enqueue("lisi"); 49 q.enqueue("zhaowu"); 50 q.enqueue("wangliu"); 51 console.log(q.toString()); 52 console.log(q.dequeue()); 53 console.log(q.toString()); 54 console.log(q.empty()); 55 56 57 58 59 </script> 60 </body> 61 </html>