The encapsulation of the queue by the javscript data structure, the problem of drumming and passing flowers, and the encapsulation of the priority queue

The encapsulation of the queue by the javscript data structure, the problem of drumming and passing flowers

/* 队列是一种受限的线性表达式,先进先出FIFO
    1.受限之处在于它只允许在表的前端进行删除操作
    2.在表的后端进行插入操作
*/


//队列封装  基于数组
function Queue(){
    
    
    // 属性
    this.items = [];
    // 方法
    // enqueue(element)在队列的尾部添加一个或者多个新的项
    Queue.prototype.enqueue = function(element){
    
    
        this.items.push(element);
    }
    // dequeue  移除队列的第一项 并返回被删除的元素
    Queue.prototype.dequeue = function(){
    
    
        return this.items.shift()
    }
    // front()  返回队首  不影响原队列
    Queue.prototype.front = function(){
    
    
        return this.items[0]
    }
    //isEmpty() 判断队列是否为空
    Queue.prototype.isEmpty = function(){
    
    
        return this.items.length == 0
    }
    // size()
    Queue.prototype.size = function() {
    
     
        return this.items.length
    }
    // toString() 将队列转成字符串
    Queue.prototype.toString = function(){
    
    
        return this.items.join(" ")
    }
}


// 测试
// var queue = new Queue();

// // 将元素加入到队列中
// queue.enqueue('1aaaa')
// queue.enqueue('2aaaa')
// queue.enqueue('3aaaa')
// queue.dequeue()
// console.log(queue.isEmpty());
// console.log(queue.toString());

// 击鼓传花

function passgame(nums,e){
    
    
    // 创建一个队列
    var queue = new Queue();
    // 将数组中的数据加入队列
    for(var i=0;i<nums.length;i++){
    
    
        queue.enqueue(nums[i]);
    }
    // 开始计数
    while(queue.size() > 1){
    
    
        for(var i=0;i<e-1;i++){
    
    
            queue.enqueue(queue.dequeue())
        }
        queue.dequeue();
    }
    var end = queue.front()
    
    return nums.indexOf(end)


}

// 测试击鼓传花
console.log(passgame([1,2,3,4,5,6,7,8,9],5));


priority queue

// 优先级队列封装

function Priority(){
    
    
    // 内部类
    function QueueElement(element,priority){
    
    
        this.element = element;
        this.priority = priority;
    }

    // 封装属性
    this.items = []
    // 实现插入
    Priority.prototype.enqueue = function(element,priority){
    
    
        var quueElement = new QueueElement(element,priority);
        if(this.items.length == 0){
    
    
            this.items.push(quueElement);
        }else{
    
    
            var  added = false
            for(var i = 0; i < this.items.length;i++){
    
    
                if(quueElement.priority<this.items[i].priority){
    
    
                    this.items.splice(i,0,quueElement);
                    this.added = true;
                    break;
                }
            }
            if(!added){
    
    
                this.items.push(quueElement);
            }

        }
        
    }
    // 移除队列第一项
    Priority.prototype.dequeue = function(){
    
    
        return this.items.shift();
    }
    // 查看队首元素
    Priority.prototype.front = function(){
    
    
        return this.items[0]
    }
    // 查看队列是否为空
    Priority.prototype.isEmpty = function(){
    
    
        return this.items.length == 0
    }
    // 查看队列中元素的个数
    Priority.prototype.size = function(){
    
    
        return this.items.length
    }
    // toString()方法
    Priority.prototype.toString = function(){
    
    
        var resuletString = ''
        for (let i = 0; i < this.items.length; i++) {
    
    
            resuletString+=this.items[i].element+"-"+this.items[i].priority+" "
        }
        return resuletString
    }
}

//测试
var pg = new Priority();
pg.enqueue('abc',100)
pg.enqueue('cda',10)
pg.enqueue('fgh','40')
console.log(pg.toString());
pg.dequeue();
console.log(pg.toString());
console.log(pg.front());

Guess you like

Origin blog.csdn.net/weixin_45822938/article/details/123295344