栈

<!-- * @Author: fxpby * @Date: 2020-09-07 15:08:14 * @LastEditTime: 2020-09-07 15:32:17 * @LastEditors: fxpby * @Description: -->

一、创建一个基于Javascript对象的Stack类

javascript
class Stack {
  constructor () {
    this.count = 0
    this.items = {}
  }
}

2.向栈中插入元素

javascript
push (elements) {
  this.items[this.count] = elements
  this.count++
  return elements
}

3.验证一个栈是否为空

javascript
isEmpty () {
  return this.items.length === 0
}

4.获取栈的大小

javascript
size () {
  return this.items.length
}

5.从栈中弹出元素

javascript
pop () {
  if (this.isEmpty()) {
    return undefined
  }
  this.count--
  const result = this.items[this.count]
  delete this.items[this.count]
  return result
}

6.查看栈顶元素

javascript
peek () {
  if (this.isEmpty()) {
    return undefined
  }
  return this.items[this.count - 1]
}

7.清空栈

javascript
clear () {
  this.items = {}
  this.count = 0
  // while (!this.isEmpty()) {
  //   this.pop()
  // }
}

8.创建toString方法

javascript
toString () {
  if (this.isEmpty()) {
    return ''
  }
  let str = ''
  for (let i = 0; i < this.count; i++) {
    str += `${this.items[i]}`
  }
  return str
}

二、创建一个基于Javascript数组的Stack类

js
class Stack {
  constructor() {
    this.stack = []
  }

  size() {
    return this.stack.length
  }

  push() {
    const args = [].slice.call(arguments)
    this.stack = [...this.stack, ...args]
    return this.stack.length
  }

  pop() {
    return this.stack.splice(this.size() - 1, 1)[0]
  }

  peek() {
    return this.stack[this.size() - 1]
  }

  isEmpty() {
    return this.size() === 0
  }
}

沿着联系继续阅读

0 条反向链接

引用本文

还没有公开笔记引用这里。

本文提到

暂时没有其他关联。

在花园里找一找

输入关键词开始搜索。按 Esc 关闭。