linked-list-lib
Version:
This package contain some possibilities of implementations of linked lists
139 lines (138 loc) • 4.02 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SinglyLinkedList = void 0;
const comparator_1 = require("../comparator");
const base_node_linked_list_1 = require("../base-node-linked-list");
class SinglyLinkedList {
constructor(comparator = new comparator_1.Comparator()) {
this.comparator = comparator;
this.head = null;
this.tail = null;
this.listSize = 0;
this.comparator = comparator;
}
firstNode() {
return this.head;
}
insertInBegin(data) {
const node = new base_node_linked_list_1.BaseNodeLinkedList(data); // O(1)
if (this.head) {
node.next = this.head;
}
this.head = node;
if (!this.head.next) {
this.tail = this.head;
}
this.listSize++;
return node;
}
deleteNode(data) {
let nodeToRemove = this.search(data); // O(n)
if (!nodeToRemove) {
return null;
}
const result = nodeToRemove.currentNode;
if (!this.head.next) { // there is 1 node
this.head = null;
this.tail = null;
this.listSize--;
return result;
}
if (!nodeToRemove.previousNode) { // there is more than 1 node (remove first node)
this.head = nodeToRemove.currentNode.next;
nodeToRemove.currentNode = null;
this.listSize--;
return result;
}
nodeToRemove.previousNode.next = nodeToRemove.currentNode.next;
if (!nodeToRemove.currentNode.next) {
this.tail = nodeToRemove.previousNode;
}
nodeToRemove.currentNode = null;
this.listSize--;
return result;
}
deleteFirstNode() {
if (this.isEmpty()) {
return null;
}
let result = this.head;
if (!this.head.next) {
this.head = null;
this.tail = null;
this.listSize--;
return result;
}
this.head = this.head.next;
this.listSize--;
return result;
}
deleteLastNode() {
if (this.isEmpty()) {
return null;
}
let cursor = this.head;
if (!this.head.next) {
this.head = null;
this.tail = null;
this.listSize--;
return cursor;
}
while (cursor.next.next) { // O(n)
cursor = cursor.next;
}
let result = this.tail;
this.tail = null;
this.tail = cursor;
this.tail.next = null;
this.listSize--;
return result;
}
traverse() {
if (this.isEmpty()) {
return [];
}
const array = [];
const addToArray = (node) => {
array.push(node.data);
return node.next ? addToArray(node.next) : array;
};
return addToArray(this.head);
}
print(referenceProperty) {
if (this.isEmpty()) {
return 'NULL';
}
let str = '[HEAD]:';
const addToString = (node) => {
str += (node.data instanceof Object) ?
`${node.data[referenceProperty]}` :
`${node.data}`;
str += ' -> ';
return node.next ? addToString(node.next) : `${str}NULL`;
};
return addToString(this.head);
}
size() {
return this.listSize;
}
isEmpty() {
return !this.head && !this.tail;
}
search(data) {
if (this.isEmpty()) {
return null;
}
let currentNode = this.head;
let previousNode = null;
while (currentNode) {
if (this.comparator.equal(currentNode.data, data)) {
return { currentNode, previousNode };
}
previousNode = currentNode;
currentNode = currentNode.next;
}
return null;
}
}
exports.SinglyLinkedList = SinglyLinkedList;