栈数据结构的特点:后进先出 例子水杯,谷仓,米袋就是栈的数据结构
push:数据进栈
pop:将栈顶的数据出栈
peek:获取栈顶的数据
isEmpty:判断栈是否为空
size:返回栈的长度
location路由:是以栈的数据结构去保存路由的
eg:JS模拟栈数据结构
<script> class Stack{ constructor(){ this.items = []; } push(value){ this.items.push(value) } pop(){ return this.items.pop(); } peek(){ return this.items[this.items.length-1]; } isEmpty(){ return this.items.length==0; } size(){ return this.items.length; } } var s = new Stack(); s.push(8); s.push(9); console.log(s.pop()); //9 console.log(s.peek()); //8 console.log(s.isEmpty()); //false console.log(s.size()); //1 </script>