10. JavaScript数据结构之双向链表

91 阅读1分钟

双向链表

封装

function DoubleLinkedList() {
    function Node(data) {
        this.data = data;
        this.pre = null;
        this.next = null;
    }
    // 头指针
    this.head = null;
    // 尾指针
    this.tail = null;

    this.length = 0;
}

向尾部添加元素

DoubleLinkedList.prototype.append = function (data) {
    var node = new Node(data);
    if (this.length == 0) {
        // 
        this.head = node;
        this.tail = node;
    } else {
        // 新加入节点的前指针指向原来的最后一个节点
        node.pre = this.tail;
        // 原来的最后一个节点的后指针指向新加入节点
        this.tail.next = node;
        this.tail = node;

    }
    this.length += 1;
}

向指定位置插入一个新的元素

DoubleLinkedList.prototype.insert = function (position, data) {
    if (position < 0 || position > this.length) {
        return
    }

    var newNode = new Node(data);

    if (this.length == 0) {
        // 
        this.head = node;
        this.tail = node;
    } else {
        // 加入第一个位置
        if (position == 0) {
            // 原来的第一个节点的前指针指向新加入节点
            this.head.pre = newNode;
            // 新加入节点指向原来的第一个节点
            newNode.next = this.head;
            // 头节点指向新加入节点
            this.head = newNode;

        }
        // 加入最后一个位置
        else if (position == this.length) {
            // 新加入节点的前指针指向原来的最后一个节点
            node.pre = this.tail;
            // 原来的最后一个节点的后指针指向新加入节点
            this.tail.next = newNode;
            this.tail = newNode;
        } else {
            var current = this.head;
            var index = 0;
            while (index++ < position) {
                current = current.next;
            }
            // 新加入节点的前指针指向原来这个位置节点的前一个节点
            newNode.pre = current.pre;
            // 新加入节点后指针指向原来这个位置的节点
            newNode.next = current;
            // 原来这个位置的节点的前一个节点的后指针指向新加入节点
            current.pre.next = newNode;
            // 原来这个位置的节点的前指针指向新加入节点
            current.pre = newNode;
        }
    }
    this.length += 1;
}

向前遍历

DoubleLinkedList.prototype.forwardToString = function () {
    var current = this.tail;
    var str = '';
    while (current) {
        str += current.data + " ";
        current = current.pre;
    }
    return str
}

向后遍历返回的字符串

DoubleLinkedList.prototype.backToString = function () {
    var current = this.head;
    var str = '';
    while (current) {
        str += current.data + " ";
        current = current.next;
    }
    return str
}

获取对应位置的节点

 DoubleLinkedList.prototype.get = function (position) {

    if (position < 0 || position > this.length) {
        return
    }
    var current = this.head;
    var index = 0;
    while (index++ < position) {
        current = current.nex;
    }
    return current.data;
}

获取某个元素的下标

DoubleLinkedList.prototype.indexOf = function (data) {
    var current = this.head;
    var index = 0;

    while (current) {
        // 判断数据是否相同
        if (current.data == data) {
            return index;
        }
        // 向后遍历
        current = current.next;
        index++
    }
    return -1;
}

更新某个位置节点的内容

DoubleLinkedList.prototype.update = function (position, data) {
    if (position < 0 || position > this.length) {
        return
    }
    var current = this.head;
    var index = 0;
    while (index++ < position) {
        current = current.next;
    }
    current.data = data;
}

移除特定位置

DoubleLinkedList.prototype.removeAt = function (position) {
    if (position < 0 || position >= this.length) {
        return
    }
    console.log(11);
    // 只有一个节点
    if (this.length == 1) {
        this.head = null;
        this.tail = null;
        console.log(11);
    }// 加入第一个位置
    else if (position == 0) {
        // 第二个位置的节点的前指针指向空
        this.head.next.pre = null; console.log(11);
        // 头节点指向第二个节点
        this.head = this.head.next;
        // 删除最后一个节点
    } else if (position == this.length) {
        // 倒数第二个节点后指针指向空
        this.tail.pre.next = null; console.log(11);
        // 尾指针指向倒数第二个节点
        this.tail = this.tail.pre;
    } else {
        var index = 0; console.log(11);
        var current = this.head;
        while (index++ < position) {
            current = current.next;
        }
        // 删除节点的前一个节点的后指针指向删除节点的后一个节点
        current.pre.next = current.next;
        // 删除节点的后一个节点的前指针指向删除节点的前一个节点
        current.next.pre = current.pre;

    }
    this.length -= 1;
}

移除某项

DoubleLinkedList.prototype.remove = function(data) {
    var position = this.indexOf(data);
    this.removeAt(position)
}