码迷,mamicode.com
首页 > Web开发 > 详细

js中的队列结构

时间:2021-05-04 15:33:37      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ack   return   mamicode   name   rgba   head   shift   style   utf-8   

<!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 = []

         //方法
         //1.将元素加入到队列里
         Queue.prototype.enQueue = function(element){
            this.items.push(element)
         }

         //2.移除队列的第一个元素,并返回被移除的元素
         Queue.prototype.deQueue = function(){
            this.items.shift()
         }

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

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

         //5.返回队列里的元素
         Queue.prototype.size = function(){
            return this.items.length
         }
         
         //6.toString
         Queue.prototype.toString =function(){
            let resultsString = ‘‘
                 for(var i =0; i<this.items.length;i++){
                    resultsString += this.items[i] + 
                 }

                 return resultsString
         }
         
      }

      //使用队列
      var queue = new Queue();

      //将元素加入到队列中
      queue.enQueue(abc )
      queue.enQueue(eqwc )
      queue.enQueue(fgqwc )
    //   console.log(queue);

      queue.deQueue()//删除前面的元素
      console.log(queue); //["eqwc ", "fgqwc "]

     console.log(queue.front());  //eqwc

     console.log(queue.isEmpty());

     console.log(queue.toString());


    </script>
</body>
</html>

效果:

技术图片

PS:

技术图片

js中的队列结构

标签:ack   return   mamicode   name   rgba   head   shift   style   utf-8   

原文地址:https://www.cnblogs.com/malong1992/p/14725147.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!