js資料結構--連結串列(likedList)

遨翔在知識的海洋裡發表於2019-01-09

連結串列

  1. append(element): 向列表尾部新增一個新的項
  2. insert(position,element):向列表的特定位置插入一個新的項
  3. remove(element): 從列表中移除一項
  4. indexOf(element): 返回元素在列表中的索引.如果列表中沒有該元素則返回-1.
  5. removeAt(position):從列表的特定位置移除一項
  6. isEmpty(): 如果連結串列中不包含任何元素,返回true,如果連結串列長度大於0則返回false
  7. size(): 返回連結串列包含的元素個數。與陣列的length屬性類似
  8. toString(): 由於列表項使用了Node類,就需要重寫繼承自JavaScript物件預設的toString()方法,讓其只輸出元素的值

連結串列實現append,getHead

likedList.js

var LikedList = function() {
    //連結串列頭
    var head = null;
    // 連結串列長度
    var length = 0;
    // 節點
    var Node = function(el) {
            this.el = el;
            this.next = null;
        }
        //連結串列尾新增元素
    this.append = function(el) {
            var node = new Node(el) // 1     2
            if (head == null) {
                head = node; //head =1
            } else {
                var current = head; //current=1
                while (current.next) { //null
                    current = current.next;
                }
                //while迴圈執行完之後,current已經是連結串列的最後一項了
                current.next = node //current.next = node = 2
            }
            length++
        }
    this.getHead = function() {
        return head;
    }
}
複製程式碼

例項化

js資料結構--連結串列(likedList)

連結串列插入 insert

likedList.js

var LikedList = function() {
    //連結串列頭
    var head = null;
    // 連結串列長度
    var length = 0;
    // 節點
    var Node = function(el) {
            this.el = el;
            this.next = null;
        }
        //連結串列尾新增元素
    this.append = function(el) {
            var node = new Node(el) // 1     2
            if (head == null) {
                head = node; //head =1
            } else {
                var current = head; //current=1
                while (current.next) { //null
                    current = current.next;
                }
                //while迴圈執行完之後,current已經是連結串列的最後一項了
                current.next = node //current.next = node = 2
            }
            length++
        }
        //連結串列某一個位置新增元素
    this.insert = function(position, el) {
        //越界
        if (position > -1 && position < length) {
            var node = new Node(el);
            if (position == 0) {
                var current = head;
                head = node;
                head.next = current;
            } else {
                var index = 0;
                var current = head;
                var previous = null;
                while (index < position) {
                    previous = current;
                    current = current.next;
                    index++
                }
                previous.next = node;
                node.next = current;
            }
            length++
        }

    }

    this.getHead = function() {
        return head;
    }
}
複製程式碼

例項化

js資料結構--連結串列(likedList)

remove removeAt

likedList.js

var LikedList = function() {
    //連結串列頭
    var head = null;
    // 連結串列長度
    var length = 0;
    // 節點
    var Node = function(el) {
            this.el = el;
            this.next = null;
        }
    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
        }
        return null
    }

    this.indexOf = function(el) {
        var current = head;
        var index = 0;
        while (current) {
            if (current.el == el) {
                return index
            }
            current = current.next
            index++
        }
        return -1
    }

    this.remove = function(el) {
        return this.removeAt(this.indexOf(el))
    }
}
複製程式碼

例項化

js資料結構--連結串列(likedList)

連結串列所有功能

likedList.js

var LikedList = function() {
    //連結串列頭
    var head = null;
    // 連結串列長度
    var length = 0;
    // 節點
    var Node = function(el) {
            this.el = el;
            this.next = null;
        }
        //連結串列尾新增元素
    this.append = function(el) {
            var node = new Node(el) // 1     2
            if (head == null) {
                head = node; //head =1
            } else {
                var current = head; //current=1
                while (current.next) { //null
                    current = current.next;
                }
                //while迴圈執行完之後,current已經是連結串列的最後一項了
                current.next = node //current.next = node = 2
            }
            length++
        }
    //連結串列某一個位置新增元素
    this.insert = function(position, el) {
        //越界
        if (position > -1 && position < length) {
            var node = new Node(el);
            if (position == 0) {
                var current = head;
                head = node;
                head.next = current;
            } else {
                var index = 0;
                var current = head;
                var previous = null;
                while (index < position) {
                    previous = current;
                    current = current.next;
                    index++
                }
                previous.next = node;
                node.next = current;
            }
            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
        }
        return null
    }
    //查詢連結串列元素下標
    this.indexOf = function(el) {
        var current = head;
        var index = 0;
        while (current) {
            if (current.el == el) {
                return index
            }
            current = current.next
            index++
        }
        return -1
    }
    //刪除指定元素
    this.remove = function(el) {
        return this.removeAt(this.indexOf(el))
    }
    // 是否為空
    this.isEmpty = function() {
        return length == 0;
    }
    //連結串列長度
    this.size = function() {
        return length
    }
    //連結串列頭
    this.getHead = function() {
        return head;
    }
}
複製程式碼

相關文章