码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript LoopQueue

时间:2016-01-13 00:26:31      阅读:242      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!