栈
栈的定义
栈是一种先进后出的有序集合。新添加的或待删除的元素都保存在栈的末尾,称为栈顶,另一端就叫栈底。在栈里,新元素都靠近栈顶,旧元素都接近栈底
栈的作用
在编程语言的编译器和内存中保存变量、和调用方法。
例如:函数调用栈,递归调用...
Stack.js文件基于数组的封装
export default function(item) {
this.item = item ? item : [];
this.__proto__.push = function(Element) {
this.item.push(Element);
};
this.__proto__.pop = function() {
return this.item.pop();
};
this.__proto__.getStack = function() {
return this.item;
};
this.__proto__.peek = function() {
return this.item[this.item.length - 1];
};
this.__proto__.isEmpty = function() {
return this.item.length == 0;
};
this.__proto__.clear = function() {
this.item = [];
};
this.__proto__.size = function() {
return this.item.length;
};
};