使用TypeScripts实现双向链表

340 阅读1分钟

1.节点类DoublyLinkNode

class DoublyLinkNode<T>{
    public prev: DoublyLinkNode<T> | null;
    public next: DoublyLinkNode<T> | null;
    constructor(public element:T) {
        this.prev = null;
        this.next = null;
    }
}

2.双向链表DoublyLinkList

class DoublyLinkList<K>{
    protected head: DoublyLinkNode<K> | null;
    protected last: DoublyLinkNode<K> | null;
    protected count: number;
    constructor() {
        this.head = null;
        this.last = null;
        this.count = 0;
    }
    /**
     * 向链表尾部添加节点元素
     * @param element 
     */
    append(element: K) {
        const node = new DoublyLinkNode(element);
        if (this.head && this.last) {
            this.last.next = node;
            node.prev = this.last;
            this.last = node;
        } else {
            this.head = node;
            node.prev = null;
            this.last = node;
        }
        this.count++;
    }

    /**
     * 查找指定位置的元素
     * @param index 
     */
    findElByIndex(index:number):DoublyLinkNode<K> | null {
        if (index >= 0 && index < this.count) {
            if (this.head) {
            let current = this.head;
             for (let tmp = 0; tmp < index; tmp++){
                 if (current.next) {
                     current = current.next;
                }
                }   
                return current;
            } else {
                return null;
            }
        }
        return null;
    }
    /**
     * 查找对应值的元素
     * @param element 
     */

    findElByValue(element: K):DoublyLinkNode<K> | null {
        if (this.head) {
            let current = this.head;
            while (current.next) {
                if (current.element = element) {
                    return current;
                }
                current = current.next;
            }
        }
        return null;
    }

    /**
     * 向指定位置添加一个节点元素
     * @param element 
     * @param index 
     */
    insert(index: number,element: K ):boolean {
        const node = new DoublyLinkNode(element);
        let current = this.findElByIndex(index);
        let lastNode = this.findElByIndex(this.count - 1);
        if (this.count === 0) {
            this.head = node;
            this.last = node;
            this.count++;
            return true
        } else {
            if (index === this.count && lastNode) {
                lastNode.next = node;
                this.last = node;
                this.count++;
                return true;
            }
            
            if (current) {
                if (current.prev==null) {
                    node.next = current;
                    this.head = node;
                }else {
                    node.next = current;
                    node.prev = current.prev;
                    current.prev.next = node;
                    current.prev = node;
                }
                this.count++;
                return true;
            }else {
                return false;
            }
        }

    }
    /**
     * 删除指定位置的元素
     * @param index 
     */
    
    removeByIndex(index: number) {
        let current = this.findElByIndex(index);
        if (current) {
            if (current.prev == null&&current.next==null) {
                this.head = null;
                this.last = null;
            } else if (current.next == null) {
                this.last = current.prev;
            } else if(current.prev == null){
                this.head = current.next                
            } else {
                current.prev.next = current.next;
                current.next.prev = current.prev;
            }
            this.count--;
            return true;
        } else {
            return false;
        }
    }
}