@monocircuit/utils
Version:
A repository that contains data structures, algorithms and more.
267 lines • 8.62 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Node_1 = require("../Node/Node");
var Comparator_1 = require("../../others/Comparator/Comparator");
/**
* A linear data structure that is build in a fashion that mostly exposes a
* BigO notation of `O(n)`
*
* @spacecomplexity `O(n)`
*/
var LinkedList = /** @class */ (function () {
/**
* @param compareFunction The compare function that will be passed to a `Comparator`.
*/
function LinkedList(options) {
/**
* The first node of the `LinkedList`.
*/
this.head = null;
/**
* The last node of the `LinkedList`.
*/
this.tail = null;
/**
* The internal length counter that will be linked to by the `length` getter
* property of `LinkedList`.
*/
this.__length = 0;
this.__compare = new Comparator_1.default((options !== null && options !== void 0 ? options : {}).compareFunction);
if (options && options.array)
this.fromArray(options.array);
}
Object.defineProperty(LinkedList.prototype, "length", {
/**
* The length of the `LinkedList`. Behaves exactly like the `length`
* property of an array.
*/
get: function () {
return this.__length;
},
enumerable: false,
configurable: true
});
/**
* Checks if the `LinkedList` does not contain any `Node`s. In
* other words returns a boolean that indicates wether or not the
* `LinkedList` is empty.
*
* @returns A boolean that indicates wether or not the `LinkedList` is empty
*/
LinkedList.prototype.isEmpty = function () {
return !this.head;
};
/**
* Adds a node to the start of the `LinkedList`.
*
* @param data A piece of data that will be held by the `Node`
* @timecomplexity ``O(1)``
* @returns The `LinkedList` instance
*/
LinkedList.prototype.prepend = function (data) {
var newNode = new Node_1.default(data, this.head);
this.head = newNode;
if (!this.tail) {
this.tail = this.head;
}
this.__length += 1;
return this;
};
/**
* Adds a node to the end of the `LinkedList`.
*
* @param data A piece of data that will be held by the `Node`
* @timecomplexity ``O(1)``
* @returns The `LinkedList` instance
*/
LinkedList.prototype.append = function (data) {
var newNode = new Node_1.default(data);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
this.__length += 1;
return this;
}
;
this.tail.child = newNode;
this.tail = newNode;
this.__length += 1;
return this;
};
/**
* Removes all nodes that hold the same data passed as an argument.
*
* @param data A piece of data that the `Node` will be checked against
* @timecomplexity `O(n)`
* @returns The deleted `Node`
*/
LinkedList.prototype.delete = function (data) {
if (!this.head) {
return null;
}
var deletedNode = null;
while (this.head && this.__compare.equalTo(this.head.data, data)) {
deletedNode = this.head;
this.head = this.head.child;
this.__length -= 1;
}
var currentNode = this.head;
if (currentNode !== null) {
while (currentNode.child) {
if (this.__compare.equalTo(currentNode.child.data, data)) {
deletedNode = currentNode.child;
currentNode.child = currentNode.child.child;
this.__length -= 1;
}
else {
currentNode = currentNode.child;
}
}
}
if (this.__compare.equalTo(this.tail.data, data)) {
this.tail = currentNode;
this.__length -= 1;
}
return deletedNode;
};
/**
* Removes all nodes that hold the same data passed as an argument.
*
* @param data A piece of data that the `Node` will be checked against
* @param callback A function that can filter the results
* @timecomplexity `O(n)`
* @returns The seeked data, or if it could not find anything `null`
*/
LinkedList.prototype.find = function (_a) {
var data = _a.data, callback = _a.callback;
if (!this.head) {
return null;
}
var currentNode = this.head;
while (currentNode) {
if (callback && callback(currentNode.data)) {
return currentNode;
}
if (data !== undefined && this.__compare.equalTo(currentNode.data, data)) {
return currentNode.data;
}
currentNode = currentNode.child;
}
return null;
};
/**
* Deletes the last node on the `LinkedList`.
*
* @timecomplexity `O(n)`
* @returns The deleted `Node`
*/
LinkedList.prototype.deleteTail = function () {
var deletedTail = this.tail;
if (this.head === this.tail) {
this.head = null;
this.tail = null;
return deletedTail;
}
var currentNode = this.head;
if (currentNode) {
while (currentNode.child) {
if (!currentNode.child.child) {
currentNode.child = null;
}
else {
currentNode = currentNode.child;
}
}
this.tail = currentNode;
this.__length -= 1;
return deletedTail;
}
return null;
};
/**
* Deletes the first node on the `LinkedList`.
*
* @timecomplexity `O(1)`
* @returns The deleted `Node`
*/
LinkedList.prototype.deleteHead = function () {
if (!this.head) {
return null;
}
var deletedHead = this.head;
if (this.head.child) {
this.head = this.head.child;
}
else {
this.head = null;
this.tail = null;
}
this.__length -= 1;
return deletedHead;
};
/**
* Converts the an `Array()` to a `LinkedList`.
*
* @param data An array containing the correct type of data
* @timecomplexity ``O(n)``
* @returns The `LinkedList` instance
*/
LinkedList.prototype.fromArray = function (data) {
var _this = this;
data.forEach(function (d) { return _this.append(d); });
return this;
};
/**
* Converts the `LinkedList` to an `Array()`.
*
* @param dataOnly Specifies if only the data of the `Nodes` should be put into the array
* @timecomplexity `O(n)`
* @returns The `LinkedList` as an array
*/
LinkedList.prototype.toArray = function (dataOnly) {
var nodes = [];
var currentNode = this.head;
while (currentNode) {
if (dataOnly)
nodes.push(currentNode.data);
else
nodes.push(currentNode);
currentNode = currentNode.child;
}
return nodes;
};
/**
* Converts the `LinkedList` to an string.
*
* @timecomplexity ``O(n)``
* @returns The `LinkedList` as a string
*/
LinkedList.prototype.toString = function (callback) {
return this.toArray(false)
.map(function (node) { return node.toString(callback); })
.toString();
};
/**
* Reverses the order of the `LinkedList`
*
* @timecomplexity ``O(n)``
* @returns The `LinkedList` instance
*/
LinkedList.prototype.reverse = function () {
var currentNode = this.head;
var previousNode = null;
var nextNode = null;
while (currentNode) {
nextNode = currentNode.child;
currentNode.child = previousNode;
previousNode = currentNode;
currentNode = nextNode;
}
this.tail = this.head;
this.head = previousNode;
return this;
};
return LinkedList;
}());
exports.default = LinkedList;
//# sourceMappingURL=LinkedList.js.map