结构类型:

1.栈:(JavaScript中没有栈这种数据结构,可以创造)

  • 栈顶栈底
  • 入栈出栈
  • 后进先出

image.png
2.队列:
先进先出

模拟栈结构

  1. <script>
  2. function Stack(){
  3. this.item =[];
  4. }
  5. /*
  6. push 入栈
  7. pop 出栈
  8. peek 获取栈顶
  9. isEmpty 判断栈是否为空
  10. size 可以看栈中有多少
  11. */
  12. Stack.prototype.push =function(val){
  13. this.item.push(val);
  14. }
  15. Stack.prototype.pop =function(){
  16. var res =this.item.pop();
  17. return res;
  18. }
  19. Stack.prototype.peek = function(){
  20. return this.item[this.items.length-1]
  21. }
  22. var s =new Stack();
  23. s.push(2);
  24. s.push(3);
  25. console.log(s.item);
  26. console.log(s.pop());
  27. </script>