javascript普通链表及双向链表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript普通链表及双向链表相关的知识,希望对你有一定的参考价值。

写代码的真是心细啊,每一步操作的先后顺序都在卡准。

我其实只是理解了思想和大概的操作。

真正要用时,可能还是要复制,粘贴。。。:)

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

    var length = 0;
    var head = null;

    this.append = function(element){
        var node = new Node(element),
            current;

        if (head == null){
            head = node;
        } else {
            current = head;
            while (current.next){
                current = current.next;
            }
            current.next = node;
        }
        length++;
    };

    this.insert = function(position, element){
        if (position >=0 && position <=length){
            var node = new Node(element),
                current = head,
                previous,
                index =0;
            if (position === 0){
                node.next = current;
                head = node;
            } else {
                while (index++ < position){
                    previous = current;
                    current = current.next;
                }
                node.next = current;
                previous.next = node;
            }
            length++;
            return true;
        } else {
            return false;
        }
    };

    this.removeAt = function(position){
        if (position > -1 && position < length){
            var current = head,
                previous,
                index = 0;
            if (position === 0){
                head = current.next;
            } else {
                while (index++ < position){
                    previous = current;
                    current = current.next;
                }
                previous.next = current.next;
            }
            length--;
            return current.element;
        } else {
            return null;
        }
    };

    this.remove = function(element){
        var index = this.indexOf(element);
        return this.removeAt(index);
    };

    this.indexOf = function(element){
        var current = head,
            index = -1;
        while (current){
            if (element == current.element){
                return index;
            }
            index++;
            current = current.next;
        }
        return -1;
    };

    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;
    }

    this.print = function(){

    };
}

var list = new LinkedList();
list.append(15);
list.append(10);
console.log(list.toString());

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

    var length = 0;
    var head = null;
    var tail = null;

    this.insert = function(position, element){
        if(position >=0 && position <= length){
            var node = new Node(element),
                current = head,
                previous,
                index = 0;

            if (position === 0){
                if (!head){
                    head = node;
                    tail = node;
                } else {
                    node.next = current;
                    current.prev = node;
                    head = node;
                }
            } else if (position === length){
                current = tail;
                current.next = node;
                node.prev = current;
                tail = node;
            } else {
                while (index++ < position){
                    previous = current;
                    current = current.next;
                }
                node.next = current;
                previous.next = node;

                current.prev = node;
                node.prev = previous;
            }
            length++;
            return true;
        } else {
            return false;
        }
    }

    this.removeAt = function(position){
        if (position > -1 && position < length){
            var current = head,
                previous,
                index = 0;

            if (position === 0){
                head = current.next;
                if (length === 1){
                    tail = null;
                } else {
                    head.prev = null;
                }
            } else if(position == length-1){
                current = tail;
                tail = current.prev;
                tail.next = null;
            } else {
                while (index++ < position){
                    previous = current;
                    current = current.next;
                }
                previous.next = current.next;
                current.next.prev = previous;
            }
            length--;
            return current.element;
        } else {
            return null;
        }
    }
}

技术分享

以上是关于javascript普通链表及双向链表的主要内容,如果未能解决你的问题,请参考以下文章

链表及双向链表

链表及双向链表

数据结构与算法-->>双向链表环形链表及约瑟夫问题

单向链表和双向链表的原理及其相关实现

javascript中的链表结构—双向链表

JavaScript 实现双向链表的操作