linked-list-lib
Version:
This package contain some possibilities of implementations of linked lists
48 lines (47 loc) • 1.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DoublyLinkedList = void 0;
const singly_linked_list_1 = require("../singly-linked-list/singly-linked-list");
const comparator_1 = require("../comparator");
class DoublyLinkedList extends singly_linked_list_1.SinglyLinkedList {
constructor(comparator = new comparator_1.Comparator()) {
super(comparator);
this.comparator = comparator;
}
insertInBegin(data) {
let node = super.insertInBegin(data);
if (node.next) {
node.next.prev = this.head;
}
return node;
}
deleteNode(data) {
let nodeToRemove = super.deleteNode(data);
if (nodeToRemove) {
if (nodeToRemove.next) {
nodeToRemove.next.prev = nodeToRemove.prev;
}
}
let result = nodeToRemove;
nodeToRemove = null;
return result;
}
deleteLastNode() {
if (this.isEmpty()) {
return null;
}
let result = this.head;
if (!this.head.next) {
this.head = null;
this.tail = null;
this.listSize--;
return result;
}
result = this.tail;
this.tail = this.tail.prev;
this.tail.next = null;
this.listSize--;
return result;
}
}
exports.DoublyLinkedList = DoublyLinkedList;