JavaScript数据结构与算法01----优先级队列

最近在用javascript刷数据结构和算法,教程是B站上面coderwhy王红元老师的视频----六天精通JavaScript数据结构与算法系统教程,js入门到精通算法,
数据结构和算法是前端进入大厂必备的知识和技能,以前在校招的时候不懂,为什么前端还总爱考数据结构和算法,对于我这种非计算机专业的学生很不友好,现在自我可以独立解决一些常见的项目问题了,就感觉自己需要进阶了,需要去锻炼一下自己的逻辑能力了,正好借着大四在公司实习的这段时间学习一下。重新来到csdn来记录一下自己将来一段时间的成长和收获,以及再敲代码的过程中自己所遇到的逻辑上的难点。

  以下是我自己,按照老师的讲解自己重新用数组封装了数据结构里面的优先级队列,
<!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 PriorityQueue() {
            var items = []
            function QueueElement(element,priority) {
                this.element = element
                this.priority = priority
            }
            PriorityQueue.prototype.enqueue = function(element,priority) {
                var queueElement = new QueueElement(element,priority) 
                if(this.isEmpty()) {
                    items.push(queueElement)
                }else {
                    var addEnd = true
                    for(var i=0; i<items.length;i++){
                        if(queueElement.priority < items[i].priority) {
                            addEnd = false
                            items.splice(i,0,queueElement)
                            break
                        }
                    }
                    if(addEnd) {
                        items.push(queueElement)
                    }
                }
            }

            PriorityQueue.prototype.dequeue = function() {
                return items.shift()
            }

            PriorityQueue.prototype.front = function() {
                return items[0]
            }

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

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

            PriorityQueue.prototype.toString = function() {
                var resultStr = ''
                for(var i=0; i<items.length;i++){
                    resultStr+=items[i].element+'-'+items[i].priority+' '
                }
                return resultStr
            }
        }

        var pQueue = new PriorityQueue()
        pQueue.enqueue('abc',10)
        pQueue.enqueue('cba',5)
        pQueue.enqueue('nba',12)
        pQueue.enqueue('mba',3)
        console.log(pQueue.toString());
    </script>
</body>
</html>
第一个遇到的问题就是,封装的QueueElement函数是放到PriorityQueue.prototype.enqueue
函数里面还是外面,一开始以为只能放到enqueue函数里面,因为放到外面,
里面访问不到外面的函数,放在里面也没报错,后来看老师的笔记,发现老师把
QueueElement函数放在了enqueue函数外面,于是就想到了,作用域链,放到外面
也会被找到,作用域链先从作用域里面查找,里面没有再向外面查找,而放在外面的
一个好处我感觉就是不会让enqueue函数再多加一层函数,可以减轻enqueue的负担。

第二个遇到的逻辑问题就是这里

 if(this.isEmpty()) {
                    items.push(queueElement)
                }else {
                    var addEnd = true
                    for(var i=0; i<items.length;i++){
                        if(queueElement.priority < items[i].priority) {
                            addEnd = false
                            items.splice(i,0,queueElement)
                            break
                        }
                    }
                    if(addEnd) {
                        items.push(queueElement)
                    }
                }
在判断优先级的时候,第一次没有break终止循环,导致的结果就是一个元素被重复
添加多次,而且第一次用了forEach循环,当break时,报错---break时一个无效的语句,
forEach循环不能用break跳出循环,第二个问题就是没有加addEnd标志位,没有考虑到
,如果添加的元素的优先级比之前的任何一个元素的优先级都小,那新添加的元素就不会又任何操作,
这不符合我们想要的逻辑,正常逻辑应该是,如果添加的元素的优先级都比之前的任何一个小,那么新添加的元素应该被插入到数组的最后面。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值