首页 > 编程语言 > 详细

JavaScript数据结构-栈

时间:2015-04-26 01:48:16      阅读:225      评论:0      收藏:0      [点我收藏+]

栈特点:

1.在栈顶添加或删除 

2.有序 

3.元素只能通过列表的一端访问 

4.后入先出(LIFO)


栈的三个主要方法 push()  pop()  peek();


     

function Stack() {
        this.top = 0;
        this.dataStore = [];
        this.push = push;
        this.pop = pop;
        this.peek = peek;
        this.clear = clear;
        this.length = length;
      }
      function push(element) {
        this.dataStore[this.top++] = element;
      }
      function pop() {
        return this.dataStore[--this.top];
      }
      function peek() {
        return this.dataStore[this.top-1];
      }
      function length() {
        return this.top;
      }
      function clear() {
        this.top = 0;
      }
      var stack = new Stack();
      stack.push("1");
      stack.push("2");
      stack.push("3");
      stack.push("4");
      stack.push("5");
      stack.push("6");
      console.log(stack.pop());
      console.log(stack.peek());


JavaScript数据结构-栈

原文:http://51web.blog.51cto.com/4386311/1638273

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!