momo3141

DLL

Mar 7th, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { inheritLeadingComments } from '../../../../../../Library/Caches/typescript/4.9/node_modules/@babel/types/lib/index.js';
  2. import LinkedListNode from './linked-list-node.js';
  3.  
  4. export default class DoublyLinkedList {
  5.     #head =null
  6.     #tail =null
  7.     #count = 0
  8.  
  9.     addFirst(value) {
  10.       const node = new LinkedListNode(value);
  11.       if(!this.#head){
  12.          this.#head=node;
  13.          this.#tail = node;
  14.       } else {
  15.       node.next = this.#head;
  16.       this.#head.prev = node;
  17.       this.#head = node;
  18.       }
  19.       this.#count++;
  20.     }
  21.  
  22.     addLast(value) {
  23.       const node = new LinkedListNode(value);
  24.       if(!this.#head) {
  25.          this.#head = node;
  26.       } else {
  27.          this.#tail.next = node;
  28.          node.prev = this.#tail;
  29.       }
  30.       this.#tail = node;
  31.       this.#count++;
  32.     }
  33.  
  34.     removeFirst(){
  35.       if(!this.#head){
  36.          throw new Error('No head');
  37.       }
  38.       if(this.count === 1){
  39.          const val = this.#head.value;
  40.          this.#head = null;
  41.          this.#tail = null;
  42.          this.#count--;
  43.          return val;
  44.       } else {
  45.       const val = this.#head.value;
  46.       this.#head = this.#head.next;
  47.       this.#head.prev = null;
  48.       this.#count--;
  49.       return val;
  50.       }
  51.     }
  52.  
  53.     removeLast() {
  54.       if(!this.#tail){
  55.          throw new Error('No tail');
  56.       }
  57.       if(this.count === 1){
  58.          const val = this.#tail.value;
  59.          this.#head = null;
  60.          this.#tail = null;
  61.          this.#count--;
  62.          return val;
  63.       } else {
  64.          const val = this.#tail.value;
  65.          this.#tail.prev.next = null;
  66.          this.#count--;
  67.          return val;
  68.       }
  69.     }
  70.    
  71.     values() {
  72.       let result = [];
  73.       while(this.#head){
  74.          result.push(this.#head.value);
  75.          this.#head = this.#head.next;
  76.       }
  77.       return result;
  78.     }
  79.  
  80.     insertAfter(node, val) {
  81.       const newNode = new LinkedListNode(val);
  82.       if(node === this.#tail){
  83.          this.#tail.next = newNode;
  84.          newNode.prev = this.#tail;
  85.          this.#tail = newNode;
  86.          this.#count++;
  87.       } else {
  88.       node.next.prev = newNode;
  89.       newNode.prev = node;
  90.       newNode.next = node.next;
  91.       node.next = newNode;
  92.       this.#count++;
  93.       }
  94.     }
  95.     insertBefore(node,val){
  96.       const newNode = new LinkedListNode(val);
  97.       if(node === this.#head){
  98.          newNode.next = this.#head;
  99.          this.#head.prev = newNode;
  100.          this.#head = newNode;
  101.          this.#count++;
  102.       } else {
  103.       node.prev.next = newNode;
  104.       newNode.next = node;
  105.       node.prev = newNode;
  106.       this.#count++;
  107.       }
  108.  
  109.     }
  110.     find(val) {
  111.       let ref = this.#head;
  112.       while(ref){
  113.          if(ref.value === val) {
  114.             return ref;
  115.          }
  116.          ref = ref.next;
  117.       }
  118.       return null;
  119.  
  120.     }
  121.  
  122.     valuesRev () { //gives the values in reverse order to check the validity of refrences.
  123.       let result = [];
  124.       while(this.#tail){
  125.          result.push(this.#tail.value);
  126.          this.#tail = this.#tail.prev;
  127.       }
  128.       return result;
  129.     }
  130.  
  131.     get count() {
  132.       return this.#count;
  133.     }
  134.     get head() {
  135.       return this.#head;
  136.     }
  137.     get tail() {
  138.       return this.#tail;
  139.     }
  140. }
  141.  
  142.  
Advertisement
Add Comment
Please, Sign In to add comment