包工头带你通算法--js 描述 栈 队列(一)

栏目: 编程工具 · 发布时间: 7年前

内容简介:这是书上的栈,队列感觉怪怪的

包工头带你通算法--js 描述 栈,队列(一)

简单叙述下栈 队列

  1. 栈:先进后出 ,队列:先进先出(就是这么简单)。 个人理解, 栈就是给个数组a = [1,2,3,4] 只能进行pop(), 和push()操作, 队列就是只能进行shift() push()操作
  2. talk is cheap show me the picture(左栈 右队列)

包工头带你通算法--js 描述 栈 队列(一)

这是书上的栈,队列

感觉怪怪的

包工头带你通算法--js 描述 栈 队列(一)

包工头带你通算法--js 描述 栈 队列(一)

都9012年了

class Stack{
        constructor(){
            this.data = [];
            this.top = 0;
            this.length = 0;
        }
         // 入栈
        push(ele){
            this.length ++;
            return this.data[this.top ++] = ele;
        }
        // 出栈
        pop(){
            this.top > 0 ? this.top -- : false;
            this.length > 0 ? this.length -- : false;
            return this.data.pop();
        }
        // 清空栈
        clear(){
            this.top = 0;
            this.length = 0;
            return this.data = [];
        }
        // 返回栈顶元素
        posTop(){
            return this.data[-- this.top]; // 这里要先减 再赋值
        }
    }

    var st = new Stack();

包工头带你通算法--js 描述 栈 队列(一)


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

Pro Git (Second Edition)

Pro Git (Second Edition)

Scott Chacon、Ben Straub / Apress / 2014-11-9 / USD 59.99

Scott Chacon is a cofounder and the CIO of GitHub and is also the maintainer of the Git homepage ( git-scm.com ) . Scott has presented at dozens of conferences around the world on Git, GitHub and the ......一起来看看 《Pro Git (Second Edition)》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具