使用typescript实现单向链表

跟風遠走 提交于 2020-01-31 13:59:44
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方法。 剩下的后期补充。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!