JS实现双向链表

JS实现双向链表

function DoublyLinkedList() {
    var Node = function (element) {
        this.element = element; 
        this.prev = null; 
        this.next = null; 
    }; 

    var length = 0, 
        head = null; 
    //向尾部追加
    this.append = function (element) {
        var node = new Node(element), 
            current, 
            previous; 

        if ( ! head) {
            head = node; 
        }else {
            current = head; 
            while (current) {
                previous = current; 
                current = current.next; 
            }
            previous.next = node
            node.prev = previous
        }
        length ++ ; 
        return true; 
    }
    //指定位置插入
    this.insert = function (position, element) {
        if (position > -1 && position <= length) {
            var node = new Node(element), 
                current = head, 
                previous, 
                index = 0; 
            if (position === 0) {
                if ( ! head) {
                    head = node; 
                }else {
                    node.next = head; 
                    head.prev = node; 
                    head = node; 
                }
            }else {
                while (index ++  < position) {
                    previous = current; 
                    current = current.next; 
                }
                if (position != length) {
                    node.next = current; 
                    current.prev = node; 
                }
                previous.next = node; 
                node.prev = previous; 
            }
            length ++ ; 
            return true; 
        }else {
            return false; 
        }
    }; 
    //删除指定位置元素
    this.removeAt = function (position) {
        if (position > -1 && position < length) {
            var current = head, 
                index = 0, 
                previous; 
            if (position === 0) {
                head = head.next; 
                head.prev = null;
            }else {
                while (index ++  < position) {
                    previous = current; 
                    current = current.next; 
                }

                if(position === length - 1){
                    previous.next =null;
                }else{
                    previous.next = current.next; 
                    current.next.prev = previous; 
                }              
            }; 
            length--; 
            return current.element; 
        }else {
            return false; 
        }
    }; 
    //删除值为element的所有元素
    this.removeEle = function (element) {
        var current = head, 
            previous,
            num=0; 
        if (current.element === element) {
            head = current.next; 
        }
        previous = current; 
        current = current.next; 

        while (current) {
            if (current.element == element) {
                previous.next = current.next;
                if(current.next){
                    current.next.prev = previous; 
                }
                current=current.next;
                length--;
                num++;
            }else{
                previous = current; 
                current = current.next; 
            }
            
        }
        return num; 
    }; 
    //删除尾部
    this.remove = function () {
        if (length === 0) {
            return false; 
        }; 

        var current = head, 
            previous; 

        if (length === 1) {
            head = null; 
            length--; 
            return current.element; 
        }

        while (current.next) {
            previous = current; 
            current = current.next; 
        }

        previous.next = null; 
        length--; 
        return current.element; 
    }; 
    //当前元素的其实位置
    this.indexOf = function (element) {
        var current = head, 
            index = 0; 

        while (current && index < length) {
            if (current.element === element) {
                return index; 
            }; 
            current = current.next;
            index++; 
        }

        return false; 
    }; 
    //是否为空
    this.isEmpty = function () {
        return length === 0; 
    }; 
    //链表长度
    this.size = function () {
        return length; 
    }; 
    //转成字符串
    this.toString = function () {
        var current = head, 
            string = ''; 

        while (current) {
            string += current.element; 
            current = current.next; 
        }
        return string; 
    }; 
    //获取头结点元素
    this.getHead = function () {
        return head.element; 
    }; 
    //获取未结点元素
    this.getTail = function () {
        var previous,current=head;
        while(current){
            previous=current;
            current=current.next;
        }
        return previous.element; 
    }; 
}

let myLink = new DoublyLinkedList(); 
myLink.append('A')
myLink.append('B')
myLink.append('C')
myLink.append('E')
myLink.insert(3, 'D')
myLink.insert(5, 'F')
myLink.insert(0, 'G')
console.log(myLink.toString()) //GABCDEF
myLink.removeAt(0) //删除G
myLink.removeAt(5) //删除F
console.log(myLink.remove()) //删除E
console.log(myLink.toString()) // ABCD
myLink.append('D') //向尾部增加D
console.log(myLink.toString()) //ABCDD
console.log(myLink.removeEle('D')) //删除所有D,打印删除D的个数 2
console.log(myLink.toString()) //ABC
console.log(myLink.indexOf('B')) //打印B的位置 1
console.log(myLink.size()) //打印链表的长度 3
console.log(myLink.getHead()) //A
console.log(myLink.getTail()) //C

 双向循环链表:将双向链表的头尾指针相连,就构成了双向循环链表。这种链表从任意一个节点都可以同时向两个方向进行节点遍历。

 

posted @ 2018-11-05 14:23  .追风逐月  阅读(2085)  评论(0编辑  收藏  举报