首页 > 其他 > 详细

数据结构_队列结构_优先级

时间:2020-02-20 10:41:06      阅读:51      评论:0      收藏:0      [点我收藏+]
class priorityElement {
    constructor (elem, priority) {
      this.elem = elem
      this.priority = priority
    }
  }

  class priorityQueue {
    constructor () {
      this.items = []
    }
    enqueue (elem, priority) {
      let priorityElem = new priorityElement(elem, priority)
      let flag = true
      if (!this.isEmpty()) {
        for (let i = 0; i < this.size(); i++) {
          if (priorityElem.priority < this.items[i].priority) {
            flag = false
            this.items.splice(i, 0, priorityElem)
            break;
          }
        }
        if (flag) this.items.push(priorityElem)
      }else {
        this.items.push(priorityElem)
      }
    }

    isEmpty () {
      return this.items.length <= 0
    }
    size () {
      return this.items.length
    }
  }
  var person = new priorityQueue()
  person.enqueue(‘a‘, 10)
  person.enqueue(‘b‘, 8)
  person.enqueue(‘c‘, 3)
  person.enqueue(‘d‘, 9)
  person.enqueue(‘e‘, 4)
  person.enqueue(‘f‘, 2)
  person.enqueue(‘g‘, 5)
  person.enqueue(‘h‘, 11)
  console.log(person)

 

数据结构_队列结构_优先级

原文:https://www.cnblogs.com/JunLan/p/12334345.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!