Skip to content

Commit 4803038

Browse files
author
AndreyGeonya
committed
add linked-list jsdoc
1 parent d14f5fe commit 4803038

1 file changed

Lines changed: 124 additions & 16 deletions

File tree

‎src/data-structures/linked-list.js‎

Lines changed: 124 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,87 @@
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+
*/
136
(function (exports) {
37+
238
'use strict';
339

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+
*/
552
this.data = data;
53+
/**
54+
* Next node.
55+
* @member {Node}
56+
*/
657
this.next = null;
58+
/**
59+
* Previous node.
60+
* @member {Node}
61+
*/
762
this.prev = null;
863
}
964

10-
function LinkedList() {
65+
/**
66+
* Linked list.
67+
*
68+
* @public
69+
* @constructor
70+
*/
71+
exports.LinkedList = function () {
1172
this.first = null;
1273
this.last = null;
1374
}
1475

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);
1785
if (this.first === null) {
1886
this.first = this.last = node;
1987
} else {
@@ -24,8 +92,15 @@
2492
}
2593
};
2694

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);
29104
if (this.first === null) {
30105
this.first = this.last = node;
31106
} else {
@@ -36,15 +111,30 @@
36111
}
37112
};
38113

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) {
40122
var temp = this.first;
41123
while (temp) {
42124
cb(temp);
43125
temp = temp.next;
44126
}
45127
};
46128

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) {
48138
if (this.first === null) {
49139
return false;
50140
}
@@ -73,7 +163,14 @@
73163
return false;
74164
};
75165

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 () {
77174
var fast = this.first,
78175
slow = this.first;
79176
while (true) {
@@ -92,7 +189,14 @@
92189
}
93190
};
94191

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 () {
96200
if (this.last === null) {
97201
return null;
98202
}
@@ -101,7 +205,14 @@
101205
return temp;
102206
};
103207

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 () {
105216
if (this.first === null) {
106217
return null;
107218
}
@@ -110,7 +221,7 @@
110221
return temp;
111222
};
112223

113-
LinkedList.prototype.recursiveReverse = function () {
224+
exports.LinkedList.prototype.recursiveReverse = function () {
114225

115226
function inverse(current, next) {
116227
if (!next) {
@@ -130,7 +241,7 @@
130241
this.last = temp;
131242
};
132243

133-
LinkedList.prototype.reverse = function () {
244+
exports.LinkedList.prototype.reverse = function () {
134245
if (!this.first || !this.first.next) {
135246
return;
136247
}
@@ -151,8 +262,5 @@
151262
this.last = temp;
152263
};
153264

154-
exports.LinkedList = LinkedList;
155-
exports.Node = Node;
156-
157265
}(typeof exports === 'undefined' ? window : exports));
158266

0 commit comments

Comments
 (0)