zl程序教程

您现在的位置是:首页 >  前端

当前栏目

JavaScript:队列的封装及面试题击鼓传花队列方法实现案例

2023-09-27 14:22:47 时间

队列的定义:队列简称队。是一种操作受限的线性表,只允许在表的一端进行插入,而在表的另一端进行删除。向队列中插入元素称为入队或进队;删除元素称为出队或离队。其操作特性为先进先出(First In First Out,FIFO),并且只允许在队尾进,队头出。

JavaScript对队列的封装

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>封装队列</title>
</head>
<body>
  <script>
    // 封装队列类
    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
      }
      // toString方法
      Queue.prototype.toString = function() {
        var resultString = ''
        for(var i = 0 ; i < this.items.length ; i++) {
          resultString += this.items[i]+' '
        }
        return resultString
      }
    }
    // 使用队列
    var queue = new Queue()
    // 将元素加入到队列中
    queue.enqueue("abc")
    queue.enqueue("def")
    queue.enqueue("lxh")
    alert(queue)
    // 从队列中删除元素
    queue.dequeue()
    alert(queue)
    // front方法
    alert(queue.front())
    // 其他方法
    alert(queue.isEmpty())
    alert(queue.size())
  </script>
</body>
</html>

队列的应用案例-->前端面试题击鼓传花
在这里插入图片描述
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>击鼓传花案例</title>
</head>
<body>
  <script>
    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() {
        var resultString = ''
        for(var i = 0 ; i < this.items.length ; i++) {
          resultString += this.items[i]+' '
        }
        return resultString
      }
    }
    // 封装函数
    function passGame(namelist,num) {
      // 创建队列结构
      var queue = new Queue()
      // 将所有的人加入到队列中
      for(var i = 0 ; i < namelist.length ; i++) {
        queue.enqueue(namelist[i])
      }
      // 开始数数
      // 不是num数字的时候,重新加入到队列的末尾
      // 是num数字的时候,将其从队列中删除
      while(queue.size() > 1) {
        for(var i = 0 ; i < num-1 ; i++) {
          // num之前的人重新放入到队列的末尾
          queue.enqueue(queue.dequeue())
        }
        // num对应的这个人直接从队列中删除掉
        queue.dequeue()
      }
      // 获取队列中剩下的那个人
      alert(queue.size())
      var endName = queue.front()
      alert("最终剩下的人:"+endName)
      return namelist.indexOf(endName)
    }

    // 测试击鼓传花的方法
    names = ['lily','lucy','lingxiaohu','hyw','xiaonaihu']
    alert(passGame(names,3))
  </script>
</body>
</html>