84 lines
2.5 KiB
JavaScript
84 lines
2.5 KiB
JavaScript
(function () {
|
|
'use strict';
|
|
|
|
/**************************************************
|
|
* Linked list node class
|
|
*
|
|
* Internal private class to represent a node within
|
|
* a linked list. Each node has a 'data' property and
|
|
* a pointer the previous node and the next node in the list.
|
|
*
|
|
* Since the 'Node' function is not assigned to
|
|
* module.exports it is not visible outside of this
|
|
* file, therefore, it is private to the LinkedList
|
|
* class.
|
|
*
|
|
***************************************************/
|
|
|
|
/**
|
|
* Creates a node object with a data property and pointer
|
|
* to the next node
|
|
*
|
|
* @constructor
|
|
* @param {object|number|string} data The data to initialize with the node
|
|
*/
|
|
function Node(data) {
|
|
this.data = data || null;
|
|
this.next = null;
|
|
this.prev = null;
|
|
}
|
|
|
|
/* Functions attached to the Node prototype. All node instances will
|
|
* share these methods, meaning there will NOT be copies made for each
|
|
* instance. This will be a huge memory savings since there will likely
|
|
* be a large number of individual nodes.
|
|
*/
|
|
Node.prototype = {
|
|
|
|
/**
|
|
* Returns whether or not the node has a pointer to the next node
|
|
*
|
|
* @returns {boolean} true if there is a next node; false otherwise
|
|
*/
|
|
hasNext: function() {
|
|
return (this.next !== null);
|
|
},
|
|
|
|
/**
|
|
* Returns whether or not the node has a pointer to the previous node
|
|
*
|
|
* @returns {boolean} true if there is a previous node; false otherwise
|
|
*/
|
|
hasPrev: function() {
|
|
return (this.prev !== null);
|
|
},
|
|
|
|
/**
|
|
* Returns the data of the the node
|
|
*
|
|
* @returns {object|string|number} the data of the node
|
|
*/
|
|
getData: function() {
|
|
return this.data;
|
|
},
|
|
|
|
/**
|
|
* Returns a string represenation of the node. If the data is an
|
|
* object, it returns the JSON.stringify version of the object.
|
|
* Otherwise, it simply returns the data
|
|
*
|
|
* @return {string} the string represenation of the node data
|
|
*/
|
|
toString: function() {
|
|
if (typeof this.data === 'object') {
|
|
return JSON.stringify(this.data);
|
|
} else {
|
|
return String(this.data);
|
|
}
|
|
}
|
|
};
|
|
|
|
module.exports = Node;
|
|
|
|
}());
|