UNPKG

couchpotato-linked-list

Version:

DoublyLinkedList implementation in javascript

245 lines (214 loc) 5.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LinkedList = exports.Item = void 0; 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); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } var Item = /*#__PURE__*/function () { function Item(value) { var next = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; var prev = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; _classCallCheck(this, Item); this._value = value; this._next = next; this._prev = prev; } _createClass(Item, [{ key: "getNext", value: function getNext() { return this._next; } }, { key: "setNext", value: function setNext(item) { this._next = item; return this; } }, { key: "getPrev", value: function getPrev() { return this._prev; } }, { key: "setPrev", value: function setPrev(item) { this._prev = item; return this; } }, { key: "getValue", value: function getValue() { return this._value; } }, { key: "setValue", value: function setValue(val) { this._value = val; return this; } }, { key: "hasNext", value: function hasNext() { return this._next !== null; } }, { key: "hasPrev", value: function hasPrev() { return this._prev != null; } }]); return Item; }(); exports.Item = Item; var LinkedList = /*#__PURE__*/function () { function LinkedList() { _classCallCheck(this, LinkedList); this._head = null; this._tail = null; this._length = 0; } _createClass(LinkedList, [{ key: "head", value: function head() { return this._head; } }, { key: "tail", value: function tail() { return this._tail; } }, { key: "length", value: function length() { return this._length; } }, { key: "append", value: function append(value) { var item = new Item(value); if (this._head === null) { this._head = item; } else { item.setPrev(this._tail); this._tail.setNext(item); } this._tail = item; this._length++; return this; } }, { key: "push", value: function push(value) { return this.append(value); } }, { key: "prepend", value: function prepend(value) { var item = new Item(value); if (this._head === null) { this._tail = item; } else { this._head.setPrev(item); item.setNext(this._head); } this._head = item; this._length++; return this; } }, { key: "shift", value: function shift(value) { return this.prepend(value); } }, { key: "removeFromFront", value: function removeFromFront() { if (this._head === null) return undefined;else { var item = this._head; if (this._length === 1) { this._tail = null; this._head = null; } else { this._head = item.getNext(); this._head.setPrev(null); } item.setNext(null); item.setPrev(null); this._length--; return item; } } }, { key: "unshift", value: function unshift() { return this.removeFromFront(); } }, { key: "removeFromEnd", value: function removeFromEnd() { if (this._head === null) return undefined;else { var item = this._tail; if (this._length === 1) { this._head = null; this._tail = null; } else { this._tail = item.getPrev(); this._tail.setNext(null); } item.setNext(null); item.setPrev(null); this._length--; return item; } } }, { key: "pop", value: function pop() { return this.removeFromEnd(); } }, { key: "getNode", value: function getNode(index) { if (index < 0 || index >= this._length) return undefined; var current = this._head; for (var i = 0; i < index; i++) { current = current.getNext(); } return current; } }, { key: "get", value: function get(index) { var node = this.getNode(index); if (node) return node.getValue();else undefined; } }, { key: "removeAt", value: function removeAt(index) { var targetNode = this.getNode(index); var prevNode = targetNode.getPrev(); var nextNode = targetNode.getNext(); // one node if (prevNode == null && nextNode == null) { this._head = null; this._tail = null; } // first node else if (prevNode == null) { this._head = nextNode; nextNode.setPrev(null); } //last node else if (nextNode == null) { this._tail = prevNode; prevNode.setNext(null); } // normal node else { prevNode.setNext(nextNode); nextNode.setPrev(prevNode); } return targetNode.getValue(); } }]); return LinkedList; }(); exports.LinkedList = LinkedList;