JS 实现出入栈操作
栈 栈是一种特殊的列表,栈内的元素只能通过列表的一端访问,这一端称之为栈顶。栈被称为一种后入先出(LIFO,last-in-first-out)的数据结构。盘子就是最好的例子,最后叠入的盘子,总是最先出去。 实现 function stack() { this.dataStore = []; //初始化数组 this.topa = 0; //栈位 this.pop = pop; //出栈 this.push = push; //入栈 this.clear = clear; //清楚栈 this.length = length; //返回栈的长度 } function pop() { return this.dataStore[--this.topa]; } function push(element) { return t