UNPKG

algs-adt

Version:

An npm package for using data structures like queues or graphs in javascript or typescript

242 lines (241 loc) 9.78 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.LinkedList = void 0; var List_1 = require("../ADT/List"); var LinkedNode_1 = require("./LinkedNode"); var LinkedList = /** @class */ (function (_super) { __extends(LinkedList, _super); function LinkedList(compareFunction) { var _this = _super.call(this, compareFunction) || this; _this.first = undefined; _this.last = undefined; _this.listSize = 0; return _this; } LinkedList.prototype.addElement = function (element, position) { if (position > this.size() || position < 0) { throw new Error("Array indices out of range"); } else if (position === 0) { this.addFirst(element); } else if (position === this.size()) { this.addLast(element); } else { var currentPos = 0; var currentNode = this.first; var nextNode = this.first; while (currentPos < position) { currentNode = nextNode; nextNode = currentNode === null || currentNode === void 0 ? void 0 : currentNode.getNext(); currentPos++; } var newNode = new LinkedNode_1.LinkedNode(element); newNode.setNext(nextNode); currentNode === null || currentNode === void 0 ? void 0 : currentNode.setNext(newNode); } this.listSize += 1; }; LinkedList.prototype.addFirst = function (element) { var newNode = new LinkedNode_1.LinkedNode(element); if (this.first) { newNode.setNext(this.first); this.first = newNode; } else { this.first = newNode; this.last = newNode; } this.listSize += 1; }; LinkedList.prototype.addLast = function (element) { var _a; var newNode = new LinkedNode_1.LinkedNode(element); if (this.first) { (_a = this.last) === null || _a === void 0 ? void 0 : _a.setNext(newNode); this.last = newNode; } else { this.first = newNode; this.last = newNode; } this.listSize += 1; }; LinkedList.prototype.changeInfo = function (element, position) { var currentPos = 0; var prevNode = this.first; var currentNode = this.first; while (currentPos < position) { prevNode = currentNode; currentNode = currentNode === null || currentNode === void 0 ? void 0 : currentNode.getNext(); currentPos++; } currentNode === null || currentNode === void 0 ? void 0 : currentNode.setValue(element); return element; }; LinkedList.prototype.deleteElement = function (position) { var current; if (position > this.size() || position < 0) { throw new Error("Array indices out of range"); } else if (position === 0) { return this.deleteFirst(); } else if (position === this.size()) { return this.deleteLast(); } else { var currentPos = 0; var prev = this.first; var currentNode = this.first; while (currentPos < position) { prev = currentNode; currentNode = currentNode === null || currentNode === void 0 ? void 0 : currentNode.getNext(); currentPos++; } current = currentNode === null || currentNode === void 0 ? void 0 : currentNode.getValue(); prev === null || prev === void 0 ? void 0 : prev.setNext(currentNode === null || currentNode === void 0 ? void 0 : currentNode.getNext()); } this.listSize -= 1; return current; }; LinkedList.prototype.deleteFirst = function () { var _a, _b, _c; if (this.isEmpty()) { throw new Error("Array indices out of range"); } else if (this.size() === 1) { var elem = (_a = this.first) === null || _a === void 0 ? void 0 : _a.getValue(); this.first = undefined; this.last = undefined; this.listSize -= 1; return elem; } else { var elem = (_b = this.first) === null || _b === void 0 ? void 0 : _b.getValue(); this.first = (_c = this.first) === null || _c === void 0 ? void 0 : _c.getNext(); this.listSize -= 1; return elem; } }; LinkedList.prototype.deleteLast = function () { var _a; if (this.isEmpty()) { throw new Error("Array indices out of range"); } else if (this.size() === 1) { var elem = (_a = this.first) === null || _a === void 0 ? void 0 : _a.getValue(); this.first = undefined; this.last = undefined; this.listSize -= 1; return elem; } else { var prev = this.first; var currentNode = this.first; while (currentNode === null || currentNode === void 0 ? void 0 : currentNode.hasNext()) { prev = currentNode; currentNode = currentNode === null || currentNode === void 0 ? void 0 : currentNode.getNext(); } prev === null || prev === void 0 ? void 0 : prev.setNext(undefined); this.last = prev; this.listSize -= 1; return currentNode === null || currentNode === void 0 ? void 0 : currentNode.getValue(); } }; LinkedList.prototype.firstElement = function () { var _a; if (this.isEmpty()) { throw new Error("Array indices out of range"); } return (_a = this.first) === null || _a === void 0 ? void 0 : _a.getValue(); }; LinkedList.prototype.forEach = function (callback) { var _a; if (this.isEmpty()) { return; } else if (this.size() === 1) { var elem = (_a = this.first) === null || _a === void 0 ? void 0 : _a.getValue(); callback(elem); } else { var currentNode = this.first; while (currentNode === null || currentNode === void 0 ? void 0 : currentNode.hasNext()) { callback(currentNode === null || currentNode === void 0 ? void 0 : currentNode.getValue()); currentNode = currentNode === null || currentNode === void 0 ? void 0 : currentNode.getNext(); } callback(currentNode === null || currentNode === void 0 ? void 0 : currentNode.getValue()); } }; LinkedList.prototype.getElement = function (position) { var current; if (position > this.size() || position < 0) { throw new Error("Array indices out of range"); } else if (position === 0) { return this.firstElement(); } else if (position === this.size()) { return this.lastElement(); } else { var currentPos = 0; var prev = this.first; var currentNode = this.first; while (currentPos < position) { prev = currentNode; currentNode = currentNode === null || currentNode === void 0 ? void 0 : currentNode.getNext(); currentPos++; } current = currentNode === null || currentNode === void 0 ? void 0 : currentNode.getValue(); } return current; }; LinkedList.prototype.isEmpty = function () { return this.size() === 0; }; LinkedList.prototype.lastElement = function () { var _a; return (_a = this.last) === null || _a === void 0 ? void 0 : _a.getValue(); }; LinkedList.prototype.size = function () { return this.listSize; }; LinkedList.prototype.subList = function (position, numberElements) { var sublist = new LinkedList(this.compareFunction); var currentPos = 0; var prevNode = this.first; var currentNode = this.first; while (currentPos < position) { prevNode = currentNode; currentNode = currentNode === null || currentNode === void 0 ? void 0 : currentNode.getNext(); currentPos++; } var count = 0; while (count < numberElements) { sublist.addLast(currentNode === null || currentNode === void 0 ? void 0 : currentNode.getValue()); currentNode = currentNode === null || currentNode === void 0 ? void 0 : currentNode.getNext(); count += 1; } return sublist; }; return LinkedList; }(List_1.List)); exports.LinkedList = LinkedList;