JS實現單鏈表、單循環鏈表


鏈表

  鏈表是一種物理存儲單元上非線性、非連續性的數據結構(它在數據邏輯上是線性的),它的每個節點由兩個域組成:數據域和指針域。數據域中存儲實際數據,指針域則存儲着指針信息,指向鏈表中的下一個元素或者上一個元素。正是由於指針的存在,鏈表的存儲在物理單元是非連續性的。 

        鏈表的優點和缺點同樣明顯。和線性表相比,鏈表在添加和刪除節點上的效率更高,因為其只需要修改指針信息即可完成操作,而不像線性表(數組)那樣需要移動元素。同樣的,鏈表的長度在理論上也是無限的(在存儲器容量范圍內),並可以動態變化長度,相比線性表優勢很大。 相應的,由於線性表無法隨機訪問節點,只能通過指針順着鏈表進行遍歷查詢來訪問,故其訪問數據元素的效率比較低。  

 

 

JS實現單鏈表

function LinkedList() {
    let Node = function (ele) {
        this.ele = ele;
        this.next = null;
    }

    let length = 0,
        head = null; //頭指針

    //向尾部追加元素
    this.append = function (ele) {
        let node = new Node(ele),
            temp; //臨時指針

        if (!head) {
            head = node;
        } else {
            temp = head;
            while (temp.next) {
                temp = temp.next
            }
            temp.next = node;
        }
        length++;
        return true;
    }

    //插入到指定位置
    this.insert = function (position, ele) {
        if (position >= 0 && position < length) {
            let node = new Node(ele),
                temp = head,
                index = 0,
                previous;

            if (position == 0) {
                node.next = temp;
                head = node;
            } else {
                while (index++ < position) {
                    previous = temp;
                    temp = temp.next;
                }
                node.next = temp;
                previous.next = node;
            }
            length++;
            return true;
        } else {
            return false;
        }
    }

    //刪除指定位置元素
    this.removeAt = function (position) {
        if (position > -1 && position < length) {
            let temp = head,
                previous,
                index = 0;

            if (position == 0) {
                head = head.next;
            } else {
                while (index++ < position) {
                    previous = temp;
                    temp = temp.next;
                }

                previous.next = temp.next;
            }
            length--;
            return temp.ele;
        } else {
            return null;
        }
    }

    //刪除所有值為ele的元素
    this.removeEle = function (ele) {
        let temp = head,
            previous,
            num = 0;
        if (ele == temp.ele) {
            head = head.next;
            length--;
            num++;
        }

        while (temp.next) {
            previous = temp;
            temp = temp.next;
            if (temp.ele == ele) {
                previous.next = temp.next;
                length--;
                num++;
            }
        }
        return num;
    }

    //刪除最后一個元素
    this.pop = function () {
        let temp = head,
            previous = temp;
        if (length < 1) {
            return false;
        }
        if (length == 1) {
            head = null;
            length--;
            return temp.ele;
        }
        while (temp.next) {
            previous = temp;
            temp = temp.next;
        }
        previous.next = null;
        length--;
        return temp.ele;
    }

    this.indexOf = function (ele) {
        let temp = head,
            index = 0;

        while (temp) {
            if (temp.ele == ele) {
                return index;
            }
            temp = temp.next;
            index++;
        }
        return -1;

    }

    this.toString = function () {
        let temp = head,
            string = '';

        while (temp) {
            string += temp.ele + ' ';
            temp = temp.next;

        }
        return string;
    }
    this.length = function () {
        return length;
    }
    this.isEmpty = function () {
        return length === 0;
    };
    this.getHead = function () {
        return head.ele;
    }
}

let mylist = new LinkedList();
mylist.append('A');
mylist.append('B');
mylist.append('C');
mylist.append('D');
mylist.append('C');
mylist.append('B');
mylist.append('A');
console.log(mylist.toString());
console.log(mylist.pop());
console.log(mylist.toString());
console.log('移除%d個C', mylist.removeEle('C'));
console.log(mylist.toString());
console.log(mylist.length());
console.log(mylist.getHead());

console.log(mylist.indexOf('C'))

 JS實現單循環鏈表:

在單鏈表的基礎上,將尾節點的指針指向頭結點,就構成了一個循環鏈表。環形鏈表從任意一個節點開始,都可以遍歷整個鏈表。

function CircularLinkedList(){  
    var Node = function(element){  
        this.element = element;  
        this.next = null;  
    }  
  
    var length = 0,  
        head   = null;  
  
    this.append = function(element){  
        var node = new Node(element),  
            current;  
  
        if (!head) {  
            head = node;  
            node.next = head;  
        }else{  
            current = head;  
  
            while(current.next !== head){  
                current = current.next;  
            }  
  
            current.next = node;  
            node.next = head;  
        };  
  
        length++;  
        return true;  
    };  
  
    this.insert = function(position, element){  
        if(position > -1 && position < length){  
            var node = new Node(element),  
                index = 0,  
                current = head,  
                previous;  
  
  
            if (position === 0) {  
  
                node.next = head;  
                head = node;  
  
            }else{  
  
                while(index++ < position){  
                    previous = current;  
                    current = current.next;  
                }  
  
                previous.next = node;  
                node.next = current;  
  
            };  
  
            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 current = head,  
            previous,  
            indexCheck = 0;  
  
        while(current && indexCheck < length){  
            if(current.element === element){  
                if(indexCheck == 0){  
                    head = current.next;  
                    length--;  
                    return true;  
                }else{  
                    previous.next = current.next;  
                    length--;  
                    return true;  
                }  
            }else{  
                previous = current;  
                current = current.next;  
                indexCheck++;  
            }  
        }  
        return false;  
    };  
  //移除最后一個元素
    this.remove = function(){  
        if(length === 0){  
            return false;  
        }  
  
        var current = head,  
            previous,  
            indexCheck = 0;  
  
        if(length === 1){  
            head = null;  
            length--;  
            return current.element;  
        }  
  
        while(indexCheck++ < length){  
            previous = current;  
            current = current.next;  
        }  
        previous.next = head;  
        length--;  
        return previous.element;  //返回移除的元素
    };  
  
    this.indexOf = function(element){  
        var current = head,  
            index = 0;  
  
        while(current && index < length){  
            if(current.element === element){  
                return index;  
            }else{  
                index++;  
                current = current.next;  
            }  
        }  
        return false;  
    };  
  
  
    this.isEmpty = function(){  
        return length === 0;  
    };  
  
    this.size = function(){  
        return length;  
    };  
  
    this.toString = function(){  
        var current = head,  
            string = '',  
            indexCheck = 0;  
  
        while(current && indexCheck < length){  
            string += current.element;  
            current = current.next;  
            indexCheck++;  
        }  
  
        return string;  
    };     

}  

let mylist=new CircularLinkedList();
mylist.append('A');
mylist.append('B');
mylist.append('C');
mylist.append('D');

console.log(mylist.toString());
console.log(mylist.remove()); //D
console.log(mylist.toString());
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM