首页 > 编程语言 >JavaScript--队列结构

JavaScript--队列结构

时间:2022-10-14 02:33:06浏览次数:59  
标签:function 元素 return -- items JavaScript 队列 var

 

1.认识队列

 

 

 

 

 

2.队列的应用

 

 

 

3.队列类的常见操作

 

 

 

 

封装一个队列

<!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>Document</title>
</head>
<body>
    <script>
        // 封装队列类
        function Queue(){
            // 属性
            this.items=[]


            // 1.将元素加入到队列中
            Queue.prototype.enqueue=function(element){
                this.items.push(element)
            }



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



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




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



            // 5.查看队列中元素的个数
            Queue.prototype.size=function(){
                return this.items.length
            }





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

        }



        // 使用队列

        var queue=new Queue()

        // 将元素加入到队列中
        queue.enqueue('abc')
        queue.enqueue('cba')
        queue.enqueue('nba')
        queue.enqueue('mba')
        queue.enqueue('abc')
        

        // 从队列中删除元素
        queue.dequeue()
        alert(queue)
        queue.dequeue()
        alert(queue)

        // front 方法
        alert(queue.front())

        // 验证其他方法
        alert(queue.isEmpty())
        alert(queue.size())



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

 

 

 

4.击鼓传花

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    // 自定义队列
    function Queue() {
        var items = []

        // 队列操作的方法
        // enter queue方法
        this.enqueue = function (element) {
            items.push(element)
        }

        // delete queue方法
        this.dequeue = function () {
            return items.shift()
        }

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

        // 查看队列是否为空
        this.isEmpty = function () {
            return items.length == 0
        }

        // 查看队列中元素的个数
        this.size = function () {
            return items.length
        }
    }

    // 实现击鼓传花的函数
    function passGame(nameList, num) {
        // 1.创建一个队列, 并且将所有的人放在队列中
        // 1.1.创建队列
        var queue = new Queue()

        // 1.2.通过for循环, 将nameList中的人放在队列中
        for (var i = 0; i < nameList.length; i++) {
            queue.enqueue(nameList[i])
        }

        // 2.寻找最后剩下的人
        while (queue.size() > 1) {
            // 将前num-1中的人, 都从队列的前端取出放在队列的后端
            for (var i = 0; i < num; i++) {
                queue.enqueue(queue.dequeue())
            }

            // 将第num个人, 从队列中移除
            queue.dequeue()
        }

        // 3.获取剩下的一个人
        alert(queue.size())
        var endName = queue.dequeue()
        alert("最终留下来的人:" + endName)

        // 4.获取该人在队列中的位置
        return nameList.indexOf(endName)
    }

    // 验证结果
    var names = ['John','Jack','Camila','Ingrid','Carl'];
    var index = passGame(names, 7)
    alert("最终位置:" + index)
</script>
</body>
</html>

 

 

 

 

5. 优先级队列

 

 

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    // 封装优先级队列
    function PriorityQueue() {
        var items = []

        // 封装一个新的构造函数, 用于保存元素和元素的优先级
        function QueueElement(element, priority) {
            this.element = element
            this.priority = priority
        }

        // 添加元素的方法
        this.enqueue = function (element, priority) {
            // 1.根据传入的元素, 创建新的QueueElement
            var queueElement = new QueueElement(element, priority)

            // 2.获取传入元素应该在正确的位置
            if (this.isEmpty()) {
                items.push(queueElement)
            } else {
                var added = false
                for (var i = 0; i < items.length; i++) {
                    // 注意: 我们这里是数字越小, 优先级越高
                    if (queueElement.priority < items[i].priority) {
                        items.splice(i, 0, queueElement)
                        added = true
                        break
                    }
                }

                // 遍历完所有的元素, 优先级都大于新插入的元素时, 就插入到最后
                if (!added) {
                    items.push(queueElement)
                }
            }
        }

        // 删除元素的方法
        this.dequeue = function () {
            return items.shift()
        }

        // 获取前端的元素
        this.front = function () {
            return items[0]
        }

        // 查看元素是否为空
        this.isEmpty = function () {
            return items.length == 0
        }

        // 获取元素的个数
        this.size = function () {
            return items.length
        }
    }

    // 创建优先级队列对象
    var pQueue = new PriorityQueue()

    // 添加元素
    pQueue.enqueue("abc", 10)
    pQueue.enqueue("cba", 5)
    pQueue.enqueue("nba", 12)
    pQueue.enqueue("mba", 3)
// 遍历所有的元素
    var size = pQueue.size()
    for (var i = 0; i < size; i++) {
        var item = pQueue.dequeue()
        alert(item.element + "-" + item.priority)
    }
</script>
</body>
</html>

 

标签:function,元素,return,--,items,JavaScript,队列,var
From: https://www.cnblogs.com/hechunfeng/p/16790246.html

相关文章

  • 15、vue+elementUI
    设置动态路由导航栏点击高亮点击查看代码<template><el-containerstyle="height:500px;border:1pxsolid#eee"><el-asidewidth="200px"style="bac......
  • 关于另一个大问题的解决!
    它默默报错,我默默找问题(c:forEach标签)无法使用c:forEach标签时,可能是我们没有导入Standard.jar包,需要导入相应jar包才会生效一定要记得奥!!!!!!!更重要的问题是:为什么一切正常......
  • CF1542B Plus and Multiply
    CF1542BPlusandMultiply-洛谷|计算机科学教育新生态(luogu.com.cn)\(T=\{a^x+yb\text{}\vert\text{}x,y\inN\}\)和\(S\)相等。证明:\(S\subset......
  • 共享单车
    makefile编译    gdb调试编译时 g++ -g文件名-o 生成的可执行文件名gdb 文件名 就在gdb模式下调试 b 两次tab键 会把涉及到b的函数全部列......
  • 22.10.14 codeforce D
    题目D.Coprime给定n个正整数数组a1,a2,…,an(1≤ai≤1000)。求i+j的最大值,使ai和aj为互素,†或−1(如果不存在i,j)。例如,考虑数组[1,3,5,2,4,7,7]。i+j可以得到的最大值......
  • [Typescript] 46. Medium - PickByType
    From T,pickasetofpropertieswhosetypeareassignableto U.ForExampletypeOnlyBoolean=PickByType<{name:stringcount:numberisReadonly:bo......
  • C#事务的使用
    https://blog.csdn.net/VisageNocturne/article/details/112094795http://t.zoukankan.com/SmilePastaLi-p-6824387.htmlhttps://www.w3xue.com/exp/article/20225/79298......
  • flask上传、下载文件
    fromflaskimportFlask,render_template,request,send_filefromwerkzeug.utilsimportsecure_filename#创建appapp=Flask(__name__)#上传文件-网页端html_cod......
  • 代码随想录Day1
     题目(LeetCode704) 给定一个 n 个元素有序的(升序)整型数组 nums和一个目标值 target ,写一个函数搜索 nums 中的target,如果目标值存在返回下标,否则返回-1......
  • [Typescript] 47. Medium- StartsWith
    Implement StartsWith<T,U> whichtakestwoexactstringtypesandreturnswhether T startswith UForexampletypea=StartsWith<'abc','ac'>//expected......