JavaScript数据结构与算法(七) 双向链表的实现
2017-06-09 15:05
405 查看
TypeScript方式实现源码
View Code
// 双向链表和普通链表的区别在于, 在链表中, // 一个节点只有链向下一个节点的链接,而在双向链表中,链接是双向的:一个链向下一个元素, // 另一个链向前一个元素,如下图所示: // [code] 1 var Node = (function () { 2 function Node(element) { 3 this.element = element; 4 this.next = null; 5 this.prev = null; 6 } 7 return Node; 8 }()); 9 /** 10 * 双向链表 11 * @name DoublyLinkedList 12 */ 13 var DoublyLinkedList = (function () { 14 function DoublyLinkedList() { 15 this.length = 0; 16 this.head = null; 17 this.tail = null; // 新增的 18 } 19 /** 20 * 向列表尾部添加一个新的项 21 * @param element 22 */ 23 DoublyLinkedList.prototype.append = function (element) { 24 var node = new Node(element), current; 25 if (this.head === null) { 26 this.head = node; 27 } 28 else { 29 current = this.head; 30 // 循环列表,知道找到最后一项 31 while (current.next) { 32 current = current.next; 33 } 34 // 找到最后一项,将其next赋为node,建立链接 35 current.next = node; 36 } 37 this.length++; // 更新列表长度 38 }; 39 /** 40 * 向列表的特定位置插入一个新的项 41 * @param position 42 * @param element 43 */ 44 DoublyLinkedList.prototype.insert = function (position, element) { 45 // 检查越界值 46 if (position >= 0 && position <= this.length) { 47 var node = new Node(element), current = this.head, previous = void 0, index = 0; 48 if (position === 0) { 49 if (!this.head) { 50 this.head = node; 51 this.tail = node; 52 } 53 else { 54 node.next = current; 55 current.prev = node; // 新增的 56 this.head = node; 57 } 58 } 59 else if (position === this.length) { 60 current = this.tail; 61 current.next = node; 62 node.prev = current; 63 this.tail = node; 64 } 65 else { 66 while (index++ < position) { 67 previous = current; 68 current = current.next; 69 } 70 node.next = current; 71 previous.next = node; 72 current.prev = node; // 新增的 73 node.prev = previous; // 新增的 74 } 75 this.length++; // 更新列表长度 76 return true; 77 } 78 else { 79 return false; 80 } 81 }; 82 /** 83 * 从列表的特定位置移除一项 84 * @param position 85 */ 86 DoublyLinkedList.prototype.removeAt = function (position) { 87 // 检查越界值 88 if (position > -1 && position < this.length) { 89 var current = this.head, previous = void 0, index = 0; 90 // 移除第一项 91 if (position === 0) { 92 this.head = current.next; 93 // 如果只有一项,更新tail // 新增的 94 if (this.length === 1) { 95 this.tail = null; 96 } 97 else { 98 this.head.prev = null; 99 } 100 } 101 else if (position === this.length - 1) { 102 current = this.tail; 103 this.tail = current.prev; 104 this.tail.next = null; 105 } 106 else { 107 while (index++ < position) { 108 previous = current; 109 current = current.next; 110 } 111 // 将previous与current的下一项链接起来:跳过current,从而移除它 112 previous.next = current.next; 113 current.next.prev = previous; // 新增的 114 } 115 this.length--; 116 return current.element; 117 } 118 else { 119 return null; 120 } 121 }; 122 /** 123 * 从列表中移除一项 124 * @param element 125 */ 126 DoublyLinkedList.prototype.remove = function (element) { 127 var index = this.indexOf(element); 128 return this.removeAt(index); 129 }; 130 /** 131 * :返回元素在列表中的索引。如果列表中没有该元素则返回-1 132 * @param element 133 */ 134 DoublyLinkedList.prototype.indexOf = function (element) { 135 var current = this.head, index = -1; 136 while (current) { 137 if (element === current.element) { 138 return index; 139 } 140 index++; 141 current = current.next; 142 } 143 return -1; 144 }; 145 /** 146 * 如果链表中不包含任何元素, 返回true, 如果链表长度大于0则返回false 147 */ 148 DoublyLinkedList.prototype.isEmpty = function () { 149 return this.length === 0; 150 }; 151 /** 152 * 返回链表包含的元素个数。与数组的length属性类似 153 */ 154 DoublyLinkedList.prototype.size = function () { 155 return this.length; 156 }; 157 /** 158 * 由于列表项使用了Node类,就需要重写继承自JavaScript对象默认的toString方法,让其只输出元素的值 159 */ 160 DoublyLinkedList.prototype.toString = function () { 161 var current = this.head, string = ''; 162 while (current) { 163 string += current.element; 164 current = current.next; 165 } 166 return string; 167 }; 168 DoublyLinkedList.prototype.getHead = function () { 169 return this.head; 170 }; 171 DoublyLinkedList.prototype.print = function () { 172 console.log(this.toString()); 173 }; 174 return DoublyLinkedList; 175 }());
View Code
相关文章推荐
- javascript实现数据结构与算法系列:循环链表与双向链表
- javascript实现数据结构与算法系列:队列 -- 链队列和循环队列实现及示例
- javascript实现数据结构与算法系列
- 数据结构与算法(JavaScript实现)
- javascript实现数据结构与算法系列:功能完整的线性链表
- 二叉树的实现 -- 数据结构与算法的javascript描述 第十章
- 集合的实现 -- 数据结构与算法的javascript描述 第九章
- JavaScript数据结构与算法(三) 优先级队列的实现
- 队列的实现 -- 数据结构与算法的javascript描述 第五章
- JavaScript——双向链表实现
- JavaScript数据结构与算法(一) 栈的实现
- 链表的实现 -- 数据结构与算法的javascript描述 第六章
- JavaScript数据结构与算法(二) 队列的实现
- javascript实现数据结构与算法系列:栈 -- 顺序存储表示和链式表示及示例
- javascript实现数据结构与算法系列:线性表的静态单链表存储结构
- JavaScript数据结构与算法(四) 循环队列的实现
- JavaScript数据结构与算法(六) 链表的实现
- 栈的实现 -- 数据结构与算法的javascript描述 第四章
- 用 Javascript 实现的“Dual listbox”(双向选择器)
- 数据结构与算法(C#实现)系列---广义树(二)