2__栈(先进后出)__

这篇博客探讨了三种不同的数据结构实现:基于数组、JavaScript对象和ES2015 WeakMap的栈。详细介绍了栈的基本操作如push、pop、peek等,并展示了如何使用栈进行十进制到二进制的转换。此外,还提出了一种用于历史记录管理的类,支持前进和后退操作,类似于Ctrl+Y和Ctrl+Z的功能。博客还涵盖了进制转换算法,支持将十进制数转换为任意2到36进制的数字。
摘要由CSDN通过智能技术生成

栈(先进后出)

创建一个基于数组的栈

class Stack {
  constructor() {
    this.items = [];
  }
  // 添加一个(或几个)新元素到栈顶
  push(element) {
    this.items.push(element);
  }
  // 移除栈顶的元素,同时返回被移除的元素
  pop() {
    return this.items.pop();
  }
  // 返回栈顶的元素,不对栈做任何修改(该方法不会移除栈顶的元素,仅仅返回它)
  peek() {
    return this.items[this.items.length - 1];
  }
  // 如果栈里没有任何元素就返回true,否则返回false
  isEmpty() {
    return this.items.length === 0;
  }
  // 返回栈里的元素个数。该方法和数组的length属性很类似
  size() {
    return this.items.length;
  }
  // 移除栈里的所有元素
  clear() {
    this.items = [];
  }
}

创建一个基于 JavaScript 对象的 Stack 类

class Stack {
  constructor() {
    // 栈的大小
    this.count = 0;
    this.items = {};
  }
  // 向栈中插入元素
  push(element) {
    this.items[this.count] = element;
    this.count++;
  }
  // 栈的大小
  size() {
    return this.count;
  }
  // 栈是否为空
  isEmpty() {
    return this.count === 0;
  }
  // 从栈中弹出元素
  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];
  }
  // 清空栈
  clear() {
    this.items = {};
    this.count = 0;
  }
  // 创建一个toString方法来像数组一样打印出栈的内容
  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;
  }
}

保护数据结构内部元素

const _items = Symbol("stackItems");

class Stack {
  constructor() {
    this[_items] = [];
  }
  // 向栈中插入元素
  push(element) {
    this[_items].push(element);
  }
  // 从栈中弹出元素
  pop() {
    return this[_items].pop();
  }
  // 查看栈顶的值
  peek() {
    return this[_items][this[_items].length - 1];
  }
  // 栈是否为空
  isEmpty() {
    return this[_items].length === 0;
  }
  // 栈的大小
  size() {
    return this[_items].length;
  }
  // 清空栈
  clear() {
    this[_items] = [];
  }
  // 打印栈
  print() {
    console.log(this.toString());
  }
  // 创建一个toString方法来像数组一样打印出栈的内容
  toString() {
    return this[_items].toString();
  }
}

用 ES2015 的 WeakMap 实现类

const _items = new WeakMap();
const _count = new WeakMap();

class Stack {
  constructor() {
    _count.set(this, 0);
    _items.set(this, {});
  }
  // 向栈中插入元素
  push(element) {
    const items = _items.get(this);
    const count = _count.get(this);
    items[count] = element;
    _count.set(this, count + 1);
  }
  // 从栈中弹出元素
  pop() {
    if (this.isEmpty()) {
      return undefined;
    }
    const items = _items.get(this);
    let count = _count.get(this);
    count--;
    _count.set(this, count);
    const result = items[count];
    delete items[count];
    return result;
  }
  // 查看栈顶的值
  peek() {
    if (this.isEmpty()) {
      return undefined;
    }
    const items = _items.get(this);
    const count = _count.get(this);
    return items[count - 1];
  }
  // 栈是否为空
  isEmpty() {
    return _count.get(this) === 0;
  }
  // 栈的大小
  size() {
    return _count.get(this);
  }

  clear() {
    /* while (!this.isEmpty()) {
        this.pop();
      } */
    _count.set(this, 0);
    _items.set(this, {});
  }
  // 清空栈
  toString() {
    if (this.isEmpty()) {
      return "";
    }
    const items = _items.get(this);
    const count = _count.get(this);
    let objString = `${items[0]}`;
    for (let i = 1; i < count; i++) {
      objString = `${objString},${items[i]}`;
    }
    return objString;
  }
}

从十进制到二进制

function decimalToBinary(decNumber) {
  const remStack = new Stack();
  let number = decNumber;
  let rem;
  let binaryString = "";
  while (number > 0) {
    rem = Math.floor(number % 2);
    remStack.push(rem);
    number = Math.floor(number / 2); // {4}
  }
  while (!remStack.isEmpty()) {
    binaryString += remStack.pop().toString();
  }
  return binaryString;
}

进制转换算法

// 把十进制转换成基数为2~36的任意进制
function baseConverter(decNumber, base) {
  const remStack = new Stack();
  const digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  let number = decNumber;
  let rem;
  let baseString = "";
  if (!(base >= 2 && base <= 36)) {
    return "";
  }
  while (number > 0) {
    rem = Math.floor(number % base);
    remStack.push(rem);
    number = Math.floor(number / base);
  }
  while (!remStack.isEmpty()) {
    baseString += digits[remStack.pop()];
  }
  return baseString;
}

Ctrl+Y Ctrl+Z 控制历史记录前进后退

class History {
  constructor() {
    this.items = []; // 数据的数组
    this.current = 0; //  当前位置
  }
  size() {
    return this.items.length;
  }
  insert(element) {
    if (this.items.length === 0) {
      // 首次插入的值
      this.items.push(element);
    } else {
      // 再插入就把当前位置current后面的数据都删除再插入
      this.items.splice(this.current + 1, this.size(), element);
      this.current++;
    }
  }
  // 前进 Ctrl+Y
  forword() {
    if (this.size() === 0) return "";
    if (this.current < this.size() - 1) {
      return this.items[++this.current];
    } else if (this.current === this.size() - 1) {
      // 数组最末端
      return this.items[this.current];
    }
  }
  // 后退 Ctrl+Z
  back() {
    if (this.size() === 0) return "";
    if (this.current > 0) {
      return this.items[--this.current];
    } else if (this.current === 0) {
      // 数组最前端
      return this.items[0];
    }
  }
  // 查看该类的数组
  log() {
    return this.items;
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值