JS實現雙向鏈表
function DoublyLinkedList() { var Node = function (element) { this.element = element; this.prev = null; this.next = null; }; var length = 0, head = null; //向尾部追加 this.append = function (element) { var node = new Node(element), current, previous; if ( ! head) { head = node; }else { current = head; while (current) { previous = current; current = current.next; } previous.next = node node.prev = previous } length ++ ; return true; } //指定位置插入 this.insert = function (position, element) { if (position > -1 && position <= length) { var node = new Node(element), current = head, previous, index = 0; if (position === 0) { if ( ! head) { head = node; }else { node.next = head; head.prev = node; head = node; } }else { while (index ++ < position) { previous = current; current = current.next; } if (position != length) { node.next = current; current.prev = node; } previous.next = node; node.prev = previous; } length ++ ; return true; }else { return false; } }; //刪除指定位置元素 this.removeAt = function (position) { if (position > -1 && position < length) { var current = head, index = 0, previous; if (position === 0) { head = head.next; head.prev = null; }else { while (index ++ < position) { previous = current; current = current.next; } if(position === length - 1){ previous.next =null; }else{ previous.next = current.next; current.next.prev = previous; } }; length--; return current.element; }else { return false; } }; //刪除值為element的所有元素 this.removeEle = function (element) { var current = head, previous, num=0; if (current.element === element) { head = current.next; } previous = current; current = current.next; while (current) { if (current.element == element) { previous.next = current.next; if(current.next){ current.next.prev = previous; } current=current.next; length--; num++; }else{ previous = current; current = current.next; } } return num; }; //刪除尾部 this.remove = function () { if (length === 0) { return false; }; var current = head, previous; if (length === 1) { head = null; length--; return current.element; } while (current.next) { previous = current; current = current.next; } previous.next = null; length--; return current.element; }; //當前元素的其實位置 this.indexOf = function (element) { var current = head, index = 0; while (current && index < length) { if (current.element === element) { return index; }; current = current.next; index++; } return false; }; //是否為空 this.isEmpty = function () { return length === 0; }; //鏈表長度 this.size = function () { return length; }; //轉成字符串 this.toString = function () { var current = head, string = ''; while (current) { string += current.element; current = current.next; } return string; }; //獲取頭結點元素 this.getHead = function () { return head.element; }; //獲取未結點元素 this.getTail = function () { var previous,current=head; while(current){ previous=current; current=current.next; } return previous.element; }; } let myLink = new DoublyLinkedList(); myLink.append('A') myLink.append('B') myLink.append('C') myLink.append('E') myLink.insert(3, 'D') myLink.insert(5, 'F') myLink.insert(0, 'G') console.log(myLink.toString()) //GABCDEF myLink.removeAt(0) //刪除G myLink.removeAt(5) //刪除F console.log(myLink.remove()) //刪除E console.log(myLink.toString()) // ABCD myLink.append('D') //向尾部增加D console.log(myLink.toString()) //ABCDD console.log(myLink.removeEle('D')) //刪除所有D,打印刪除D的個數 2 console.log(myLink.toString()) //ABC console.log(myLink.indexOf('B')) //打印B的位置 1 console.log(myLink.size()) //打印鏈表的長度 3 console.log(myLink.getHead()) //A console.log(myLink.getTail()) //C
雙向循環鏈表:將雙向鏈表的頭尾指針相連,就構成了雙向循環鏈表。這種鏈表從任意一個節點都可以同時向兩個方向進行節點遍歷。