Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Linked List in TypeScript

fueanta May 16th, 2020 (edited) 1,119 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. interface Node<T> {
  2.     prev: Node<T>,
  3.     value: T,
  4.     next: Node<T>,
  5. }
  6.  
  7.  
  8. export default class LinkedList<T> {
  9.     private _head: Node<T>;
  10.     private _tail: Node<T>;
  11.  
  12.     constructor() {
  13.         this._head = null;
  14.         this._tail = null;
  15.     }
  16.  
  17.     addToHead(value: T): void {
  18.         const newNode: Node<T> = {
  19.             prev: null,
  20.             value: value,
  21.             next: this._head,
  22.         };
  23.  
  24.         // Linked list has already been initialized
  25.         if (this._head) this._head.prev = newNode;
  26.         // Linked list has not been initialized yet
  27.         else this._tail = newNode;
  28.  
  29.         this._head = newNode;
  30.     }
  31.  
  32.     addToTail(value: T): void {
  33.         const newNode: Node<T> = {
  34.             prev: this._tail,
  35.             value: value,
  36.             next: null,
  37.         };
  38.  
  39.         // Linked list has already been initialized
  40.         if (this._tail) this._tail.next = newNode;
  41.         // Linked list has not been initialized yet
  42.         else this._head = newNode;
  43.  
  44.         this._tail = newNode;
  45.     }
  46.  
  47.     removeHead(): T {
  48.         if (!this._head) return null;
  49.  
  50.         const value = this._head.value;
  51.         this._head = this._head.next;
  52.  
  53.         // Linked list has at least one node
  54.         if (this._head) this._head.prev = null;
  55.         // Linked list is empty
  56.         else this._tail = null;
  57.  
  58.         return value;
  59.     }
  60.  
  61.     removeTail(): T {
  62.         if (!this._tail) return null;
  63.  
  64.         const value = this._tail.value;
  65.         this._tail = this._tail.prev;
  66.  
  67.         // Linked list has at leaast one node
  68.         if (this._tail) this._tail.next = null;
  69.         // Linked list is empty
  70.         else this._head = null;
  71.  
  72.         return value;
  73.     }
  74.  
  75.     search(value: T): boolean {
  76.         for (let pointer: Node<T> = this._head; pointer; pointer = pointer.next)
  77.             if (pointer.value === value) return true;
  78.  
  79.         return false;
  80.     }
  81.  
  82.     indexOf(value: T): Array<number> {
  83.         const indices: Array<number> = [];
  84.  
  85.         for (let pointer: Node<T> = this._head, i: number = 0; pointer; pointer = pointer.next, i++)
  86.             if (pointer.value === value) indices.push(i);
  87.  
  88.         return indices;
  89.     }
  90. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top