博客地址:栈与队列

栈结构,栈结构就是在数组的基础上限制一些操作

栈的常用方法

  • push(): 添加一个新元素到栈顶

  • pop(): 移除栈顶元素,同时返回被移除的元素

  • peek(): 返回栈顶元素,不对栈做修改

  • isEmpty(): 栈里无元素则返回true

  • size(): 返回栈的元素个数

  • toString(): 将栈结构的内容以字符形式返回

栈的代码实现


function Stack() {
   
  //栈的属性
  this.items  = []

  //栈的操作

  Stack.prototype.push = function(element) {
   
    this.items.push(element)
  }

  Stack.prototype.pop = function() {
   
    return this.items.pop()
  }

  Stack.prototype.peek = function() {
   
    return this.items[this.items.length - 1]
  }

  Stack.prototype.isEmpty = function() {
   
    return this.items.length == 0
  }

  Stack.prototype.size = function() {
   
    return this.items.length
  }

  Stack.prototype.toString = function() {
   
    let resultString = ""
    for (const item of this.items) {
   
      resultString += item + ' '
    }
    return resultString
  }
}

通过栈结构实现二进制转化为十进制

每次将余数压入栈中,最后出栈的顺序便是转化的结果

function dec2Bin(decNumber) {
   

  let stack = new Stack()
  while (decNumber > 0) {
   
    stack.push(decNumber % 2)

    decNumber = Math.floor(decNumber / 2)
  }

  let binaryString = ""
  while(!stack.isEmpty()) {
   
    binaryString += stack.pop()
  }
  return binaryString
}

队列


队列分前端后后端,在后端加入元素,在前端删除元素

特点:先进先出

队列常用方法

  • enqueue(element): 向队列尾部添加一个新的项

  • dequeue(): 移除队列的第一项,并返回移除的元素

  • front(): 返回队列中第一个元素,不改变原队列

  • isEmpty(): true&false

  • size(): 元素个数

  • toString(): 将队列中的内容转为字符串形式

基于数组的队列封装

function Queue() {
   
  //属性
  this.items = []
  //方法
  Queue.prototype.enqueue = function(element) {
   
    this.items.push(element)
  }
  Queue.prototype.dequeue = function() {
   
    return this.items.shift()
  }
  Queue.prototype.front = function() {
   
    return this.items[0]
  }
  Queue.prototype.isEmpty = function() {
   
    return this.items.length == 0
  }
  Queue.prototype.size = function() {
   
    return this.items.length
  }
  Queue.prototype.toString = function() {
   
    let resString = ""
    for (const item of this.items) {
   
      resString += item + " "
    }
    return resString
  }
}

击鼓传花游戏的实现


代码实现

function passGame(nameList, num) {
   
  let queue = new Queue()
  //先将所有依次加入队列
  for (const item of nameList) {
   
    queue.enqueue(item)
  }

  //开始数数
  while(queue.size() > 1) {
   
    for(let i = 1;i < num;i ++) {
   
      queue.enqueue(queue.dequeue())
    }
    queue.dequeue()
  }
  let name = queue.front()
  console.log(name)
}
passGame([10,20,30,40],3)

优先级队列的实现

需要给队列中的每个项目添加一个priority,表示优先级高低

function PrioityQueue() {
   

  this.items = []

  function QueueElement(element, priority) {
   
    this.element = element
    this.priority = priority
  }

  PrioityQueue.prototype.enqueue = function (element, priority) {
   
    let queueElement = new QueueElement(element, priority)
    //判断队列是否未空
    if (this.items.length == 0) {
   
      this.items.push(queueElement)
    } else {
   
      let added = false
      for (let i = 0; i < this.items.length; i++) {
   
        if (queueElement.priority < this.items[i].priority) {
   
          this.items.splice(i, 0, queueElement)
          added = true
          break
        }
      }
      //不需要插入则直接放入最后
      if (!added) {
   
        this.items.push(queueElement)
      }
    }
  }
  PrioityQueue.prototype.dequeue = function () {
   
    return this.items.shift()
  }
  PrioityQueue.prototype.front = function () {
   
    return this.items[0]
  }
  PrioityQueue.prototype.isEmpty = function () {
   
    return this.items.length == 0
  }
  PrioityQueue.prototype.size = function () {
   
    return this.items.length
  }
  PrioityQueue.prototype.toString = function () {
   
    let resString = ""
    for (const item of this.items) {
   
      resString += item + " "
    }
    return resString
  }
}