zoukankan      html  css  js  c++  java
  • JavaScript实现基于对象的栈

    class Stack {
      constructor() {
        this.count = 0;
        this.items = {};
      }
      push(element) {
        this.items[this.count] = element;
        this.count++;
      }
      pop() {
        if (this.isEmpty()) {
          return undefined;
        }
        this.count--;
        const result = this.items[this.count];
        delete this.items[this.count];
        return result;
      }
      peek() {
        if (this.isEmpty()) {
          return undefined;
        }
        return this.items[this.count - 1];
      }
      isEmpty() {
        return this.count === 0;
      }
      size() {
        return this.count;
      }
      clear() {
        /* while (!this.isEmpty()) {
            this.pop();
          } */
        this.items = {};
        this.count = 0;
      }
      toString() {
        if (this.isEmpty()) {
          return '';
        }
        let objString = `${this.items[0]}`;      //反引号包裹的匹配字面量
        for (let i = 1; i < this.count; i++) {
          objString = `${objString},${this.items[i]}`;
        }
        return objString;
      }
    }
  • 相关阅读:
    用python将博客园的文章爬取到本地
    2016.7.9
    2016.7.8
    2016.7.7
    2016.7.5
    2016.7.4
    2016.7.3
    2016.7.2
    2016.6.28
    2016.6.27
  • 原文地址:https://www.cnblogs.com/WP-WangPin/p/13885720.html
Copyright © 2011-2022 走看看