function Queue() { this.dataStore = []; this.enqueue = enqueue; this.dequeue = dequeue; this.front = front; this.back = back; this.toString = toString; this.empty = empty; } function enqueue(element) {//向队尾添加一个元素 this.dataStore.push(element); } function dequeue() {//删除队首的元素 return this.dataStore.shift(); } function front() {//读取队首元素 return this.dataStore[0]; } function back() {//读取队尾元素 return this.dataStore[this.dataStore.length - 1]; } function toString() { var retStr = ""; for ( var i = 0; i < this.dataStore.length; ++i) { retStr += this.dataStore[i] + " "; } return retStr; } function empty() {//判断队列是否为空 if (this.dataStore.length == 0) { return true; } else { return false; } } var q = new Queue(); q.enqueue("Meredith"); q.enqueue("Cynthia"); q.enqueue("Jennifer"); document.write(q.toString() + "<br />"); q.dequeue(); document.write(q.toString() + "<br />"); document.write("Front of queue: " + q.front() + "<br />"); document.write("Back of queue: " + q.back() + "<br />"); /*测试结果: Meredith Cynthia Jennifer Cynthia Jennifer Front of queue: Cynthia Back of queue: Jennifer */
原文:http://www.cnblogs.com/feile/p/5372872.html