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;
      }
    }
  • 相关阅读:
    java分解质因数
    GUID全局唯一标识符
    Oracle-教师信息表(Teacher)
    Oracle-成绩表(Score)
    Oracle-建表course
    Oracle-建表student
    输入输出-复制
    Map
    哈希
    链表
  • 原文地址:https://www.cnblogs.com/WP-WangPin/p/13885720.html
Copyright © 2011-2022 走看看