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

55次阅读

共计 406 个字符,预计需要花费 2 分钟才能阅读完成。

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;
  }
}

正文完
 0