栈数据结构的特点:后进先出 例子水杯,谷仓,米袋就是栈的数据结构

2020-12-14_182844.png

push:数据进栈

pop:将栈顶的数据出栈

peek:获取栈顶的数据

isEmpty:判断栈是否为空

size:返回栈的长度

location路由:是以栈的数据结构去保存路由的

eg:JS模拟栈数据结构

  1. <script>
  2. class Stack{
  3. constructor(){
  4. this.items = [];
  5. }
  6. push(value){
  7. this.items.push(value)
  8. }
  9. pop(){
  10. return this.items.pop();
  11. }
  12. peek(){
  13. return this.items[this.items.length-1];
  14. }
  15. isEmpty(){
  16. return this.items.length==0;
  17. }
  18. size(){
  19. return this.items.length;
  20. }
  21. }
  22. var s = new Stack();
  23. s.push(8);
  24. s.push(9);
  25. console.log(s.pop()); //9
  26. console.log(s.peek()); //8
  27. console.log(s.isEmpty()); //false
  28. console.log(s.size()); //1
  29. </script>