数据结构

: 是限制在表的一端进行插入和删除运算的线性表,通常称插入、删除的这一端为栈顶(Top),另一端为栈底(Bottom)。先进后出。top= -1时为空栈,top=0只能说明栈中只有一个元素,并且元素进栈时top应该自增. 后进先出
栈常见有操作

  • push(element):添加一个新元素到栈顶位置
  • pop():移除栈顶的元素,同时返回被移除的元素
  • peek():返回栈顶的元素,不对栈做任何修改(这个方法不会移除栈顶的元素,仅仅返回它)
  • isEmpty():如果栈里没有任何元素就返回true,否则返回false
  • size():返回栈里的元素个数。这个方法和数组的length属性很类似
  • toString():将栈结构的内容以字符形式返回
 <script>
        // 封装栈类
        function Stack() {
            // 栈中的属性
            this.items = []
            Stack.prototype.push = function(element) {
                this.items.push(element)
            }
            Stack.prototype.pop = function() {
                return this.items.pop()
            }
            Stack.prototype.peek = function() {
                return this.items[this.items.length - 1]
            }
            Stack.prototype.isEmpty = function() {
                return this.items.length == 0
            }
            Stack.prototype.size = function() {
                return this.items.length
            }
            Stack.prototype.toString = function() {
                var resultString = ''
                for (var i = 0; i < this.items.length; i++) {
                    resultString += this.items[i] + ''
                }
                return resultString
            }
        }
        // 栈的使用
        var s = new Stack()
        s.push(20)
        s.push(10)
        s.push(30)
        s.push(50)
        console.log(s)
        s.pop()
        console.log(s)
        console.log(s.peek())
        console.log(s.isEmpty())
        console.log(s.size())

		    // 将十进制转成二进制
        function dec2bin(decNumber) {
            // 定义栈对象
            var stack = new Stack()
                // 循环操作
            while (decNumber > 0) {
                // 获取余数,并且放入到栈中
                stack.push(decNumber % 2)
                    // 获取整除后的结果,作为下一次运行的数字
                decNumber = Math.floor(decNumber / 2)
            }
            // 从栈中取出0和1
            var binaryString = ''
            while (!stack.isEmpty()) {
                binaryString += stack.pop()
            }
            return binaryString
        }
        alert(dec2bin(100))
    </script>

队列:也是一种运算受限的线性表。它只允许在表的一端进行插入,而在另一端进行删除。允许删除的一端称为队头(front),允许插入的一端称为队尾(rear)。先进先出。
队列常用的方法:

  • enqueue(element):向队列尾部添加一个(或多个)新的项
  • dequeue():移除队列的第一(即排在队列最前面的)项,并返回被移除的元素
  • front():返回队列中第一个元素——最先被添加,也将是最先被移除的元素。队列不做任何变动(不移除元素,只返回元素信息——与Stack类的peek方法非常类似)
  • isEmpty():如果队列中不包含任何元素,返回true,否则返回false
  • size():返回队列包含的元素个数,与数组的length属性类似
  • toString():将队列中的内容,转成字符串形式
 <script>
        // 封装队列
        function Queue() {
            this.items = []
            Queue.prototype.enqueue = function(element) {
                this.items.push(element)
            }
            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() {
                var resultString = ''
                for (var i = 0; i < this.items.length; i++) {
                    resultString += this.items[i] + ' '
                }
                return resultString
            }
        }
        // 使用队列
        var queue = new Queue()
        queue.enqueue('a')
        queue.enqueue('b')
        queue.enqueue('c')
        queue.enqueue('d')
        alert(queue)
        queue.dequeue()
        alert(queue)
        alert(queue.front())
        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])
            }
            while (queue.size() > 1) {
                // 不是num的时候,重新加入到队列的末尾
                for (var i = 0; i < num - 1; i++) {
                    queue.enqueue(queue.dequeue())
                }
                // 是num这个数字,将其从队列中删除
                queue.dequeue()
            }
            // 获取剩下的那个人
            alert(queue.size())
            var endName = queue.front()
            alert('最终剩下的人' + endName)
            return nameList.indexOf(endName)
        }
        // 测试
        var names = ['a', 'b', 'c', 'd', 'e', 'd']
        alert(passGame(names, 3))
    </script>

优先级队列

<script>
        // 封装优先级队列
        function PriorityQueue() {
            // 在PriorityQueue重新创建一个类
            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].priority) {
                            this.items.splice(i, 0, queueElement)
                            added = true
                            break
                        }
                    }
                    if (!added) {
                        this.items.push(queueElement)
                    }
                }
            }

            PriorityQueue.prototype.dequeue = function() {
                return this.items.shift()
            }
            PriorityQueue.prototype.front = function() {
                return this.items[0]
            }
            PriorityQueue.prototype.isEmpty = function() {
                return this.items.length == 0
            }
            PriorityQueue.prototype.size = function() {
                return this.items.length
            }
            PriorityQueue.prototype.toString = function() {
                var result = ''
                for (var i = 0; i < this.items.length; i++) {
                    result += this.items[i].element + '-' + this.items[i].priority + ' '
                }
                return result
            }
        }
        var pq = new PriorityQueue()
        pq.enqueue('a', 12)
        pq.enqueue('b', 14)
        pq.enqueue('c', 4)
        alert(pq)
    </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值