Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 | 19x 10248x 10248x 10248x 10183x 10167x 10281x 172x 20282x 19x 60x 60x 60x 70328x 20040x 10355x 200x 29x 192x 192x 31x 31x 161x 161x 161x 192x 3x 2x 2x 2x 2x 2x 2x 10006x 10006x 10006x 3x 3x 10003x 10003x 10006x 10006x 10001x 10001x 1x 1x 10000x 10000x 10000x 10001x 5x 5x 5x 11x 5x 1x 1x 1x 1x 1x 13x 13x 75x 11x 64x 2x 3x 3x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 4x 13x 13x 13x 41x 41x 13x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 6x 6x 2x 2x 2x 2x 6x 6x 6x 1x 1x 1x 3x 3x 3x 2x 2x 2x 6x 6x 2x 2x 2x 2x 8x 4x 8x 2x 2x 2x 2x 7x 7x 2x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 53x 49x 4x 53x 52x 52x 52x 52x 52x 52x 52x 52x 52x 1x | /**
* data-structure-typed
*
* @author Tyler Zeng
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
* @license MIT License
*/
export class DoublyLinkedListNode<T = number> {
/**
* The constructor function initializes the value, next, and previous properties of an object.
* @param {T} val - The "val" parameter is the value that will be stored in the node. It can be of any data type, as it
* is defined as a generic type "T".
*/
constructor(val: T) {
this._val = val;
this._next = null;
this._prev = null;
}
private _val: T;
get val(): T {
return this._val;
}
set val(value: T) {
this._val = value;
}
private _next: DoublyLinkedListNode<T> | null;
get next(): DoublyLinkedListNode<T> | null {
return this._next;
}
set next(value: DoublyLinkedListNode<T> | null) {
this._next = value;
}
private _prev: DoublyLinkedListNode<T> | null;
get prev(): DoublyLinkedListNode<T> | null {
return this._prev;
}
set prev(value: DoublyLinkedListNode<T> | null) {
this._prev = value;
}
}
export class DoublyLinkedList<T = any> {
/**
* The constructor initializes the linked list with an empty head, tail, and length.
*/
constructor() {
this._head = null;
this._tail = null;
this._length = 0;
}
private _head: DoublyLinkedListNode<T> | null;
get head(): DoublyLinkedListNode<T> | null {
return this._head;
}
set head(value: DoublyLinkedListNode<T> | null) {
this._head = value;
}
private _tail: DoublyLinkedListNode<T> | null;
get tail(): DoublyLinkedListNode<T> | null {
return this._tail;
}
set tail(value: DoublyLinkedListNode<T> | null) {
this._tail = value;
}
private _length: number;
get length(): number {
return this._length;
}
/**
* The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
* given array.
* @param {T[]} data - The `data` parameter is an array of elements of type `T`.
* @returns The `fromArray` function returns a DoublyLinkedList object.
*/
static fromArray<T>(data: T[]) {
const doublyLinkedList = new DoublyLinkedList<T>();
for (const item of data) {
doublyLinkedList.push(item);
}
return doublyLinkedList;
}
/**
* The push function adds a new node with the given value to the end of the doubly linked list.
* @param {T} val - The value to be added to the linked list.
*/
push(val: T): void {
const newNode = new DoublyLinkedListNode(val);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
newNode.prev = this.tail;
this.tail!.next = newNode;
this.tail = newNode;
}
this._length++;
}
/**
* The `pop()` function removes and returns the value of the last node in a doubly linked list.
* @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
* list is empty, it returns null.
*/
pop(): T | undefined {
if (!this.tail) return undefined;
const removedNode = this.tail;
Iif (this.head === this.tail) {
this.head = null;
this.tail = null;
} else {
this.tail = removedNode.prev;
this.tail!.next = null;
}
this._length--;
return removedNode.val;
}
/**
* The `shift()` function removes and returns the value of the first node in a doubly linked list.
* @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
* list.
*/
shift(): T | undefined {
Iif (!this.head) return undefined;
const removedNode = this.head;
if (this.head === this.tail) {
this.head = null;
this.tail = null;
} else {
this.head = removedNode.next;
this.head!.prev = null;
}
this._length--;
return removedNode.val;
}
/**
* The unshift function adds a new node with the given value to the beginning of a doubly linked list.
* @param {T} val - The `val` parameter represents the value of the new node that will be added to the beginning of the
* doubly linked list.
*/
unshift(val: T): void {
const newNode = new DoublyLinkedListNode(val);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
} else {
newNode.next = this.head;
this.head!.prev = newNode;
this.head = newNode;
}
this._length++;
}
/**
* The `getAt` function returns the value at a specified index in a linked list, or null if the index is out of bounds.
* @param {number} index - The index parameter is a number that represents the position of the element we want to
* retrieve from the list.
* @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds
* or the linked list is empty, it will return null.
*/
getAt(index: number): T | undefined {
Iif (index < 0 || index >= this.length) return undefined;
let current = this.head;
for (let i = 0; i < index; i++) {
current = current!.next;
}
return current!.val;
}
/**
* The function `getNodeAt` returns the node at a given index in a doubly linked list, or null if the index is out of
* range.
* @param {number} index - The `index` parameter is a number that represents the position of the node we want to
* retrieve from the doubly linked list. It indicates the zero-based index of the node we want to access.
* @returns The method `getNodeAt(index: number)` returns a `DoublyLinkedListNode<T>` object if the index is within the
* valid range of the linked list, otherwise it returns `null`.
*/
getNodeAt(index: number): DoublyLinkedListNode<T> | null {
Iif (index < 0 || index >= this.length) return null;
let current = this.head;
for (let i = 0; i < index; i++) {
current = current!.next;
}
return current;
}
/**
* The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
* node if found, otherwise it returns null.
* @param {T} val - The `val` parameter is the value that we want to search for in the doubly linked list.
* @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<T>` if a node with the specified value `val`
* is found in the linked list. If no such node is found, it returns `null`.
*/
findNode(val: T): DoublyLinkedListNode<T> | null {
let current = this.head;
while (current) {
if (current.val === val) {
return current;
}
current = current.next;
}
return null;
}
/**
* The `insert` function inserts a value at a specified index in a doubly linked list.
* @param {number} index - The index parameter represents the position at which the new value should be inserted in the
* DoublyLinkedList. It is of type number.
* @param {T} val - The `val` parameter represents the value that you want to insert into the Doubly Linked List at the
* specified index.
* @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
* if the index is out of bounds.
*/
insertAt(index: number, val: T): boolean {
Iif (index < 0 || index > this.length) return false;
if (index === 0) {
this.unshift(val);
return true;
}
if (index === this.length) {
this.push(val);
return true;
}
const newNode = new DoublyLinkedListNode(val);
const prevNode = this.getNodeAt(index - 1);
const nextNode = prevNode!.next;
newNode.prev = prevNode;
newNode.next = nextNode;
prevNode!.next = newNode;
nextNode!.prev = newNode;
this._length++;
return true;
}
/**
* The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
* @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
* data structure. It is of type number.
* @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
* bounds.
*/
deleteAt(index: number): T | undefined {
Iif (index < 0 || index >= this.length) return undefined;
if (index === 0) return this.shift();
Iif (index === this.length - 1) return this.pop();
const removedNode = this.getNodeAt(index);
const prevNode = removedNode!.prev;
const nextNode = removedNode!.next;
prevNode!.next = nextNode;
nextNode!.prev = prevNode;
this._length--;
return removedNode!.val;
}
delete(valOrNode: T): boolean;
delete(valOrNode: DoublyLinkedListNode<T>): boolean;
/**
* The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
* @param {T | DoublyLinkedListNode<T>} valOrNode - The `valOrNode` parameter can accept either a value of type `T` or
* a `DoublyLinkedListNode<T>` object.
* @returns The `delete` method returns a boolean value. It returns `true` if the value or node was successfully
* deleted from the doubly linked list, and `false` if the value or node was not found in the list.
*/
delete(valOrNode: T | DoublyLinkedListNode<T>): boolean {
let node: DoublyLinkedListNode<T> | null;
Iif (valOrNode instanceof DoublyLinkedListNode) {
node = valOrNode;
} else {
node = this.findNode(valOrNode);
}
if (node) {
if (node === this.head) {
this.shift();
} else Iif (node === this.tail) {
this.pop();
} else {
const prevNode = node.prev;
const nextNode = node.next;
prevNode!.next = nextNode;
nextNode!.prev = prevNode;
this._length--;
}
return true;
}
return false;
}
/**
* The `toArray` function converts a linked list into an array.
* @returns The `toArray()` method is returning an array of type `T[]`.
*/
toArray(): T[] {
const array: T[] = [];
let current = this.head;
while (current) {
array.push(current.val);
current = current.next;
}
return array;
}
/**
* The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
*/
clear(): void {
this._head = null;
this._tail = null;
this._length = 0;
}
/**
* The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
* @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
* function is used to determine whether a particular value in the linked list satisfies a certain condition.
* @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
* the callback function. If no element satisfies the condition, it returns `null`.
*/
find(callback: (val: T) => boolean): T | null {
let current = this.head;
while (current) {
if (callback(current.val)) {
return current.val;
}
current = current.next;
}
return null;
}
/**
* The function returns the index of the first occurrence of a given value in a linked list.
* @param {T} val - The parameter `val` is of type `T`, which means it can be any data type. It represents the value
* that we are searching for in the linked list.
* @returns The method `indexOf` returns the index of the first occurrence of the specified value `val` in the linked
* list. If the value is not found, it returns -1.
*/
indexOf(val: T): number {
let index = 0;
let current = this.head;
while (current) {
if (current.val === val) {
return index;
}
index++;
current = current.next;
}
return -1;
}
/**
* The `findLast` function iterates through a linked list from the last node to the first node and returns the last
* value that satisfies the given callback function, or null if no value satisfies the callback.
* @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
* function is used to determine whether a given value satisfies a certain condition.
* @returns The method `findLast` returns the last value in the linked list that satisfies the condition specified by
* the callback function. If no value satisfies the condition, it returns `null`.
*/
findLast(callback: (val: T) => boolean): T | null {
let current = this.tail;
while (current) {
if (callback(current.val)) {
return current.val;
}
current = current.prev;
}
return null;
}
/**
* The `toArrayReverse` function converts a doubly linked list into an array in reverse order.
* @returns The `toArrayReverse()` function returns an array of type `T[]`.
*/
toArrayReverse(): T[] {
const array: T[] = [];
let current = this.tail;
while (current) {
array.push(current.val);
current = current.prev;
}
return array;
}
/**
* The `reverse` function reverses the order of the elements in a doubly linked list.
*/
reverse(): void {
let current = this.head;
[this.head, this.tail] = [this.tail, this.head];
while (current) {
const next = current.next;
[current.prev, current.next] = [current.next, current.prev];
current = next;
}
}
/**
* The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
* @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
* represents the value of the current node in the linked list, and the index argument represents the index of the
* current node in the linked list.
*/
forEach(callback: (val: T, index: number) => void): void {
let current = this.head;
let index = 0;
while (current) {
callback(current.val, index);
current = current.next;
index++;
}
}
/**
* The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
* DoublyLinkedList with the transformed values.
* @param callback - The callback parameter is a function that takes a value of type T (the type of values stored in
* the original DoublyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
* DoublyLinkedList).
* @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
*/
map<U>(callback: (val: T) => U): DoublyLinkedList<U> {
const mappedList = new DoublyLinkedList<U>();
let current = this.head;
while (current) {
mappedList.push(callback(current.val));
current = current.next;
}
return mappedList;
}
/**
* The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
* elements that satisfy the given callback function.
* @param callback - The `callback` parameter is a function that takes a value of type `T` and returns a boolean value.
* It is used to determine whether a value should be included in the filtered list or not.
* @returns The filtered list, which is an instance of the DoublyLinkedList class.
*/
filter(callback: (val: T) => boolean): DoublyLinkedList<T> {
const filteredList = new DoublyLinkedList<T>();
let current = this.head;
while (current) {
if (callback(current.val)) {
filteredList.push(current.val);
}
current = current.next;
}
return filteredList;
}
/**
* The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
* single value.
* @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
* used to perform a specific operation on each element of the linked list.
* @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
* point for the reduction operation.
* @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
* elements in the linked list.
*/
reduce<U>(callback: (accumulator: U, val: T) => U, initialValue: U): U {
let accumulator = initialValue;
let current = this.head;
while (current) {
accumulator = callback(accumulator, current.val);
current = current.next;
}
return accumulator;
}
insertAfter(existingValueOrNode: T, newValue: T): boolean;
insertAfter(existingValueOrNode: DoublyLinkedListNode<T>, newValue: T): boolean;
/**
* The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
* @param {T | DoublyLinkedListNode<T>} existingValueOrNode - The existing value or node in the doubly linked list
* after which the new value will be inserted. It can be either the value of the existing node or the existing node
* itself.
* @param {T} newValue - The value that you want to insert into the doubly linked list.
* @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
* existing value or node is not found in the doubly linked list.
*/
insertAfter(existingValueOrNode: T | DoublyLinkedListNode<T>, newValue: T): boolean {
let existingNode;
Iif (existingValueOrNode instanceof DoublyLinkedListNode) {
existingNode = existingValueOrNode;
} else {
existingNode = this.findNode(existingValueOrNode);
}
if (existingNode) {
const newNode = new DoublyLinkedListNode(newValue);
newNode.next = existingNode.next;
if (existingNode.next) {
existingNode.next.prev = newNode;
}
newNode.prev = existingNode;
existingNode.next = newNode;
Iif (existingNode === this.tail) {
this.tail = newNode;
}
this._length++;
return true;
}
return false;
}
insertBefore(existingValueOrNode: T, newValue: T): boolean;
insertBefore(existingValueOrNode: DoublyLinkedListNode<T>, newValue: T): boolean;
/**
* The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
* @param {T | DoublyLinkedListNode<T>} existingValueOrNode - The existing value or node in the doubly linked list
* before which the new value will be inserted. It can be either the value of the existing node or the existing node
* itself.
* @param {T} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
* list.
* @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
* insertion fails.
*/
insertBefore(existingValueOrNode: T | DoublyLinkedListNode<T>, newValue: T): boolean {
let existingNode;
if (existingValueOrNode instanceof DoublyLinkedListNode) {
existingNode = existingValueOrNode;
} else {
existingNode = this.findNode(existingValueOrNode);
}
if (existingNode) {
const newNode = new DoublyLinkedListNode(newValue);
newNode.prev = existingNode.prev;
if (existingNode.prev) {
existingNode.prev.next = newNode;
}
newNode.next = existingNode;
existingNode.prev = newNode;
Iif (existingNode === this.head) {
this.head = newNode;
}
this._length++;
return true;
}
return false;
}
}
|