说明
为了加强自己的内功,我准备多学学底层的基础的东西,刚看完了你不知道的js,下面来整一整数据结构,第一个就是栈。
开始了
栈是一种遵循后进先出(last in first out)原则的有序集合。新添加的或待删除的元素都保存在栈的同一端,称为栈顶,另一端就是栈底。就像装乒乓球的盒子,

ES6版本
class Stack {
constructor() {
this.items = []
}
// 往栈里添加新元素
push (ele) {
this.items.push(ele)
}
// 移除栈里的元素
pop() {
return this.items.pop()
}
// 查看栈顶的元素
peek () {
return this.items[items.length - 1]
}
// 检查栈顶是否为空
isEmpty () {
return this.items.length == 0
}
// 返回栈的长度
size () {
return this.items.length;
}
// 清空栈
clear () {
this.items = []
}
// 打印栈元素 ,测试使用
console () {
return this.items
}
}
ES版本
function Stack () {
var items = [];
//往栈里添加新元素
this.push = function (ele) {
items.push(ele);
},
// 移除栈里的元素
this.pop = function () {
return items.pop();
},
// 查看栈顶的元素
this.peek = function () {
return items[items.length -1]
},
// 检查栈顶是否为空
this.isEmpty = function () {
return items.length == 0;
},
// 返回栈的长度
this.size = function () {
return items.length
},
// 清空栈
this.clear = function () {
items = []
},
// 打印栈元素 ,测试使用
this.console = function () {
return items
}
}
下一篇是堆。