数据结构与算法:栈和队列

这篇具有很好参考价值的文章主要介绍了数据结构与算法:栈和队列。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1 栈

栈是一种后入先出(LIFO)的线性逻辑存储结构。只允许在栈顶进行进出操作。

数据结构与算法:栈和队列

1.1 栈基本操作

基本操作包括:入栈(push)/出栈(pop)/获取栈顶元素(peek)。

栈的实现主要有两种: 1. 数组实现,即顺序栈 2. 链表实现,即链式栈

无论是以数组还是以链表实现,入栈、出栈的时间复杂度都是O(1)。

栈的应用比如函数执行/括号匹配/表达式计算/浏览器前进后退。

数据结构与算法:栈和队列 数据结构与算法:栈和队列  

1.2 设计栈

1.2.1 数组实现栈

class ArrayStack<T> {
  items: T[];
  constructor() {
    this.items = [];
  }
  /**
   * 入栈
   * @param item
   */
  push(item: T) {
    this.items.push(item);
  }
  /**
   * 出栈
   * @returns
   */
  pop() {
    if (this.isEmpty()) throw new Error('栈空');
    return this.items.pop();
  }
  /**
   * 获取栈顶元素
   * @returns
   */
  peek() {
    if (this.isEmpty()) throw new Error('栈空');
    return this.items[this.items.length - 1];
  }
  /**
   * 判空
   * @returns
   */
  isEmpty() {
    return this.items.length === 0;
  }
  /**
   * 获取栈元素的个数
   * @returns
   */
  getSize() {
    return this.items.length;
  }
}

1.2.2 链表实现栈

class LinkStack<T> {
  // 栈的长度
  size: number;
  // 栈顶指针
  top: LinkNode<T> | null;
  constructor() {
    this.size = 0;
    this.top = null;
  }
  /**
   * 入栈
   * @param item
   */
  push(val: T) {
    let node = new LinkNode(val);
    if (this.top === null) {
      // 栈空
      this.top = node;
    } else {
      // 栈非空
      node.next = this.top;
      this.top = node;
    }
    this.size = this.size + 1;
  }
  /**
   * 出栈
   * @returns
   */
  pop() {
    if (this.top === null) {
      // 栈空
      throw new Error('栈空');
    } else {
      // 栈非空
      const data = this.top.val; // 栈顶元素值
      this.top = this.top.next; // 新栈顶
      this.size = this.size - 1;
      return data;
    }
  }
  /**
   * 获取栈顶元素
   * @returns
   */
  peek() {
    if (this.top === null) {
      // 栈空
      throw new Error('栈空');
    } else {
      return this.top.val;
    }
  }
  /**
   * 判空
   * @returns
   */
  isEmpty() {
    return this.top === null;
  }
  /**
   * 获取栈元素的个数
   * @returns
   */
  getSize() {
    return this.size;
  }
}

1.3 剑指 offer 栈算法题( typescript 版)

包含min函数的栈

栈的压入、弹出序列

2 队列

队列是一种先入先出(FIFO)的线性逻辑存储结构。只允许在队首进行出队(即delete删除)操作,队尾进行入队(即insert插入)操作。

2.1 队列基本操作

队列的基本操作包括:入队 (enqueue)/ 出队 (dequeue)/ 获取队头元素(peek)

队列的实现主要有两种: 1. 数组实现,即顺序队列 2. 链表实现,即链式队列。

无论是以数组还是以链表实现,入队、出队的时间复杂度都是O(1)。

队列的应用比如线程池、资源池、消息队列、异步队列。

数据结构与算法:栈和队列

2.2 设计队列

2.2.1 数组顺序队列

使用数组实现,使用shift出队时每次都要移动队列元素,效率不高。改进方案是可以队列初始化时就需要规定队列长度通过判断队尾是否有空间,有就让元素一直入队,直到队尾没有空间位置,然后进行整体进行一次搬移,这样优化了入队的效率,平均时间复杂度还是 O(1)。

