首页 > 编程语言 > 详细

JavaScript LoopQueue

时间:2016-01-13 00:25:30      阅读:214      评论:0      收藏:0      [点我收藏+]
function Queue() {
var items = [];
this.enqueue = function(element) {
items.push(element)
}
this.dequeue = function(element) {
return items.shift()
}
this.front = function() {
return items[0]
}
this.isEmpty = function() {
return items.length == 0
}
this.size = function() {
return items.length
}
this.printf = function() {
console.log(items.toString())
}
this.print = function() {
console.log(items.toString())
}
}
function hotPotato(nameList, num) {
var queue = new Queue();
for (var i = 0; i < nameList.length; i++) {
queue.enqueue(nameList[i])
}
var eliminated = ‘‘;
while (queue.size() > 1) {
for (var i = 0; i < num; i++) {
queue.enqueue(queue.dequeue())
}
eliminated = queue.dequeue();
console.log(eliminated+ ‘  go ‘) 
}
return queue.dequeue()
}
var names = [‘shidengyun‘,‘zhujing‘,‘shidengxia‘];
var winner = hotPotato(names, 7);
console.log(‘winner  ‘ + winner);

JavaScript LoopQueue

原文:http://www.cnblogs.com/shidengyun/p/5125902.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!