按书上的来弄的。慢慢理解了。

function Queue() {
    var items = [];
    this.enqueue = function(element){
        items.push(element);
    }

    this.dequeue = function(){
        return items.shift();
    }

    this.front = function(){
        return items[0];
    }

    this.isEmpty = function(){
        return items.length == 0;
    }

    this.clear = function(){
        items = [];
    }

    this.size = function(){
        return items.length;
    }

    this.print = function(){
        console.log(items.toString());
    }
}

function PriorityQueue() {
    var items = [];

    function QueueElement(element, priority){
        this.element = element;
        this.priority = priority;
    }
    this.enqueue = function(element, priority){
        var queueElement = new QueueElement(element, priority);
        if (this.isEmpty()){
            items.push(queueElement);
        } else {
            var added = false;
            for (var i=0; i<items.length; i++){
                if (queueElement.priority < items[i].priority){
                    items.splice(i, 0, queueElement);
                    added = true;
                    break;
                }
            }
            if (!added){
                items.push(queueElement);
            }
        }
    }

    this.dequeue = function(){
        return items.shift();
    }

    this.front = function(){
        return items[0];
    }

    this.isEmpty = function(){
        return items.length == 0;
    }

    this.clear = function(){
        items = [];
    }

    this.size = function(){
        return items.length;
    }

    this.print = function(){
        console.log(items);
    }
}

var priorityQueue = new PriorityQueue();
priorityQueue.enqueue("John", 2);
priorityQueue.enqueue("Jack", 1);
priorityQueue.enqueue("Camila", 1);
priorityQueue.print();

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 + '在击鼓传花中被淘汰.');
    }
    return queue.dequeue();
}

var names = ['John','Jack','Camila','Ingrid','Carl'];
var winner = hotPotato(names, 7);
console.log('胜利者:' + winner);

最新文章

  1. css3之文本新属性
  2. ADO.NET基础03
  3. Nginx 下配置SSL证书的方法
  4. 【POJ】3150 Cellular Automaton(矩阵乘法+特殊的技巧)
  5. js继承实例
  6. Careercup - Facebook面试题 - 4922014007558144
  7. LeetCode 6. ZigZag Conversion Question
  8. hql中不能写count(1)能够写count(a.id)
  9. Android_AndroidStudio配置
  10. Delphi X10.2 + FireDAC 使用 SQL 语句 INSERT
  11. SpringCloud(1)服务注册与发现Eureka
  12. [POI2002][HAOI2007]反素数(Antiprime)
  13. Altium designer 新建快捷键
  14. 《Python神经网络编程》中文版PDF+英文版PDF+源代码,业界良心书
  15. FastJson、Jackson、Gson进行Java对象转换Json细节处理
  16. luogu3811 乘法逆元
  17. pythonのpygame初体验
  18. threejs linesegment的拾取实验
  19. supervisor 使用教程(转)
  20. jQuery表格排序(tablesorter)

热门文章

  1. Jenkins 搭建U3D自动发布 Android
  2. Color Space
  3. centos7中systemctl命令使用方法和心得体会
  4. jquery autocomplete 简单实用例子
  5. malloc 函数到底做了什么?
  6. &lt;转&gt;错误 x error LNK1104: 无法打开文件“E:\xxxx\Debug\xxxx.exe”
  7. 最新 DEDECMS SQL 注入 0day
  8. 探讨关于C#中Foreach的本质
  9. show processlist 其中status详解(适用于所有概况)
  10. HDU 4920 Matrix multiplication (硬件优化)