码迷,mamicode.com
首页 > 编程语言 > 详细

javascript普通链表及双向链表

时间:2016-08-28 20:53:29      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

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

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

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

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普通链表及双向链表

标签:

原文地址:http://www.cnblogs.com/aguncn/p/5815731.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!