【JS数据结构与算法】优先级队列 PriorityQueue

目录

1、创建内部类。

2、定义属性

3、优先级队列中插入元素的封装。

4、代码检验

5、完整代码


与队列相比:优先级队列的每个元素都有一个优先权。

1、创建内部类。

首先,这里需要封装一个内部类。表示可以通过此类来创建一个带权值的元素

      // 在PriorityQueue重新创建一个类,可以理解为内部类
      function QueueElement(element,priority){
        this.element = element;
        this.priority = priority;
      }

2、定义属性

以数组的形式存储数据。

      // 属性
      this.items  = [];

dequeue()、front()、isEmpty()、size()、toString()方法与队列一样,可查看【JS数据结构】队列(Queue)、队列的封装及其应用

3、优先级队列中插入元素的封装。

      // 封装enqueue函数,将带权值的元素加入到队列中。
      PriorityQueue.prototype.enqueue = function(element, priority){

        // 定义一个队列对象
        var qE = new QueueElement(element, priority);

        // 判断队列是否为空
        if(this.items.length == 0){
          this.items.push(qE);
        }else{
          var flag = false;  //flag作为旗子
          for (var i = 0; i < this.items.length; i++) {
            if(qE.priority < this.items[i].priority){
              this.items.splice(i, 0, qE);
              flag = true;
              break;
            }
          }
          if(!flag){
            this.items.push(qE);
          }
        }
      };

封装enqueue函数,将带权值的元素加入到队列中。需要传入两个参数,一个是要插入的元素(qE),另一个是该元素带的权值(priority)

有三种情况:

1.当队列的长度为0时,直接使用push方法将元素加入队列中。

2.当队列的长度不为0时,又分两种,一种是根据优先级插入到队列之中

3.另一种是根据优先级插入到队列的尾部。

4、代码检验

    var pq = new PriorityQueue();
    pq.enqueue('abc',111);
    pq.enqueue('cba',200);
    pq.enqueue('nba',50);
    pq.enqueue('nj',66);

    alert(pq); //nba nj abc cba 

 

5、完整代码

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>优先级队列</title>
</head>
<body>
  <script type="text/javascript">
    //优先级队列封装
    function PriorityQueue(){

      // 在PriorityQueue重新创建一个类,可以理解为内部类
      function QueueElement(element,priority){
        this.element = element;
        this.priority = priority;
      }

      // 属性
      this.items  = [];


      // 队列的相关操作,与队列不同的是,考虑优先级的先后顺序
      // 1、封装enqueue函数,将元素加入到队列中。
      PriorityQueue.prototype.enqueue = function(element, priority){

        // 定义一个队列对象
        var queueElement = new QueueElement(element, priority);

        // 判断队列是否为空
        if(this.items.length == 0){
          this.items.push(queueElement);
        }else{
          var flag = false;
          for (var i = 0; i < this.items.length; i++) {
            if(queueElement.priority < this.items[i].priority){
              this.items.splice(i, 0, queueElement);
              flag = true;
              break;
            }
          }
          if(!flag){
            this.items.push(queueElement);
          }
        }
      };

      // 2、从队列中删除前端的元素
      PriorityQueue.prototype.dequeue = function(){
        return this.items.shift();
      };

      // 3、查看前端的元素
      PriorityQueue.prototype.front = function(){
        return this.items[0];
      };

      // 4、判断队列是否为空
      PriorityQueue.prototype.isEmpty = function(){
        return this.items.length === 0;
      };

      // 5、获取队列中的元素个数
      PriorityQueue.prototype.size = function(){
        return this.items.length;
      };

      // 6、toString 方法 
      PriorityQueue.prototype.toString = function(){
        var resultString = '';
        for (var i = 0; i < this.items.length; i++) {
          resultString += this.items[i].element + ' ';
        }
        return resultString;  
      };

    }


    var pq = new PriorityQueue();
    pq.enqueue('abc',111);
    pq.enqueue('cba',200);
    pq.enqueue('nba',50);
    pq.enqueue('nj',66);

    alert(pq); //nba nj abc cba

  </script>

</body>
</html>

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,多级反馈队列调度算法是一种进程调度算法,它是基于时间片轮转算法的变体。它将进程按照优先级分成多个队列,然后给每个队列分配不同的时间片,高优先级队列的时间片较短,低优先级队列的时间片较长。如果一个进程在一个队列中运行了一个时间片后仍然没有完成,那么它会被移到下一个队列,并将其时间片重置为原始值。这个过程将一直持续到进程完成或者到达最后一个队列。 以下是使用 JavaScript 实现多级反馈队列调度算法的伪代码: ``` // 定义进程类 class Process { constructor(name, priority, time) { this.name = name; // 进程名称 this.priority = priority; // 进程优先级 this.time = time; // 进程需要执行的时间 } } // 定义多级反馈队列调度算法函数 function multiLevelFeedbackQueue(processes) { const queues = [[], [], []]; // 三个队列,分别存放高、中、低优先级进程 const timeQuantum = [4, 8, 16]; // 不同队列对应的时间片 let currentTime = 0; // 当前时间 let completedProcesses = []; // 完成执行的进程 while (true) { let currentProcess = null; // 从高优先级队列开始查找有没有需要执行的进程 for (let i = 0; i < queues.length; i++) { if (queues[i].length > 0) { currentProcess = queues[i].shift(); break; } } // 如果没有需要执行的进程,退出循环 if (!currentProcess) { break; } // 执行当前进程 for (let i = 0; i < timeQuantum[currentProcess.priority]; i++) { currentProcess.time--; currentTime++; // 如果进程已经执行完毕,将其加入完成队列 if (currentProcess.time === 0) { completedProcesses.push(currentProcess); break; } // 如果进程执行时间超过了当前队列的时间片,将其移到下一个队列 if (i === timeQuantum[currentProcess.priority] - 1) { if (currentProcess.priority < queues.length - 1) { currentProcess.priority++; } queues[currentProcess.priority].push(currentProcess); break; } } } // 返回完成执行的进程 return completedProcesses; } // 测试 const processes = [ new Process('P1', 0, 20), new Process('P2', 1, 10), new Process('P3', 2, 30), new Process('P4', 0, 15), new Process('P5', 1, 5), ]; const completedProcesses = multiLevelFeedbackQueue(processes); console.log(completedProcesses); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值