【數據結構】JavaScript實現單鏈表、單鏈表反轉

鏈表

鏈接也是一種存儲數據的工具,不同於數組,鏈表中的元素並不是連續存儲的。因此不能通過下標去訪問。 鏈表分爲單(向)鏈表,雙向鏈表,循環鏈表等。.今天來實現一下單鏈表。
單鏈表中的每個元素包括兩個兩個域,一個是保存元素本身的域,另一個是指向一下一個節點的指針域


    function LinkedList(){
        var Node = function( ele ){
            this.ele = ele;     //ele  屬性代表插入的值
            this.next = null;   //  next屬性 代表 指向下一個節點的指針
        }
        var head = null;
        var length = 0;


        // 向鏈表末尾插入一個元素
        this.append = function(ele){
            var node = new Node(ele);
            var currentNode = null;

            if( head === null){    // 列表爲空,插入的節點是第一個頭結點
                head = node;
            }
            else{
                currentNode = head;
                // 循環鏈表,直到最後一個節點
                while( currentNode.next !== null ){
                    currentNode = currentNode.next;
                }
                // 出了while循環,說明找到了最後一個節點
                currentNode.next = node;

            }
            length++;
        }
        // 刪除鏈表中指定的某個節點:刪除頭結點 or  除了頭結點以外的節點
        this.removeAt = function( position ){
            var currentNode = head;
            var previousNode = null;
            var index = 0;

            if( position >= 0 && position < length ){
                if( position === 0 ){
                    node = currentNode.next;
                }
                else{
                    while( index++ < position ){
                        previousNode = currentNode;
                        currentNode = currentNode.next;
                    }
                    // 出了while循環, index === position
                    previousNode.next = currentNode.next;
                }
                length--;
                return currentNode.ele
            }
            // 要刪除的節點的不存在
            else{

                return -1;
            }
        }
        // 在任意位置插入一個元素
        this.insert = function(){
            // 檢查是否越界
            if( position >= 0 && position <= length ){
                var newNode = new Node(ele);
                var currentNode = head;   // 保存一下頭結點
                var previousNode = null;
                var index = 0;

                if( position === 0 ){
                   newNode.next = currentNode;
                   head = newNode;
                }
                else{
                    while ( index++ < position ){
                        previousNode = currentNode;
                        currentNode = currentNode.next;
                    }
                    // 出了循環表示找到位置
                    newNode.next = currentNode;
                    previousNode.next = currentNode;
                }
                length++;
                return 1;
            }
            else{
                return -1;
            }
        }

        //查找鏈表中的某個元素所在位置, 無此元素返回 -1
        this.find = function(ele){
             var currentNode = head;
             var index = 0;
             while( currentNode ){
                if( currentNode.ele = ele ){
                    return index;
                }
                currentNode = currentNode.next;
                index++; 
            }  
            return -1;
        }       

        this.isEmpty = function(){
            return length === 0;
        }

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

    }

下面來實現鏈表的反轉

function reverse( linkedList ){
    var head = linkedList.head;

    // 如果只有一個節點 或者 是空鏈表
    if( head === null || head.next === null ){
        return;
    }
    var p = head;
    var q = p.next;
    // 反轉後的頭結點變成尾節點
    head.next = null;
    while(q){
        r = q.next;
        q.next = p;
        p = q;
        q = r;
    }
    // 退出循環後 r = q.next = null, q.next = q; p=q; q=null;
    // p指向原來節點的尾節點, 那麼翻轉後,尾節點變成頭結點
    linkedList.head = p;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章