class ArrayQueue<T> {
  items: T[];
  constructor() {
    this.items = [];
  }
  /**
   * 入队
   * @param item
   */
  push(item: T) {
    this.items.push(item);
  }
  /**
   * 出队
   * @returns
   */
  pop() {
    if (this.isEmpty()) throw new Error('队列空');
    return this.items.shift();
  }
  /**
   * 获取队顶元素
   * @returns
   */
  peek() {
    if (this.isEmpty()) throw new Error('队列空');
    return this.items[0];
  }
  /**
   * 判空
   * @returns
   */
  isEmpty() {
    return this.items.length === 0;
  }
  /**
   * 获取队元素的个数
   * @returns
   */
  getSize() {
    return this.items.length;
  }
}

2.2.2 数组循环队列

数组实现,初始化需指定队列容量capacity,留一个空位,队空条件 head = tail,队满条件 head =( tail + 1) % capacity,队列元素个数(tail - head + capacity) % capacity)。

class LoopQueue {
  // 存放元素的数组
  values: (number | undefined)[];
  // 当前元素个数
  count: number;
  // 队的长度
  capacity: number;
  // 队尾
  head: number;
  // 队尾
  tail: number;
  constructor(capacity: number) {
    this.head = 0;
    this.tail = 0;
    this.capacity = capacity;
    this.count = 0;
    this.values = new Array(capacity);
  }
  /**
   * 入队
   * @param item
   */
  enQueue(val: number) {
    if (this.isFull()) {
      throw new Error('队满');
    }
    this.values[this.tail] = val;
    this.tail = (this.tail + 1) % this.capacity;
    this.count = this.count + 1;
    return true;
  }
  /**
   * 出队
   * @returns
   */
  deQueue(): number {
    if (this.isEmpty()) {
      throw new Error('队空');
    }
    const value = this.values[this.head] as number;
    this.values[this.head] = undefined;
    this.head = (this.head + 1) % this.capacity;
    this.count = this.count - 1;
    return value;
  }
  /**
   * 获取队头元素
   * @returns
   */
  peek() {
    if (this.isEmpty()) {
      throw new Error('队空');
    }
    const value = this.values[this.head];
    return value;
  }
  /**
   * 判空
   * @returns
   */
  isEmpty() {
    // 或 return this.head === this.tail
    return this.count === 0;
  }
  /**
   * 判满
   * @returns
   */
  isFull() {
    // 或 return this.head === (this.tail + 1) % this.capacity
    return this.count === this.capacity - 1;
  }
  /**
   * 获取队元素的个数
   * @returns
   */
  getSize() {
    return this.count;
  }
  /**
   * 清空队列
   * @returns
   */
  clear() {
    this.head = 0;
    this.tail = 0;
    this.count = 0;
    this.values = new Array(this.capacity);
    return true;
  }
}

2.2.3 链式顺序队列

链表实现,链表尾入队,链表头出队

class LinkQueue<T> {
  // 队的长度
  size: number;
  // 队尾指针
  head: LinkNode<T> | null;
  // 队尾指针
  tail: LinkNode<T> | null;
  constructor() {
    this.size = 0;
    this.head = null;
    this.tail = null;
  }
  /**
   * 入队
   * @param item
   */
  enQueue(val: T) {
    let node = new LinkNode(val);
    if (this.size === 0) {
      this.head = node;
      this.tail = node;
    } else {
      this.tail!.next = node;
      this.tail = this.tail!.next;
    }
    this.size = this.size + 1;
  }
  /**
   * 出队
   * @returns
   */
  deQueue() {
    if (this.size === 0) {
      // 队空
      throw new Error('队空');
    } else {
      // 队非空
      const node = this.head;
      this.head = node!.next;
      this.size = this.size - 1;
      return node!.val;
    }
  }
  /**
   * 获取队头元素
   * @returns
   */
  peek() {
    if (this.size === 0) {
      // 队空
      throw new Error('队空');
    } else {
      return this.head!.val;
    }
  }
  /**
   * 判空
   * @returns
   */
  isEmpty() {
    return this.size === 0;
  }
  /**
   * 获取队元素的个数
   * @returns
   */
  getSize() {
    return this.size;
  }
}

