@gabrielrufino/cube
Version:
Data structures made in Typescript
219 lines (218 loc) • 7.71 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var Node_1 = __importDefault(require("./Node"));
var DoublyLinkedList = /** @class */ (function () {
function DoublyLinkedList() {
var inputs = [];
for (var _i = 0; _i < arguments.length; _i++) {
inputs[_i] = arguments[_i];
}
this._head = null;
this._tail = null;
this._size = 0;
var nodes = inputs.map(function (input) { return new Node_1.default(input); });
for (var i = 0; i < inputs.length; i++) {
nodes[i].previous = nodes[i - 1] || null;
nodes[i].next = nodes[i + 1] || null;
}
this._head = nodes[0];
this._tail = nodes[nodes.length - 1];
this._size = inputs.length;
}
Object.defineProperty(DoublyLinkedList.prototype, "data", {
get: function () {
var _a, _b, _c, _d;
var current = this._head;
var data = [];
while (current) {
data.push({
previous: (_b = (_a = current.previous) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : null,
value: current.value,
next: (_d = (_c = current.next) === null || _c === void 0 ? void 0 : _c.value) !== null && _d !== void 0 ? _d : null,
});
current = current.next;
}
return data;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DoublyLinkedList.prototype, "size", {
get: function () {
return this._size;
},
enumerable: false,
configurable: true
});
/**
* Complexity: O(1)
*/
DoublyLinkedList.prototype.push = function (element) {
var node = new Node_1.default(element);
if (this._tail) {
this._tail.next = node;
node.previous = this._tail;
}
else {
this._head = node;
this._tail = node;
}
this._size += 1;
return element;
};
/**
* Complexity: O(n/2)
*/
DoublyLinkedList.prototype.getFromPosition = function (position) {
var _a, _b, _c, _d;
if (position < 0 || position >= this.size) {
return undefined;
}
var distanceToTheHead = position;
var distanceToTheTail = this.size - position - 1;
var current;
if (distanceToTheTail > distanceToTheHead) {
current = this._head;
for (var i = 0; i < position; i++) {
current = (current === null || current === void 0 ? void 0 : current.next) || null;
}
}
else {
current = this._tail;
for (var i = this.size - 1; i > position; i--) {
current = (current === null || current === void 0 ? void 0 : current.previous) || null;
}
}
if (current === null || current === void 0 ? void 0 : current.value) {
return {
previous: (_b = (_a = current.previous) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : null,
value: current.value,
next: (_d = (_c = current.next) === null || _c === void 0 ? void 0 : _c.value) !== null && _d !== void 0 ? _d : null,
};
}
};
DoublyLinkedList.prototype.positionOf = function (element) {
var current = this._head;
var position = 0;
while (current && current.value !== element) {
current = current.next;
position += 1;
}
return position === this.size ? undefined : position;
};
/**
* Complexity: O(n)
*/
DoublyLinkedList.prototype.insertInPosition = function (element, position) {
var _a, _b;
if (position < 0 || position > this.size) {
return undefined;
}
var node = new Node_1.default(element);
if (position === 0 && this._head) {
node.next = this._head;
this._head.previous = node;
this._head = node;
this._size += 1;
return element;
}
if (position === this.size - 1 && ((_a = this._tail) === null || _a === void 0 ? void 0 : _a.previous)) {
this._tail.previous.next = node;
node.previous = ((_b = this._tail) === null || _b === void 0 ? void 0 : _b.previous) || null;
node.next = this._tail;
this._tail.previous = node;
this._size += 1;
return element;
}
if (position === this.size && this._tail) {
node.previous = this._tail;
this._tail.next = node;
this._tail = node;
this._size += 1;
return element;
}
var way = this._findFasterWayToPosition(position);
var current;
if (way === 'ASC') {
current = this._head;
for (var i = 0; i < position; i++) {
current = (current === null || current === void 0 ? void 0 : current.next) || null;
}
}
else {
current = this._tail;
for (var i = this.size - 1; i > position; i--) {
current = (current === null || current === void 0 ? void 0 : current.previous) || null;
}
}
if (current === null || current === void 0 ? void 0 : current.previous) {
current.previous.next = node;
node.previous = current.previous;
node.next = current;
current.previous = node;
}
this._size += 1;
return element;
};
/**
* Complexity: O(n)
*/
DoublyLinkedList.prototype.remove = function (element) {
var current = this._head;
while (current && current.value !== element) {
current = current.next;
}
if (current) {
if (current.previous) {
current.previous.next = current.next;
}
if (current.next) {
current.next.previous = current.previous;
}
if (current === this._head) {
this._head = current.next;
}
if (current === this._tail) {
this._tail = current.previous;
}
this._size -= 1;
return current.value;
}
return undefined;
};
/**
* Complexity: O(n)
*/
DoublyLinkedList.prototype.removeFromPosition = function (position) {
var item = this.getFromPosition(position);
if (item) {
this.remove(item.value);
return item.value;
}
return undefined;
};
DoublyLinkedList.prototype._findFasterWayToPosition = function (position) {
var distanceToTheHead = position;
var distanceToTheTail = this.size - position - 1;
if (distanceToTheTail > distanceToTheHead) {
return 'ASC';
}
return 'DESC';
};
DoublyLinkedList.prototype[Symbol.toPrimitive] = function (type) {
var primitives = {
default: true,
number: this.size,
string: "[Head] ".concat(this.data.map(function (_a) {
var value = _a.value;
return value;
}).join(' <=> '), " [Tail]"),
};
return primitives[type];
};
return DoublyLinkedList;
}());
exports.default = DoublyLinkedList;