zoukankan      html  css  js  c++  java
  • 队列数组的实现

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <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.length == 0
                }
                // 5.查看队列中元素的个数
                Queue.prototype.size = function(){
                    return this.items.length
                }
                // 6.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('111')
            alert(queue)
            queue.dequeue()
            alert(queue)
            // 验证其他方法
            alert(queue.isEmpty())
            alert(queue.size())
    
        </script>
    </body>
    </html>
    

    击鼓传花的实现方法

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <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.length == 0
                }
                // 5.查看队列中元素的个数
                Queue.prototype.size = function(){
                    return this.items.length
                }
                // 6.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('111')
            alert(queue)
            queue.dequeue()
            alert(queue)
            // 验证其他方法
            alert(queue.isEmpty())
            alert(queue.size())
            // 击鼓传花
            function passGame(nameList,num){
                // 创建队列结构
                var queue = new Queue()
                for(var i=0;i<nameList.length;i++){
                    queue.enqueue(nameList[i])
                }
                //开始数数字,
                // 不是num的时候,重新加入到队列的末尾
                // 是num这个数字的时候,将其从队列中删除
                // Num数字之前的人重新放到队列的末尾
                while(queue.size()>1){
                    for(var i=0;i<num-1;i++){
                        queue.enqueue(queue.dequeue())
                    }
                    //num对应的这个人,直接从队列中删除
                    dequeue.dequeue()
                }
            }
        </script>
    </body>
    </html>
    

    优先队列

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>优先队列</title>
    </head>
    <body>
        <script>
            // 封装优先级队列
            function PriorityQueue(){
                PriorityQueue.prototype.enqueue = function(element){
                    this.items.push(element)
                }
                // 2.从队列中删除前端元素
                PriorityQueue.prototype.dequeue = function(){
                    return this.items.shift()
                }
                // 3.查看前端元素
                PriorityQueue.prototype.front = function(){
                    return this.items[0]
                }
                // 4.查看队列是否为空
                PriorityQueue.prototype.isEmpty = function(){
                    return this.items.length == 0
                }
                // 5.查看队列中元素的个数
                PriorityQueue.prototype.size = function(){
                    return this.items.length
                }
                // 6.toString方法
                PriorityQueue.prototype.toString = function(){
                    var resultString = ''
                    for(var i=0;i<this.items.length;i++){
                        resultString+=this.items[i]+''
                    }
                    return resultString
                }
                //封装属性
                function QueueElement(element,priority){
                    this.element = element
                    this.priority = priority //数据的优先级
                }
                //封装属性
                this.items = []
                PriorityQueue.prototype.enqueue = function(element,priority){
                    //创建QueueElement
                var queueElement = new QueueElement(element,priority)
                // 判断队列是否为空
                if(this.items.length==0){
                    this.items.push(queueElement)
                }else{
                    var added = false 
                    for(var i=0;i<this.items.length;i++){
                        if(queueElement.priority<this.items[i]){
                            self.items.splice(i,0,queueElement)
                            added = true 
                            break
                        }
                    }
                    if(!added){
                        this.items.push(queueElement)
                    }
                }
                }
            }
            //测试代码
            var pq = new PriorityQueue()
            pq.enqueue('abc',111)
            pq.enqueue('cnb',11)
            pq.enqueue('nbc',123)
            pq.enqueue('vbn',9)
            console.log('pq',pq)
            alert('pq',pq)
        </script>
    </body>
    </html>
    
  • 相关阅读:
    json.parse 与 json.stringfy
    window.self ->window.top->window.parent
    Matrix探究细谈
    android TypedValue.applyDimension()的探究
    Android判断屏幕锁屏的步骤总结
    Intent和IntentFilter详解
    android中活动的启动模式
    android中的显式与隐式Intent
    android中的Pending.getActivity()参数详解
    android之传感器
  • 原文地址:https://www.cnblogs.com/smart-girl/p/13447964.html
Copyright © 2011-2022 走看看