定义栈类
   function Stack() {
        this.dataStore = [];
        this.top = 0;
        this.push = push;
        this.pop = pop;
        this.peek = peek;
        this.clear = clear;
        this.length = length;
    }
    function push(element) {
        return this.dataStore[this.top ++] = element;
    }
    function pop() {
        return this.dataStore[--this.top]
    } 
    function peek() {
          return this.dataStore[this.top - 1];
    }
    function clear() {
          this.top = 0;
    }
    function length() {
          return this.top;
    }
    
//实践 ——回文
  function isPalindrome(word) {
      var s = new Stack();
      for (var i =0; i 0) {
          rword += s.pop();
      }
      return rword === word;
      }








你可能感兴趣的:(栈)