乐趣区

关于数据结构:数据结构js实现栈

class Stack {constructor() {this.items = [];
  }

  push(item) {this.items.push(item);
  }

  pop() {return this.items.pop();
  }

  // 返回顶部元素,然而不批改
  peek() {return this.items.at(-1);
  }

  isEmpty() {return this.items.length === 0;}
  clear() {this.items = [];
  }
  size() {return this.items.length;}

  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;
  }
}
退出移动版