使用typescript实现单向链表

interface node<T>{
    next:node<T> | null;
    element:T
}

interface Data {
    name: string,
    age: number
}

class LianBiao<T> {
    length:number = 0
    head:node<T> |null = null
    append (element:node<T>) {
        var node:node<T> = element
        if (this.head === null) {
            this.head  = node
        } else {
            var current = this.head
            while (current.next) {
                current = current.next
            }
            current.next = node
        }
        this.length++
    }
}
class LianbiaoNode<T> implements node<T> {
    element:T
    next:node<T> | null = null
    constructor (element:T, Linext:node<T>|null = null) {
        this.element = element
        this.next = Linext
    }
}

var testNode = new LianbiaoNode<Data>({ name: '张三',age: 20})
var biao = new LianBiao<Data>()
var list = biao.append(testNode)

  该链表保证了链表中每一项元素都是<Data>规定的格式。但是目前只实现了一个append方法。 剩下的后期补充。

posted @ 2019-06-09 15:28  四点钟-city  阅读(556)  评论(0编辑  收藏  举报