UNPKG

@truck/doubly-linked-list

Version:

A JavaScript implementation of the Doubly Linked-List data structure

216 lines (174 loc) 5.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _lodash = _interopRequireDefault(require("lodash.isfunction")); var _node = _interopRequireDefault(require("./node")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var defaultCallback = function defaultCallback(a) { return a.value; }; var defaultComparator = function defaultComparator(a) { return function (b) { return a === b; }; }; var DoublyLinkedList = /*#__PURE__*/ function () { function DoublyLinkedList() { _classCallCheck(this, DoublyLinkedList); this.head = undefined; this.listSize = 0; this.tail = undefined; } _createClass(DoublyLinkedList, [{ key: "delete", value: function _delete(value) { var comparator = (0, _lodash.default)(value) ? value : defaultComparator(value); var current = this.head; while (current) { if (comparator(current.value)) { var _current = current, next = _current.next, previous = _current.previous; if (next) { next.previous = previous; } if (previous) { previous.next = next; } if (current === this.head) { this.head = next; } if (current === this.tail) { this.tail = previous; } this.listSize -= 1; return true; } current = current.next; } return false; } }, { key: "insert", value: function insert(value) { var node = new _node.default(value); var head = this.head; if (this.head) { node.next = head; head.previous = node; } else { this.tail = node; } this.head = node; this.listSize += 1; } }, { key: "insertAfter", value: function insertAfter(value, after) { var comparator = (0, _lodash.default)(after) ? after : defaultComparator(after); var node = new _node.default(value); var current = this.head; if (!current) { this.head = node; this.tail = node; } else { while (current) { var _current2 = current, next = _current2.next; if (!next) { current.next = node; node.previous = current; this.tail = node; break; } else if (comparator(current.value)) { node.next = next; next.previous = node; current.next = node; node.previous = current; break; } current = next; } } this.listSize += 1; } }, { key: "insertBefore", value: function insertBefore(value, after) { var comparator = (0, _lodash.default)(after) ? after : defaultComparator(after); var node = new _node.default(value); var current = this.head; if (!current) { this.head = node; this.tail = node; this.listSize += 1; return; } while (current) { var _current3 = current, next = _current3.next, previous = _current3.previous, currentValue = _current3.value; if (comparator(currentValue)) { node.next = current; node.previous = previous; if (previous) { previous.next = node; } current.previous = node; if (this.head === current) { this.head = node; } this.listSize += 1; return; } current = next; } current = this.tail; current.next = node; node.previous = current; this.tail = node; this.listSize += 1; } }, { key: "search", value: function search(value) { var comparator = (0, _lodash.default)(value) ? value : defaultComparator(value); var current = this.head; while (current) { if (comparator(current.value)) { return current; } current = current.next; } return undefined; } }, { key: "toArray", value: function toArray() { var callback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultCallback; var returnArray = []; var current = this.head; while (current) { returnArray.push(callback(current)); current = current.next; } return returnArray; } }, { key: "length", get: function get() { return this.listSize; } }]); return DoublyLinkedList; }(); var _default = DoublyLinkedList; exports.default = _default;