TypeScript数据结构
时间: 2023-11-20 09:40:19 浏览: 150
在使用TypeScript构建数据结构时,您可以基于数组或链表实现栈和队列结构。对于栈结构,您可以使用基于数组的实现方法。以下是一个示例代码:
```typescript
class Stack<T> {
private arr: T[] = [];
push(ele: T) {
this.arr.push(ele);
}
pop(): T | undefined {
return this.arr.pop();
}
peek(): T {
return this.arr[this.arr.length - 1];
}
isEmpty(): boolean {
return this.arr.length === 0;
}
size(): number {
return this.arr.length;
}
}
export default Stack;
```
对于队列结构,您也可以使用基于数组的实现方法。以下是一个示例代码:
```typescript
class Queue<T> {
private arr: T[] = [];
enqueue(ele: T) {
this.arr.push(ele);
}
dequeue(): T | undefined {
return this.arr.shift();
}
peek(): T {
return this.arr
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)