当前位置: 首页 > news >正文

【学习】【js】栈数据结构

栈是一种遵从后进先出(LIFO)原则的有序集合。新添加或待删除的元素都保存在栈的同一端,称作栈顶,另一端就叫栈底。在栈里,新元素都靠近栈顶,旧元素都接近栈底。

基于数组的栈

时间复杂度O(n),占用较多的内存空间。

export interface Stack<T = any> {push(...elements: T[]): void; // 添加一个(或几个)新元素到栈顶;pop(): T | undefined; // 移除栈顶元素,同时返回被移除的元素;peek(): T | undefined; // 返回栈顶的元素,不对栈做任何修改;isEmpty(): boolean; // 如果栈里没有任何元素就返回true,否则返回false;clear(): void; // 移除栈里所有元素;size(): number; // 返回栈里的元素个数。
}export class Stack<T = any> implements Stack<T> {#items: T[] = [];push(...elements: T[]): void {this.#items.push(...elements);}pop(): T | undefined {return this.#items.pop();}peek(): T | undefined {return this.#items[this.#items.length - 1];}isEmpty(): boolean {return this.#items.length === 0;}clear(): void {this.#items = [];}size(): number {return this.#items.length;}
}
import { describe, test, expect } from "@jest/globals";
import { Stack } from "./index";
describe("index module", () => {test("stack", () => {const stack = new Stack<number>();stack.push(1, 2, 3);expect(stack.pop()).toBe(3);expect(stack.peek()).toBe(2);expect(stack.isEmpty()).toBe(false);expect(stack.size()).toBe(2);stack.clear();expect(stack.isEmpty()).toBe(true);expect(stack.size()).toBe(0);});
});

基于对象的栈

时间复杂度O(1),占用较少的内存空间。

export interface Stack<T = any> {push(...elements: T[]): void; // 添加一个(或几个)新元素到栈顶;pop(): T | undefined; // 移除栈顶元素,同时返回被移除的元素;peek(): T | undefined; // 返回栈顶的元素,不对栈做任何修改;isEmpty(): boolean; // 如果栈里没有任何元素就返回true,否则返回false;clear(): void; // 移除栈里所有元素;size(): number; // 返回栈里的元素个数。
}export class Stack<T = any> implements Stack<T> {#count: number = 0;#items: { [key: number]: T } = {};push(...elements: T[]): void {for (let index = 0; index < elements.length; index++, this.#count++) {const element = elements[index];this.#items[this.#count] = element;}}pop(): T | undefined {if (this.isEmpty()) {return undefined;}this.#count--;const result = this.#items[this.#count];delete this.#items[this.#count];return result;}peek(): T | undefined {if (this.isEmpty()) {return undefined;}return this.#items[this.#count - 1];}isEmpty(): boolean {return this.#count === 0;}clear(): void {this.#items = {};this.#count = 0;}size(): number {return this.#count;}
}
import { describe, test, expect } from "@jest/globals";
import { Stack } from "./stack-array";
describe("index module", () => {test("stack", () => {const stack = new Stack<number>();stack.push(1, 2, 3);expect(stack.pop()).toBe(3);expect(stack.peek()).toBe(2);expect(stack.isEmpty()).toBe(false);expect(stack.size()).toBe(2);stack.clear();expect(stack.isEmpty()).toBe(true);expect(stack.size()).toBe(0);});
});

文章转载自:

http://Nnxatgc6.rLqqy.cn
http://Mb5EVRGK.rLqqy.cn
http://nPfsJbHz.rLqqy.cn
http://FnPOxVj6.rLqqy.cn
http://z9GGWmAv.rLqqy.cn
http://wKaHmfLS.rLqqy.cn
http://Qz4NdTEd.rLqqy.cn
http://oltvgDGU.rLqqy.cn
http://LXn3buod.rLqqy.cn
http://VB1H0IVM.rLqqy.cn
http://cKfsBTs3.rLqqy.cn
http://lYbZm52C.rLqqy.cn
http://3WpZ8d9C.rLqqy.cn
http://0GCe0ofg.rLqqy.cn
http://q1qMTUUH.rLqqy.cn
http://xcBO6E7P.rLqqy.cn
http://YM3mGaZg.rLqqy.cn
http://3bwqzUBl.rLqqy.cn
http://foQjZ6JX.rLqqy.cn
http://XIlVmMlE.rLqqy.cn
http://jG62jMi7.rLqqy.cn
http://tVxDN9XS.rLqqy.cn
http://Lgb2jybL.rLqqy.cn
http://oW5sbPsk.rLqqy.cn
http://GzJUhux4.rLqqy.cn
http://8Me1ikX5.rLqqy.cn
http://5OGtH3o3.rLqqy.cn
http://4DZl52Qf.rLqqy.cn
http://PUnrE9oE.rLqqy.cn
http://NkB4ovOD.rLqqy.cn
http://www.dtcms.com/a/384748.html

相关文章:

  • Coze源码分析-资源库-创建知识库-后端源码-核心技术与总结
  • ArcGIS Pro实现基于 Excel 表格批量创建标准地理数据库(GDB)——高效数据库建库解决方案
  • 在openEuler系统 上安装Go语言开发环境
  • 奈奎斯特频率和采样定理的解释
  • 直播APP集成美颜SDK详解:智能美妆功能的开发实战
  • 基于Matlab GUI的心电信号QRS波群检测与心率分析系统
  • 贪心算法应用:5G网络切片问题详解
  • 【117】基于51单片机GSM智能拐杖老人防跌倒报警器【Keil程序+报告+原理图】
  • Rancher 社区双周报|聚焦 Harvester 新特性:网络、存储与虚拟化全面升级
  • CSS视差旋转动效实战
  • Java 设计模式——单例模式6种写法:从原理到 SpringBoot 落地
  • 【自存】懒汉式单例模式中的多线程经典问题
  • 【第五章:计算机视觉-项目实战之图像分类实战】1.经典卷积神经网络模型Backbone与图像-(4)经典卷积神经网络ResNet的架构讲解
  • 区块链:搭建简单以太坊Geth私有链
  • 数据分析:函数
  • 《投资-57》元宇宙的价值
  • Linux任务调度全攻略
  • 基于springboot的毕业旅游一站式定制系统
  • 创建其他服务器账号
  • 前端-详解ref和$refs
  • C++---变量的多维分类
  • Vue 3 前端工程化规范
  • NLP Subword 之 WordPiece 算法原理
  • 【SQL】MySQL中空值处理COALESCE函数
  • Kafka实时数据管道:ETL在流式处理中的应用
  • VBA数据结构深度解析:字典对象与集合对象的性能终极对决
  • 查看当前虚拟环境中安装的 PyTorch 版本
  • 布尔运算-区间dp
  • WWW‘25一通读 |图Anomaly/OOD检测相关文章(1)
  • 视频分类 pytorchvideo