@gabrielrufino/cube
Version:
Data structures made in Typescript
162 lines (161 loc) • 5.31 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 LinkedList = /** @class */ (function () {
function LinkedList() {
var inputs = [];
for (var _i = 0; _i < arguments.length; _i++) {
inputs[_i] = arguments[_i];
}
this._FIRST_POSITION = 0;
this._head = null;
this._size = 0;
var nodes = inputs.map(function (input) { return new Node_1.default(input); });
for (var i = 0; i < inputs.length - 1; i++) {
nodes[i].next = nodes[i + 1];
}
this._head = nodes[0];
this._size = nodes.length;
}
Object.defineProperty(LinkedList.prototype, "data", {
get: function () {
var _a, _b;
var data = [];
var current = this._head;
while (current) {
data.push({
value: current.value,
next: (_b = (_a = current.next) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : null,
});
current = current.next;
}
return data;
},
enumerable: false,
configurable: true
});
Object.defineProperty(LinkedList.prototype, "size", {
get: function () {
return this._size;
},
enumerable: false,
configurable: true
});
Object.defineProperty(LinkedList.prototype, "isEmpty", {
get: function () {
return this.size === 0;
},
enumerable: false,
configurable: true
});
LinkedList.prototype.positionOf = function (element) {
var current = this._head;
var position = 0;
while (current && current.value !== element) {
current = current.next;
position += 1;
}
if (current) {
return position;
}
};
LinkedList.prototype.push = function (element) {
if (this._head) {
var current = this._head;
while (current.next) {
current = current.next;
}
current.next = new Node_1.default(element);
}
else {
this._head = new Node_1.default(element);
}
this._size += 1;
return element;
};
LinkedList.prototype.remove = function (element) {
var position = this.positionOf(element);
if (position) {
return this.removeFromPosition(position);
}
return null;
};
LinkedList.prototype.insertInPosition = function (element, position) {
if (position < this._FIRST_POSITION || position > this.size) {
return undefined;
}
var node = new Node_1.default(element);
if (position === this._FIRST_POSITION) {
node.next = this._head;
this._head = node;
}
else {
var before = this._getNodeFromPosition(position - 1);
var after = (before === null || before === void 0 ? void 0 : before.next) || null;
if (before) {
before.next = node;
}
node.next = after;
}
this._size += 1;
return element;
};
LinkedList.prototype.getFromPosition = function (position) {
var _a, _b;
var node = this._getNodeFromPosition(position);
if (!node) {
return null;
}
return {
value: node.value,
next: (_b = (_a = node.next) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : null,
};
};
LinkedList.prototype.removeFromPosition = function (position) {
if (position < this._FIRST_POSITION || position >= this.size) {
return null;
}
var current = this._head;
if (position === this._FIRST_POSITION) {
this._head = current.next;
}
else {
var previous = void 0;
for (var i = 0; i < position && current; i++) {
previous = current;
current = current.next;
}
if (previous) {
previous.next = current.next;
}
}
this._size -= 1;
return current.value;
};
LinkedList.prototype._getNodeFromPosition = function (position) {
if (position < this._FIRST_POSITION || position > (this.size - 1)) {
return null;
}
var node = this._head;
for (var i = 0; i < position && (node === null || node === void 0 ? void 0 : node.next); i++) {
node = node.next;
}
return node;
};
LinkedList.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(' => ')),
};
return primitives[type];
};
return LinkedList;
}());
exports.default = LinkedList;