3__队列和双端队列__

本文介绍了队列和双端队列的数据结构实现,包括它们的添加、移除元素的方法。同时,展示了如何使用队列实现‘击鼓传花’游戏以及检查字符串是否为回文的算法。通过这两个实例,深入理解了队列和双端队列在实际问题中的应用。
摘要由CSDN通过智能技术生成

队列和双端队列(先进先出)

创建队列

class Queue {
  constructor() {
    // 队列的大小
    this.count = 0;
    // 队列第一个元素
    this.lowestCount = 0;
    this.items = {};
  }
  // 向队列添加元素,新的项只能添加到队列末尾
  enqueue(element) {
    this.items[this.count] = element;
    this.count++;
  }
  // 从队列移除元素
  dequeue() {
    if (this.isEmpty()) {
      return undefined;
    }
    const result = this.items[this.lowestCount];
    delete this.items[this.lowestCount];
    this.lowestCount++;
    return result;
  }
  // 查看队列头元素
  peek() {
    if (this.isEmpty()) {
      return undefined;
    }
    return this.items[this.lowestCount];
  }
  // 检查队列是否为空并获取它的长度
  isEmpty() {
    return this.count - this.lowestCount === 0;
    // 或者
    // return this.size() === 0
  }
  // 返回队列包含的元素个数
  size() {
    return this.count - this.lowestCount;
  }
  // 清空队列
  clear() {
    this.items = {};
    this.count = 0;
    this.lowestCount = 0;
  }
  // 创建toString方法
  toString() {
    if (this.isEmpty()) {
      return "";
    }
    let objString = `${this.items[this.lowestCount]}`;
    for (let i = this.lowestCount + 1; i < this.count; i++) {
      objString = `${objString},${this.items[i]}`;
    }
    return objString;
  }
}

双端队列数据结构

class Deque {
  constructor() {
    // 队列的大小
    this.count = 0;
    // 队列前端第一个元素下标
    this.lowestCount = 0;
    this.items = {};
  }
  // 在双端队列前端添加新的元素
  addFront(element) {
    if (this.isEmpty()) {
      this.addBack(element);
    } else if (this.lowestCount > 0) {
      this.lowestCount--;
      this.items[this.lowestCount] = element;
    } else {
      for (let i = this.count; i > 0; i--) {
        this.items[i] = this.items[i - 1];
      }
      this.count++;
      this.items[0] = element;
    }
  }
  // 在双端队列后端添加新的元素
  addBack(element) {
    this.items[this.count] = element;
    this.count++;
  }
  // 从双端队列前端移除第一个元素
  removeFront() {
    if (this.isEmpty()) {
      return undefined;
    }
    const result = this.items[this.lowestCount];
    delete this.items[this.lowestCount];
    this.lowestCount++;
    return result;
  }
  // 从双端队列后端移除第一个元素
  removeBack() {
    if (this.isEmpty()) {
      return undefined;
    }
    this.count--;
    const result = this.items[this.count];
    delete this.items[this.count];
    return result;
  }
  // 返回双端队列前端的第一个元素
  peekFront() {
    if (this.isEmpty()) {
      return undefined;
    }
    return this.items[this.lowestCount];
  }
  // 返回双端队列后端的第一个元素
  peekBack() {
    if (this.isEmpty()) {
      return undefined;
    }
    return this.items[this.count - 1];
  }
  // 检测队列是否为空
  isEmpty() {
    return this.size() === 0;
  }
  // 清空队列
  clear() {
    this.items = {};
    this.count = 0;
    this.lowestCount = 0;
  }
  // 队列大小
  size() {
    return this.count - this.lowestCount;
  }
  // 打印
  toString() {
    if (this.isEmpty()) {
      return "";
    }
    let objString = `${this.items[this.lowestCount]}`;
    for (let i = this.lowestCount + 1; i < this.count; i++) {
      objString = `${objString},${this.items[i]}`;
    }
    return objString;
  }
}

循环队列——击鼓传花游戏

// elementsList传入的数组 num为随机数
function hotPotato(elementsList, num) {
  const queue = new Queue();
  // 被淘汰的人数组
  const eliminatedList = [];
  // 写入队列
  for (let i = 0; i < elementsList.length; i++) {
    queue.enqueue(elementsList[i]);
  }

  while (queue.size() > 1) {
    // 传花,打乱队列
    for (let i = 0; i < num; i++) {
      queue.enqueue(queue.dequeue());
    }
    // 把倒霉蛋拿出来
    eliminatedList.push(queue.dequeue());
  }

  return {
    eliminated: eliminatedList,
    // 最后的幸运儿
    winner: queue.dequeue(),
  };
}

回文检查器(回文是正反都能读通的单词、词组、数或一系列字符的序列)

function palindromeChecker(aString) {
  if (
    aString === undefined ||
    aString === null ||
    (aString !== null && aString.length === 0)
  ) {
    return false;
  }
  const deque = new Deque();
  // 变小写后把字符串移除所有空格,比如 a bb a => abba
  const lowerString = aString.toLocaleLowerCase().split(" ").join("");
  let firstChar;
  let lastChar;
  // 字符串写入队列
  for (let i = 0; i < lowerString.length; i++) {
    deque.addBack(lowerString.charAt(i));
  }
  // 首尾字符相比
  while (deque.size() > 1) {
    firstChar = deque.removeFront();
    lastChar = deque.removeBack();
    if (firstChar !== lastChar) {
      return false;
    }
  }

  return true;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值