js的数据结构和算法-线性结构(数组,栈,队列)

163 阅读1分钟

  • 是一种线性结构
  • 特点:后进后出

封装

        function Stack(item) {
            this.items = [];
            Stack.prototype.push = function (item) {
                this.items.push(item)
            }
            Stack.prototype.pop = function () {
                return this.items.pop()
            }
            Stack.prototype.TopItem = function () {
                return this.items[this.items.length - 1]
            }
            Stack.prototype.isEmpty = function () {
                return this.items.length == 0
            }
            Stack.prototype.Num = function () {
                return this.items.length
            }
            Stack.prototype.toString = function () {
                return this.items.join(' ')
            }
        }
        var pp = new Stack()

利用栈结构计算十进制转化为二进制

        function toChange(num){
            let record=new Stack()
            while(num/2>0){
                record.push(num%2);
                var num=Math.floor(num/2) 
            }
            var rs='';
            while(!record.isEmpty()){
                rs+=record.pop()
            }
            return rs
        }
        toChange(50)

队列

  • 只允许在前端删除,后端插入,先进先出
  • 也是一种受限的线性结构
  • FIFO(first in first out)

队列的封装

        function Queue(item){
            this.items=[]
            Queue.prototype.enqueue=function(item){
                this.items.push(item)
            }
            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(){
                return this.items.join(' ')
            }
        }
        var cc=new Queue()

击鼓串花

        //用队列来做击鼓串花
        //规则:给一个数组,给一个数字
        //数字从0数到这个数字,那么这个人就淘汰,循环到最后一个人结束游戏
        function passGame(namelist,num){
            let ccc=new Queue()
            //把list的都加入队列中
            for(var i=0;i<namelist.length;i++){
                ccc.enqueue(namelist[i])
            }
            //开始游戏
            //循环到只有一个的时候暂停
            while(ccc.size>1){
                for(var i=0;i<num-1;i++){
                //不被删除的数字,先删除再添加到队列的末尾
                ccc.enqueue(ccc.dequeue())
            }
            //删除那个要删除的
            ccc.dequeue()
            }
            return ccc.front()
        }

优先级队列

  • 插入元素的时候,会考虑数据的优先级再进行排列
  • 只有插入不一样,别的都和普通队列一样

优先级队列的封装

        function PriorityQueue() {
            this.items = [];
            function QueueElement(ele, pro) {
                this.ele = ele;
                this.pro = pro;
            }
            PriorityQueue.prototype.enqueue = function (ele, pro) {
                var item = new QueueElement(ele, pro)
                //如果队列没有数据的话,就直接插入
                if (this.items.length == 0) {
                    this.items.push(item)
                } else {
                    var add = false;
                    for (var i = 0; i < this.items.length; i++) {
                    //splice插入是插到i的前面的
                        if (item.pro < this.items[i].pro) {
                            this.items.splice(i, 0, item)
                            add = true;
                            break
                        }
                    }
                    if (!add) {
                        this.items.push(item)
                    }
                }

            }
        }