Skip to content

Latest commit

 

History

History
45 lines (35 loc) · 656 Bytes

File metadata and controls

45 lines (35 loc) · 656 Bytes

基于数组实现栈

定义栈接口

// 02.ts
interface Stack<T>{
    push(ele:T):void
    pop():T | undefined
    peek():T | undefined
    isEmpty():boolean
    size():number
}

export default Stack

实现栈的类

// 03.ts
class Stack<T=string> {
    private data: T[] = []

    push(ele:T):void{
        this.data.push(ele)
    }

    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
    }
}