2.3 剑指 offer 队列算法题( typescript 版)

两个栈实现队列文章来源地址https://www.toymoban.com/news/detail-512740.html

到了这里,关于数据结构与算法:栈和队列的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 青岛大学_王卓老师【数据结构与算法】Week05_01_栈和队列的定义和特点1_学习笔记

    本文是个人学习笔记,素材来自青岛大学王卓老师的教学视频。 一方面用于学习记录与分享, 另一方面是想让更多的人看到这么好的《数据结构与算法》的学习视频。 如有侵权,请留言作删文处理。 课程视频链接: 数据结构与算法基础–第05周01–3.1栈和队列的定义和特点

    2024年02月15日
    浏览(34)
  • 【数据结构】栈和队列(队列篇)

    上期我们已经学习了数据结构中的栈,这期我们开始学习队列。 目录 1.队列的概念及结构 2.队列的实现 队列结构体定义 常用接口函数 初始化队列 队尾入队列 队头出队列 获取队列头部元素、 获取队列队尾元素 获取队列中有效元素个数 检测队列是否为空 销毁队列 3.循环队

    2024年02月13日
    浏览(39)
  • 数据结构——栈和队列

    目录  一.前言 二.前文回顾 三.栈 3.1 栈的概念及结构 3.2 栈的实现 3.2.1 初始化函数 3.2.2 销毁函数 3.2.3 入栈函数 3.2.4 出栈函数 3.2.5 计算大小函数 3.2.6 空栈函数 3.2.7 获取栈顶函数  3.2.8 小测试 3.3 全部代码 四.栈的练习 4.1 有效的括号 五.队列 5.1队列的概念及结构 5.2 队列的实

    2024年01月25日
    浏览(42)
  • 数据结构:栈和队列

    朋友们、伙计们,我们又见面了,本期来给大家解读一下栈和队列方面的相关知识点,如果看完之后对你有一定的启发,那么请留下你的三连,祝大家心想事成! C 语 言 专 栏:C语言:从入门到精通 数据结构专栏:数据结构 个 人 主 页 :  stackY、 目录 前言:  1.栈 1.1栈的

    2024年02月06日
    浏览(35)
  • 数据结构---栈和队列

    栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作,进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。 压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。 出栈:栈的删除操作

    2024年01月18日
    浏览(36)
  • 栈和队列【数据结构】

    2024年02月16日
    浏览(37)
  • 数据结构--栈和队列

    栈是一种常见的数据结构,它遵循 后进先出LIFO (Last In First Out)的原则。 进行数据插入和操作的一端称为栈顶,另一端称为栈底 。 压栈 :栈的插入操作被称为压栈/进栈/入栈,入数据在栈顶。 出栈 :栈的删除操作。出数据也在栈顶; 栈可以用 数组 或者是 链表 来实现;

    2024年02月09日
    浏览(36)
  • 数据结构【栈和队列】

    一、栈 1.定义:只允许一端进行插入和删除的线性表,结构与手枪的弹夹差不多,可以作为实现递归函数(调用和返回都是后进先出)调用的一种数据结构; 栈顶:允许插入删除的那端; 栈底:固定的,不允许插入或删除; 空栈:不含元素; 2.特点:后进先出; 3.操作:入

    2024年02月15日
    浏览(44)
  • 数据结构 | 栈和队列

    … 📘📖📃本文已收录至:数据结构 | C语言 更多知识尽在此专栏中! 栈(Stack) 又名堆栈,它是一种运算受限的线性表,限定仅在表尾进行插入和删除操作的线性表。 队列(Queue) 也是一种特殊的线性表,特殊之处在于它只允许在表的前端(Front)进行删除操作,而在表的

    2024年01月23日
    浏览(39)
  • [数据结构】栈和队列

    目录 1.栈 1.1概念 1.2 栈的使用 1.3.栈的模拟实现 2.队列 2.1概念 2.2队列的使用 2.3队列的模拟实现 2.4 循环队列 2.5双端队列   栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素

    2024年02月07日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包