/* * 單向鏈表 * Node 類用來表示節點 * LinkedList 類提供了插入節點、刪除節點、顯示列表元素的方法,以及其他一些輔助方法。 */ function Node(element) { this.element = element; this.next = null; }; fun... ...
/* * 單向鏈表 * Node 類用來表示節點 * LinkedList 類提供了插入節點、刪除節點、顯示列表元素的方法,以及其他一些輔助方法。 */ function Node(element) { this.element = element; this.next = null; }; function LList() { this.head = new Node("head"); }; LList.prototype = { constructor: LList, find: function(item) { var currNode = this.head; while (currNode && currNode.element != item) { currNode = currNode.next; } return currNode; }, insert: function(newElement, item) { var newNode = new Node(newElement); var current = this.find(item); newNode.next = current.next; current.next = newNode; }, display: function() { var currNode = this.head; while (!(currNode.next == null)) { console.log(currNode.next.element); currNode = currNode.next; } }, findPrevious: function(item) { var currNode = this.head; while (!(currNode.next == null) && (currNode.next.element != item)) { currNode = currNode.next; } return currNode; }, remove: function(item) { var prevNode = this.findPrevious(item); if (!(prevNode.next == null)) { prevNode.next = prevNode.next.next; } } }; //這個鏈表還需要優化,當前面element與後面insert定位的element相同時存在問題
//下麵例子給予說明 var myName = new LList(); myName.insert('張', 'head'); myName.insert('檸', '張'); myName.insert('檬', '檸'); myName.display(); //張,檸,檬 var myName = new LList(); myName.insert('張', 'head'); myName.insert('檸', '張'); myName.insert('檬', '檸'); myName.insert('的', '檬'); myName.insert('檸', '的'); myName.insert('檬', '檸'); myName.display(); //張,檸,檬,檬, 的, 檸, 並未按照順序來 /* * 雙向鏈表 * Node 類用來表示節點 * LinkedList 類提供了插入節點、刪除節點、顯示列表元素的方法,以及其他一些輔助方法。 */ function Node(element) { this.element = element; this.next = null; this.previous = null; }; LList.prototype = { constructor: Node, find: function(item) { var currNode = this.head; while (currNode.element != item) { currNode = currNode.next; } return currNode; }, insert: function(newElement, item) { var newNode = new Node(newElement); var current = this.find(item); newNode.next = current.next; newNode.previous = current; current.next = newNode; }, remove: function(item) { var currNode = this.find(item); if (!(currNode.next == null)) { currNode.previous.next = currNode.next; currNode.next.previous = currNode.previous; currNode.next = null; currNode.previous = null; } }, findLast: function() { var currNode = this.head; while (!(currNode.next == null)) { currNode = currNode.next; } return currNode; }, dispReverse: function() { var currNode = this.head; currNode = this.findLast(); while (!(currNode.previous == null)) { console.log(currNode.element); currNode = currNode.previous; } }, display: function() { var currNode = this.head; while (!(currNode.next == null)) { console.log(currNode.next.element); currNode = currNode.next; } } }; //雙向鏈表也存在上述問題 //迴圈列表 function Node(element) { this.element = element; this.next = null; }; function LList() { this.head = new Node("head"); this.head.next = this.head; }; LList.prototype = { constructor: LList, find: function(item) { var currNode = this.head; while (currNode && currNode.element != item && !(currNode.next.element == "head")) { currNode = currNode.next; } return currNode.element == item ? currNode : null; }, insert: function(newElement, item) { var newNode = new Node(newElement); var current = this.find(item); newNode.next = current.next; current.next = newNode; }, display: function() { var currNode = this.head; while (!(currNode.next == null) && !(currNode.next.element == "head")) { console.log(currNode.next.element); currNode = currNode.next; } }, findPrevious: function(item) { var currNode = this.head; while (!(currNode.next == null) && !(currNode.next.element == "head") && (currNode.next.element != item)) { currNode = currNode.next; } return currNode; }, remove: function (item) { var prevNode = this.findPrevious(item); if (!(prevNode.next == null)) { prevNode.next = prevNode.next.next; } } };