TypeScript数据结构与算法(1)最基础的数据结构-数组-Array
数组是数据结构中最基础、最简单的一种数据结构了,同时也是我们开发过程中,使用频率最高的,我使用了TypeScript实现了这个基础的数据结构。
数组类内部,需要提供给用户以下接口:
1.getSize():获取数组大小(也就是数组实际存放了多少元素)
2.getCapacity():获取数组容量(用户自定义)
3.isFull():判断数组是否已满
4.isEmpty():判断数组是否为空
5.add():往数组中添加元素
6.remove():在数组中移除元素
7.get():获取某个元素
8.set():设置某个元素
9.find():查找某个元素
10.contains():数组是否包含某个元素
下面是源码:
/** * Autor: Created by 李清风 on 2020-12-17. * Desc: 数组,关键词:扩容/缩容、元素移位,紧密排列、size的含义 */ export class DataStruct_Array<T> { private data: T[]; private size: number; //TypeScript的构造函数重载不太方便 //public constructor(capacity?, arr?) //构造器 public constructor(capacity: number, arr?: T[]) { this.data = new Array<T>(capacity); this.size = 0; } public getSize(): number { return this.size; } public getCapacity(): number { return this.data.length } public isFull(): boolean { return this.size == this.data.length; } public isEmpty(): boolean { return this.size == 0; } //在数组尾添加元素 public addLast(e: T) { this.add(this.size, e); } //添加元素到首 public addFirst(e: T) { this.add(0, e); } public add(index: number, e: T) { if (index < 0 || index > this.size) { throw new Error("LogError:Add failed.Require index >=0 and index<size."); } if (this.size == this.data.length) { this.resize(this.data.length * 2);//对数组进行扩容 } for (let i: number = this.size - 1; i >= index; i--) { this.data[i + 1] = this.data[i]; } // for (let i: number = this.size; i > index; i--) { // this.data[i] = this.data[i - 1]; // } this.data[index] = e; this.size++; } //因为使用了array来构造,这里只提供扩容的思路 private resize(expandCount: number) { let newData: T[] = new Array<T>(expandCount); for (let i = 0; i < this.size; i++) { newData[i] = this.data[i]; } this.data = newData; //内存引用地址转,等待GC回收 } //移除指定index的元素 public remove(index: number): T { if (index < 0 || index > this.size) { throw new Error("LogError:Add failed.Require index >=0 and index<size."); } let ret: T = this.data[index]; for (let i = index + 1; i < this.size; i++) { this.data[i - 1] = this.data[i]; //移位 } this.size--;//更新size this.data[this.size] = null; //引用类型要注意释放loitering objects if (this.size == this.data.length / 2) { this.resize(this.data.length); } return ret; } public removeFirst() { return this.remove(0); } public removeLast() { return this.remove(this.size - 1); } public removeElement(e: T) { let index = this.find(e); if (index > 0) { this.remove(index); } } public get(index: number): T { if (index < 0 || index > this.size) { throw new Error("LogError:Add failed.Require index >=0 and index<size."); } return this.data[index]; } public getFirst(): T { return this.get(0); } public getLast(): T { return this.get(this.size - 1); } public set(index: number, e: T) { if (index < 0 || index > this.size) { throw new Error("LogError:Add failed.Require index >=0 and index<size."); } this.data[index] = e; } public contains(e: T): boolean { for (let i = 0; i < this.size; i++) { if (this.data[i] == e) { return true; } } return false; } //寻找符合的元素,可以使用二分查找实现,但是需要对数组指定sort函数 public find(e: T): number { for (let i = 0; i < this.size; i++) { if (this.data[i] == e) { return i; } } return -1; } }

浙公网安备 33010602011771号