繁体   English   中英

更新 javascript 中队列的优先级

[英]update priority of queue in javascript

  1. 在 hasValue class 中,为什么返回不起作用? 当我尝试使用 console.log 和 alert 时,它起作用了。
  2. 想实现 function 像 priorityQueue.changePriority("Sheru", 1); changePriority class 不起作用。 注释代码是我试图实现更改的代码,即我想更改队列中现有项目的优先级。 有人可以帮忙吗?
class QElement {
  constructor(element, priority) {
    this.element = element;
    this.priority = priority;
  }
}

class PriorityQueue {
  constructor() {
    this.items = [];
  }
  
  isEmpty() {
    return this.items.length == 0;
  }
  
  add(element, priority) {
    var qElement = new QElement(element, priority);
    var contain = false;
    for (var i = 0; i < this.items.length; i++) {
      if (this.items[i].priority > qElement.priority) {
        this.items.splice(i, 0, qElement);
        contain = true;
        break;
      }
    }
    if (!contain) {
      this.items.push(qElement);
    }
  }
  
  peek() {
    if (this.isEmpty())
      return "No elements in Queue";
    return this.items[0];
  }
  
  poll() {
    if (this.isEmpty())
      return "Underflow";
    return this.items.shift();
  }
  
  /*changePriority(firstTerm, secondTerm)
  {
    let xxx = new QElement(firstTerm, secondTerm);
      for (let i = 0; i < this.items.length; i++){
        if (this.items[i].element === firstTerm){
           this.items[i].priority = secondTerm;
           this.items.splice(i, 0, xxx);
        }
        }

        this.items.push(xxx);
  }*/
  
  hasValue(args) {
    let status = false;
    for (let i = 0; i < this.items.length; i++) {
      if (this.items[i].element === args) {
        status = true;
      }
    }
    console.log(status);
  }
  
  size() {
    if (this.isEmpty())
      return "Underflow";
    return this.items.length;
  }
  
  printPQueue() {
    var str = "";
    for (var i = 0; i < this.items.length; i++)
      str += this.items[i].element + " ";
    return str;
  }
}

var priorityQueue = new PriorityQueue();
console.log(priorityQueue.isEmpty());
console.log(priorityQueue.peek());
priorityQueue.add("Sumit", 2);
priorityQueue.add("Gourav", 1);
priorityQueue.add("Piyush", 1);
priorityQueue.add("Sunny", 2);
priorityQueue.add("Sheru", 3);
console.log(priorityQueue.printPQueue());
console.log(priorityQueue.peek().element);
console.log(priorityQueue.poll().element);
priorityQueue.add("Sunil", 2);
console.log(priorityQueue.size());
priorityQueue.hasValue('Sumit');
console.log(priorityQueue.printPQueue());
priorityQueue.changePriority("Sheru", 1);
console.log(priorityQueue.printPQueue());
  1. 您缺少 return 关键字。 这很有效:
hasValue(args) {
    for (let i = 0; i < this.items.length; i++) {
        if (this.items[i].element === args) {
            return true;
        }
    }
    
    return false;
}
  1. 我不明白你的 changePriority function 应该如何工作的想法。 只需找到元素并根据优先级更改上下移动它:
swap(a, b) {
    let tmp = this.items[a];
    this.items[a] = this.items[b];
    this.items[b] = tmp;
}

changePriority(firstTerm, secondTerm) {
    let i = 0;

    while (i < this.items.length) {
        if (this.items[i].element === firstTerm) {

            if (secondTerm < this.items[i].priority) {
                // move up

                this.items[i].priority = secondTerm;

                while (i > 0 && this.items[i - 1].priority > secondTerm) {
                    this.swap(i - 1, i);
                    i--;
                }

            } else if (secondTerm > this.items[i].priority) {
                // move down

                this.items[i].priority = secondTerm;

                while (i < this.items.length - 1 && this.items[i + 1].priority < secondTerm) {
                    this.swap(i + 1, i);
                    i++;
                }

            }
            break;
        }

        i++;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM