UNPKG

@lucadani7/algonodejs-for-beginners

Version:

Just a simple Node.js package with some basic algorithms perfect for people just starting out. It's got easy-to-understand TypeScript versions of stuff like sorting, searching, math with numbers, and messing with strings.

155 lines (154 loc) 4.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LinkedList = void 0; const NodeClass_1 = require("./NodeClass"); class LinkedList { constructor() { this.head = null; this.tail = null; this.count = 0; } addFirst(data) { const node = new NodeClass_1.NodeClass(data, this.head); this.head = node; if (!this.tail) { this.tail = node; } ++this.count; } addLast(data) { const node = new NodeClass_1.NodeClass(data, null); if (!this.head) { this.head = this.tail = node; } else { this.tail.link = node; this.tail = node; } ++this.count; } insertAt(data, position) { if (position < 0 || position > this.count) { throw new RangeError("Index out of bounds!"); } if (position === 0) { this.addFirst(data); return; } if (position === this.count) { this.addLast(data); return; } let current = this.head; for (let i = 0; i < position - 1; current = current.link, ++i) ; current.link = new NodeClass_1.NodeClass(data, current.link); ++this.count; } remove(data) { if (!this.head) { return false; } if (this.head.item === data) { this.head = this.head.link; if (!this.head) { this.tail = null; } --this.count; return true; } let current = this.head; while (current.link && current.link.item !== data) { current = current.link; } if (current.link !== null) { if (current.link === this.tail) { this.tail = current; } current.link = current.link.link; --this.count; return true; } return false; } reverse() { let previous = null; let current = this.head; this.tail = this.head; while (current) { const next = current.link; current.link = previous; previous = current; current = next; } this.head = previous; } contains(data) { for (let node = this.head; node !== null; node = node.link) { if (data === node.item) { return true; } } return false; } get(position) { if (position < 0 || position >= this.count) { throw new RangeError("Index out of bounds!"); } let node = this.head; for (let i = 0; i < position; node = node.link, ++i) ; return node.item; } toArray() { const result = []; for (let node = this.head; node !== null; result.push(node.item), node = node.link) ; return result; } fromArray(arr) { const list = new LinkedList(); for (const item of arr) { list.addLast(item); } return list; } toString() { return this.toArray().join(" -> "); } clone() { const copy = new LinkedList(); for (const item of this) { copy.addLast(item); } return copy; } equals(other) { if (this.count !== other.count) { return false; } const a = this.toArray(); const b = other.toArray(); return a.every((val, i) => val === b[i]); } existsCertainValue(value, compareFn) { const cmp = compareFn ?? ((a, b) => a === b); return this.toArray().some(x => cmp(x, value)); } getFrequencyRecord() { const freq = {}; for (const val of this) { const key = String(val); freq[key] = (freq[key] ?? 0) + 1; } return freq; } clear() { this.head = this.tail = null; this.count = 0; } size() { return this.count; } } exports.LinkedList = LinkedList;