|
| 1 | +/** |
| 2 | + * Linked list. |
| 3 | + * |
| 4 | + * @example |
| 5 | + * |
| 6 | + * var LL = require('../src/data-structures/linked-list'); |
| 7 | + * |
| 8 | + * var linkedList = new LL.LinkedList(); |
| 9 | + * |
| 10 | + * linkedList.push({ |
| 11 | + * name: 'John', |
| 12 | + * birthyear: 1981 |
| 13 | + * }); |
| 14 | + * linkedList.push({ |
| 15 | + * name: 'Pavlo', |
| 16 | + * birthyear: 2000 |
| 17 | + * }); |
| 18 | + * linkedList.push({ |
| 19 | + * name: 'Garry', |
| 20 | + * birthyear: 1989 |
| 21 | + * }); |
| 22 | + * linkedList.push({ |
| 23 | + * name: 'Derek', |
| 24 | + * birthyear: 1990 |
| 25 | + * }); |
| 26 | + * linkedList.push({ |
| 27 | + * name: 'Ivan', |
| 28 | + * birthyear: 1966 |
| 29 | + * }); |
| 30 | + * |
| 31 | + * console.log(linkedList.shift().data); // { name: 'John', birthyear: 1981 } |
| 32 | + * console.log(linkedList.pop().data); // { name: 'Ivan', birthyear: 1966 } |
| 33 | + * |
| 34 | + * @module data-structures/linked-list |
| 35 | + */ |
1 | 36 | (function (exports) { |
| 37 | + |
2 | 38 | 'use strict'; |
3 | 39 |
|
4 | | - function Node(data) { |
| 40 | + /** |
| 41 | + * Linked list node. |
| 42 | + * |
| 43 | + * @public |
| 44 | + * @constructor |
| 45 | + * @param {Object} data Data of the node. |
| 46 | + */ |
| 47 | + exports.Node = function (data) { |
| 48 | + /** |
| 49 | + * Data of the node. |
| 50 | + * @member {Object} |
| 51 | + */ |
5 | 52 | this.data = data; |
| 53 | + /** |
| 54 | + * Next node. |
| 55 | + * @member {Node} |
| 56 | + */ |
6 | 57 | this.next = null; |
| 58 | + /** |
| 59 | + * Previous node. |
| 60 | + * @member {Node} |
| 61 | + */ |
7 | 62 | this.prev = null; |
8 | 63 | } |
9 | 64 |
|
10 | | - function LinkedList() { |
| 65 | + /** |
| 66 | + * Linked list. |
| 67 | + * |
| 68 | + * @public |
| 69 | + * @constructor |
| 70 | + */ |
| 71 | + exports.LinkedList = function () { |
11 | 72 | this.first = null; |
12 | 73 | this.last = null; |
13 | 74 | } |
14 | 75 |
|
15 | | - LinkedList.prototype.push = function (data) { |
16 | | - var node = new Node(data); |
| 76 | + /** |
| 77 | + * Add data to the end of linked list. |
| 78 | + * |
| 79 | + * @public |
| 80 | + * @method |
| 81 | + * @param {Object} data Data which should be added. |
| 82 | + */ |
| 83 | + exports.LinkedList.prototype.push = function (data) { |
| 84 | + var node = new exports.Node(data); |
17 | 85 | if (this.first === null) { |
18 | 86 | this.first = this.last = node; |
19 | 87 | } else { |
|
24 | 92 | } |
25 | 93 | }; |
26 | 94 |
|
27 | | - LinkedList.prototype.unshift = function (data) { |
28 | | - var node = new Node(data); |
| 95 | + /** |
| 96 | + * Add data to the beginning of linked list. |
| 97 | + * |
| 98 | + * @public |
| 99 | + * @method |
| 100 | + * @param {Object} data Data which should be added. |
| 101 | + */ |
| 102 | + exports.LinkedList.prototype.unshift = function (data) { |
| 103 | + var node = new exports.Node(data); |
29 | 104 | if (this.first === null) { |
30 | 105 | this.first = this.last = node; |
31 | 106 | } else { |
|
36 | 111 | } |
37 | 112 | }; |
38 | 113 |
|
39 | | - LinkedList.prototype.inorder = function (cb) { |
| 114 | + /** |
| 115 | + * In order traversal of the linked list. |
| 116 | + * |
| 117 | + * @public |
| 118 | + * @method |
| 119 | + * @param {Function} cb Callback which should be executed on each node. |
| 120 | + */ |
| 121 | + exports.LinkedList.prototype.inorder = function (cb) { |
40 | 122 | var temp = this.first; |
41 | 123 | while (temp) { |
42 | 124 | cb(temp); |
43 | 125 | temp = temp.next; |
44 | 126 | } |
45 | 127 | }; |
46 | 128 |
|
47 | | - LinkedList.prototype.remove = function (data) { |
| 129 | + /** |
| 130 | + * Remove data from the linked list. |
| 131 | + * |
| 132 | + * @public |
| 133 | + * @method |
| 134 | + * @param {Object} data Data which should be removed. |
| 135 | + * @return {Boolean} Returns true if data has been removed. |
| 136 | + */ |
| 137 | + exports.LinkedList.prototype.remove = function (data) { |
48 | 138 | if (this.first === null) { |
49 | 139 | return false; |
50 | 140 | } |
|
73 | 163 | return false; |
74 | 164 | }; |
75 | 165 |
|
76 | | - LinkedList.prototype.hasCycle = function () { |
| 166 | + /** |
| 167 | + * Check or linked list contains cycle. |
| 168 | + * |
| 169 | + * @public |
| 170 | + * @method |
| 171 | + * @return {Boolean} Returns true if linked list contains cycle. |
| 172 | + */ |
| 173 | + exports.LinkedList.prototype.hasCycle = function () { |
77 | 174 | var fast = this.first, |
78 | 175 | slow = this.first; |
79 | 176 | while (true) { |
|
92 | 189 | } |
93 | 190 | }; |
94 | 191 |
|
95 | | - LinkedList.prototype.pop = function () { |
| 192 | + /** |
| 193 | + * Return last node from the linked list. |
| 194 | + * |
| 195 | + * @public |
| 196 | + * @method |
| 197 | + * @return {Node} Last node. |
| 198 | + */ |
| 199 | + exports.LinkedList.prototype.pop = function () { |
96 | 200 | if (this.last === null) { |
97 | 201 | return null; |
98 | 202 | } |
|
101 | 205 | return temp; |
102 | 206 | }; |
103 | 207 |
|
104 | | - LinkedList.prototype.shift = function () { |
| 208 | + /** |
| 209 | + * Return first node from the linked list. |
| 210 | + * |
| 211 | + * @public |
| 212 | + * @method |
| 213 | + * @return {Node} First node. |
| 214 | + */ |
| 215 | + exports.LinkedList.prototype.shift = function () { |
105 | 216 | if (this.first === null) { |
106 | 217 | return null; |
107 | 218 | } |
|
110 | 221 | return temp; |
111 | 222 | }; |
112 | 223 |
|
113 | | - LinkedList.prototype.recursiveReverse = function () { |
| 224 | + exports.LinkedList.prototype.recursiveReverse = function () { |
114 | 225 |
|
115 | 226 | function inverse(current, next) { |
116 | 227 | if (!next) { |
|
130 | 241 | this.last = temp; |
131 | 242 | }; |
132 | 243 |
|
133 | | - LinkedList.prototype.reverse = function () { |
| 244 | + exports.LinkedList.prototype.reverse = function () { |
134 | 245 | if (!this.first || !this.first.next) { |
135 | 246 | return; |
136 | 247 | } |
|
151 | 262 | this.last = temp; |
152 | 263 | }; |
153 | 264 |
|
154 | | - exports.LinkedList = LinkedList; |
155 | | - exports.Node = Node; |
156 | | - |
157 | 265 | }(typeof exports === 'undefined' ? window : exports)); |
158 | 266 |
|
0 commit comments