鏈表(JavaScript)

 

 

 

1.鏈表結構實現: 

(1)append:尾部添加:分兩種情況:一、鏈表爲空,head設爲node;二、鏈表不爲空,獲取到鏈表尾添加元素;

注意:鏈表添加後長度要手動增加

append代碼實現:

var LinkList = function() {
    var head = null;
    var length = 0;

    // 輔助類:用於創建鏈表項
    function Node(ele) {
        this.ele = ele;
        this.next = null;
    }
    /**
     * append 尾部添加節點
     * 如果爲空鏈表,則將創建的項賦值給head
     * 如果非空鏈表,則循環到最後,插入鏈表項
     * 添加元素時length++
    */
    this.append = function(ele) {
        var node = new Node(ele);
        if(head == null) {
            head= node;
        } else {
            var current = head;
            while(current.next) {
                current = current.next;
            }
            current.next = node;
        }
        length ++ ;
    }
    this.getHead = function() {
        return head;
    }
}    
var linkList = new LinkList();
linkList.append('小明');
linkList.append('小紅');
linkList.append('小黃');
console.log(linkList.getHead());

 

(2)insert:鏈表中插入元素:分兩種情況:

一、插入鏈表頭;

二、向其他位置插入元素;

代碼實現:

    // insert 鏈表中插入元素
    this.insert = function(position, ele) {
        var node = new Node(ele);
        if(position > -1 && position < length) {
            if(position == 0) {
                var current = head;
                head = node;
                node.next = current;
            } else {
                var previous = null;
                var current = head;
                var index = 0;
                while(index < position) {
                    previous = current;
                    current = current.next;
                    index ++ ;
                }
                previous.next = node;
                node.next = current;
            }
            length ++ ;
        }
    }

 

(3)removeAt:根據位置刪除鏈表中元素:分兩種情況:

一、刪除鏈表頭;

二、刪除其他位置元素;

注意:刪除後 length --,刪除的數據一般會用到,所以最後返回刪除的數據

代碼實現:

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

 

(4)indexOf:獲取某個元素的位置

代碼實現:

    // indexOf 獲取某項的位置
    this.indexOf = function(ele) {
        var current = head;
        var index = 0;
        while(current) {
            if(current.ele == ele) {
                return index;
            }
            current = current.next;
            index ++ ;
        }
        return -1;
    }

 

(5)remove :根據元素刪除

    this.remove = function(ele) {
        return this.removeAt(this.indexOf(ele));
    }

(6)isEmpty:判斷鏈表是否爲空

        size:鏈表長度

    // isEmpty 是否爲空
    this.isEmpty = function() {
        return length == 0;
    }
    // size 鏈表長度
    this.size = function() {
        return length;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章