Praktikum_mf

Двусвязный список

Mar 30th, 2021
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class DoublyLinkedList {
  2.     constructor() {
  3.                 this.size = 0;
  4.         this.head = null;
  5.         this.tail = null;
  6.     }
  7.  
  8.     add(value, index) {
  9.         const node = createNode(value);
  10.  
  11.         if (index === undefined) {
  12.             if (this.tail) {
  13.                 node.prev = this.tail;
  14.                 this.tail.next = node;
  15.                 this.tail = node;
  16.             } else {
  17.                 this.head = node;
  18.                 this.tail = node;
  19.             }
  20.         } else {
  21.             const nodeAtIndex = this.searchByIndex(index);
  22.  
  23.             node.next = nodeAtIndex;
  24.             node.prev = nodeAtIndex.prev;
  25.  
  26.             if (nodeAtIndex.prev) {
  27.                 nodeAtIndex.prev.next = node;
  28.             }
  29.  
  30.             nodeAtIndex.prev = node;
  31.  
  32.             if (nodeAtIndex === this.head) {
  33.                 this.head = node;
  34.             }
  35.         }
  36.  
  37.         this.size++;
  38.     }
  39.  
  40.     removeByValue(value) {
  41.         const node = this.searchByValue(value);
  42.  
  43.         if (!node) {
  44.             return;
  45.         }
  46.  
  47.         this._removeNode(node);
  48.     }
  49.  
  50.     removeByIndex(index) {
  51.         this._removeNode(this.searchByIndex(index));
  52.     }
  53.  
  54.     searchByIndex(index) {
  55.         this._checkIndex(index);
  56.  
  57.         let node = this.head;
  58.  
  59.         for (let i = 0; i < index; i++) {
  60.             node = node.next;
  61.         }
  62.  
  63.         return node;
  64.     }
  65.  
  66.     searchByValue(value, startIndex = 0) {
  67.         let node = startIndex ? this.searchByIndex(startIndex) : this.head;
  68.  
  69.         while (node && node.value !== value) {
  70.             node = node.next;
  71.         }
  72.  
  73.         return node;
  74.     }
  75.  
  76.     _checkIndex(index) {
  77.         if (index >= this.size) {
  78.             throw new Error('Индекс за пределами списка');
  79.         }
  80.     }
  81.  
  82.     _removeNode(node) {
  83.         const prevNode = node.prev;
  84.         const nextNode = node.next;
  85.  
  86.         if (prevNode) {
  87.             prevNode.next = nextNode;
  88.         }
  89.  
  90.         if (nextNode) {
  91.             nextNode.prev = prevNode;
  92.         }
  93.  
  94.         node.prev = null;
  95.         node.next = null;
  96.  
  97.         if (node === this.head) {
  98.             this.head = nextNode;
  99.         }
  100.  
  101.         if (node === this.tail) {
  102.             this.tail = prevNode;
  103.         }
  104.  
  105.         this.size--;
  106.     }
  107. }
  108.  
  109. function createNode(value) {
  110.     return {
  111.         value,
  112.         next: null,
  113.         prev: null,
  114.     };
  115. }
Advertisement
Add Comment
Please, Sign In to add comment