优先级队列javascript

优先级队列javascript,javascript,data-structures,Javascript,Data Structures,在下面的优先级队列中,当使用相同优先级的元素排队时,它们彼此相邻添加,但我看不到任何与此相关的特殊条件 function PriorityQueue() { var collection = []; this.printCollection = function() { (console.log(collection)); }; this.enqueue = function(element){ if (this.isEmpty

在下面的优先级队列中,当使用相同优先级的元素排队时,它们彼此相邻添加,但我看不到任何与此相关的特殊条件

    function PriorityQueue() {
    var collection = [];
    this.printCollection = function() {
      (console.log(collection));
    };
    this.enqueue = function(element){
        if (this.isEmpty()){ 
            collection.push(element);
        } else {
            var added = false;
            for (var i=0; i<collection.length; i++){
                 if (element[1] < collection[i][1]){ //checking priorities
                    collection.splice(i,0,element);
                    added = true;
                    break;
                }
            }
            if (!added){
                collection.push(element);
            }
        }
    };
    this.dequeue = function() {
        var value = collection.shift();
        return value[0];
       };
    this.front = function() {
        return collection[0];
    };
    this.size = function() {
        return collection.length; 
    };
    this.isEmpty = function() {
        return (collection.length === 0); 
    };
}

var pq = new PriorityQueue(); 
pq.enqueue(['Beau Carnes', 2]); 
pq.enqueue(['Quincy Larson', 3]);
pq.enqueue(['Ewa Mitulska-Wójcik', 1])
pq.enqueue(['Briana Swift', 2])
pq.printCollection();
pq.dequeue();
console.log(pq.front());
pq.printCollection();
函数优先级队列(){
var集合=[];
this.printCollection=函数(){
(控制台日志(收集));
};
this.enqueue=函数(元素){
如果(this.isEmpty()){
收集、推送(元素);
}否则{
var added=假;

对于(var i=0;i而言,条件在拼接调用中

集合.拼接(i,0,元素);

解释排队功能

 this.enqueue = function(element){
        //check if collection is empty push the element
        if (this.isEmpty()){ 
            collection.push(element);
        } else {
            //Set a flag to check if element added 
            //(this is to determine is the for loop found a match)
            var added = false;
            for (var i=0; i<collection.length; i++){
                 //if the element at the index 'i' in the collection has
                 //higher priority you need the splice the array at that 
                 //index and insert the element there
                 if (element[1] < collection[i][1]){ //checking priorities
                    //splice the array at that index and insert the element
                    collection.splice(i,0,element);
                    //satisfy the added condition
                    added = true;
                    //break the for loop
                    break;
                }
            }
            //if not added in the loop
            if (!added){
                //push to the end on the collection
                collection.push(element);
            }
        }
    };
this.enqueue=函数(元素){
//检查集合是否为空,然后推送元素
如果(this.isEmpty()){
收集、推送(元素);
}否则{
//设置标志以检查是否添加了元素
//(这是为了确定for循环是否找到匹配项)
var added=假;

对于(var i=0;i您可以使用下面提到的库。只需执行npm安装collectiondatalib即可

特点:

易于使用,所有集合和搜索排序方法都可用


您也可以在该页面上找到源代码。

collection.splice(i,0,element);
在该if块中,它表示如果元素的优先级小于集合中的元素,则替换受尊重的元素,如果元素优先级相同,请查看拼接的工作原理:collection.splice(i,0,element);此处,
0
表示如果我尝试删除任何元素,则pq.enqueue(['Something',2])为什么要在Briana Swift附近添加,它必须在if条件下失败。请尝试解释排队if条件。