UNPKG

list-runner

Version:

a lightweight linked-list implementation that offers both Singly (next) and Doubly data structures (next and previous)

117 lines (100 loc) 3.51 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.StemSingly = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _SentinelSingly = require("./SentinelSingly"); var StemSingly = /*#__PURE__*/ function () { function StemSingly(head) { var Sentinel = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _SentinelSingly.SentinelSingly; (0, _classCallCheck2["default"])(this, StemSingly); this.sentinelHead = new Sentinel(head); this.sentinelTail = new Sentinel(); head.setNext(this.sentinelTail); } (0, _createClass2["default"])(StemSingly, [{ key: "getHead", value: function getHead() { return this.sentinelHead.getNext(); } }, { key: "interlink", value: function interlink(cells) { var looper; for (looper = 0; looper < cells.length - 1; looper++) { cells[looper].setNext(cells[looper + 1]); } var lastCellIndex = cells.length - 1; cells[lastCellIndex].setNext(this.sentinelTail); } // interlinks the chain of next pointers for the provided cells }, { key: "interlinkNext", value: function interlinkNext(cell1, cell2, cell3) { cell1.setNext(cell2); cell2.setNext(cell3); } // cell2 (the cell between cell1 and cell3) gets dereferenced (ie. cell2 is deleted from the stem) }, { key: "unlinkNext", value: function unlinkNext(cell1, cell3) { cell1.setNext(cell3); } }, { key: "insert", value: function insert(cell, baseline) { // NOTE: if the baseline cell is this.sentinelTail then we are at the end of the stem and cannot insert the new cell if (baseline === this.sentinelTail) { return false; } this.interlinkNext(baseline, cell, baseline.getNext()); return true; } }, { key: "extract", value: function extract(baseline) { if (baseline === this.sentinelTail || baseline.getNext() === this.sentinelTail) { return false; } var extractionResult = baseline.getNext(); this.unlinkNext(baseline, baseline.getNext().getNext()); return extractionResult; } }, { key: "unshift", value: function unshift(cell) { return this.insert(cell, this.sentinelHead); } }, { key: "shift", value: function shift() { return this.extract(this.sentinelHead); } }, { key: "replace", value: function replace(cell, baseline) { // NOTE: if baseline is this.sentinelTail or the previous cell then we are at the end of the stem and cannot perform the replacement if (baseline === this.sentinelTail || baseline.getNext() === this.sentinelTail) { return false; } this.interlinkNext(baseline, cell, baseline.getNext().getNext()); return true; } }, { key: "delete", value: function _delete(baseline) { if (baseline === this.sentinelHead || baseline === this.sentinelTail) { return false; } var newSentinel = new _SentinelSingly.SentinelSingly(); baseline.setNext(newSentinel); this.sentinelTail = newSentinel; return true; } }]); return StemSingly; }(); // eslint-disable-line import/prefer-default-export exports.StemSingly = StemSingly;