JavaScript——雙向鏈表實現

本文版權歸博客園和做者吳雙本人共同全部,轉載和爬蟲請註明原文連接  http://www.cnblogs.com/tdws/html

下午分享了JavaScript實現單向鏈表,晚上就來補充下雙向鏈表吧。對鏈表的實現不是很瞭解的能夠移步:http://www.cnblogs.com/tdws/p/6033209.htmlnode

雙向鏈表與鏈表的不一樣之處主要在於他的雙向查找。由於在這種結構中咱們設計了每一個節點的prev(向上查找)的引用或指針和next(向下查找)的引用或指針。得益於這種結構你能作到正向和反向的查找。你也能夠在查找某個index位置的元素時,根據其長度計算,究竟是使用正向仍是反向,這取決於你本身。post

直接上代碼吧,詳解在註釋裏:大數據

先看一下代碼的總體結構:this

下面是具體實現:spa

    function DoublyLinkedList() {
        var Node = function (element) {
            this.element = element;
            this.next = null;               //下一個是誰
            this.prev = null;               //上一個是誰
        };
        var head = null;
        var length = 0;
        var tail = 0;
        this.insert = function (position, element) {
            if (position >= 0 && position <= length) {
                var node = new Node(element);
                var current = head;
                var index = 0;
                var previous;
                if (position == 0) {                            
                    if (head == null) {                             //空鏈表
                        head = node;
                        tail = node;
                    } else {
                        head = node;                                  //新元素做爲頭部
                        head.next = current;                          //頭部的下一個節點是舊頭部
                        current.prev = node;                           //舊頭部的上一個節點是新元素
                    }
                } else if (position == length) {                        //尾部
                    current = tail;
                    current.next = node;                                 //舊尾部的下一個節點 是新節點
                    node.prev = current;                                 //新節點的上一個節點是舊尾部
                    tail = node;                                         //更新尾部節點爲新元素
                } else {
                    while (index < position) {
                        previous = current;
                        current = current.next;
                        index++;
                    }                                                   //遍歷後current爲當前position的節點
                    node.next = current;                                //新節點的next是current             
                    previous.next = node;                                //上節點的下一個是新元素

                    node.prev = previous;                               //新元素的上個節點是previous
                    current.previous = node;                            //current的上個節點是新元素
                }
                length++;
                return true;
            } else {
                return false;
            }
        };

        this.removeAt = function (position) {
            if (position > -1 && position < length) {
                var current = head;
                var index = 0;
                var previous;
                if (position == 0) {
                    head = current.next;                    //給head賦值爲 下個節點,不關心其是否爲null
                    if (length == 1) {                        //若是長度爲1  head已經爲null,則將tail置爲null
                        tail = null;
                    } else {                                    //head已經賦值爲下個節點
                        head.prev = null;                       //head的prev置爲null
                    }
                } else if (position == length - 1) {            //最後一個元素
                    current = tail;
                    tail = current.prev;
                    tail.next = null;
                } else {
                    while (index++ < position) {                    //普通中間元素
                        previous = current.prev;                    
                        current = current.next;
                    }                                               //遍歷後獲得當前position元素
                    previous.next = current.next;                    //當前osition元素的prev指向當前postion元素的下個元素
                    current.next.prev = previous;                       //總之越過一個
                }
                length--;
                return current.element;
            } else {
                return null;
            }
        };

        this.getLength = function () {
            return length;
        };

        this.toString = function () {
            var current = head;
            var string = '';
            while (current) {
                string += ',' + current.element;
                current = current.next;
            }
            return string;
        };
    }

廢話也很少說了,關於雙向鏈表的文章網上一搜一大堆。設計

順便提到的就是Redis五大數據類型中的List列表類型,咱們知道Redis列表咱們能夠正向查找元素,也能夠反向查找元素。這也算是雙向鏈表在實際中的一大用途吧。指針

Redis相關文章連接 http://www.cnblogs.com/tdws/tag/NoSql/code

 

若是個人點滴分享對你能有點滴幫助,歡迎點擊下方紅色按鈕關注,我將持續輸出分享。htm

相關文章
相關標籤/搜索