TS实现栈结构
栈是一种受限制的数组结构,其特点为“先进后出”。
//定义栈结构的接口
interface IStack<T> {
push(element: T): void
pop(): T | undefined
peek(): T | undefined
isEmpty(): boolean
size(): number
}
//封装基于数组实现的栈类
class arrayStack<T> implements IStack<T> {
private data: T[] = []
push(element: T): void {
this.data.push(element)
}
pop(): T | undefined {
return this.data.pop()
}
peek(): T | undefined {
return this.data[this.data.length-1]
}
isEmpty(): boolean {
return this.data.length === 0
}
size(): number {
return this.data.length
}
}
#ts##数据结构##前端#