momo3141

3та

Mar 9th, 2023
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let [N, K] = gets().split(' ')
  2. let students = gets().split(' ')
  3. class LinkedListNode {
  4.     constructor(value) {
  5.         this.value = value;
  6.         this.next = null;
  7.         this.prev = null;
  8.     }
  9. }
  10.  
  11. class DoublyLinkedList {
  12.     constructor() {
  13.         this.head = null;
  14.         this.tail = null;
  15.         this.count = 0;
  16.        
  17.     }
  18.     addLast(value) {
  19.         const node = new LinkedListNode(value);
  20.         if(!this.head) {
  21.            this.head = node;
  22.         } else {
  23.            this.tail.next = node;
  24.            node.prev = this.tail;
  25.         }
  26.         this.tail = node;
  27.        
  28.         this.count++;
  29.       }
  30.  
  31.    insertBefore(node, value) {
  32.         if (!this.head) {
  33.             throw new Error('Empty list');
  34.         }
  35.         const newNode = new LinkedListNode(value);
  36.         newNode.prev = node.prev;
  37.         node.prev = newNode;
  38.         newNode.next = node;
  39.         if (newNode.prev !== null) {
  40.           newNode.prev.next = newNode;
  41.         } else {
  42.          this.head = newNode;
  43.         }
  44.         this.count++;
  45.     }
  46.  
  47.    find(val) {
  48.         let ref = this.head;
  49.         while(ref){
  50.            if(ref.value === val) {
  51.               return ref;
  52.            }
  53.            ref = ref.next;
  54.         }
  55.         return null;
  56.       }
  57.  
  58.    moveBefore(val1, val2){
  59.         let ref = this.head;
  60.         let ref1 = null
  61.         let ref2 = null
  62.         while(ref){
  63.             if(ref.value === val1){
  64.                 ref1 = ref
  65.             }
  66.             if(ref.value === val2){
  67.                 ref2 = ref
  68.             }
  69.             ref = ref.next;
  70.         }
  71.         this.removeNode(ref1);
  72.         this.insertBefore(ref2,ref1.value);
  73.  
  74.       }
  75.  
  76.    removeNode(node){
  77.         if(node === this.tail){
  78.            this.tail.prev.next = null
  79.            this.tail = this.tail.prev
  80.            
  81.         } else if(node === this.head) {
  82.            this.head.next.prev = null;
  83.            this.head = this.head.next;
  84.            
  85.         } else {
  86.         node.prev.next = node.next;
  87.         node.next.prev = node.prev;
  88.         }
  89.         this.count--;
  90.       }
  91.    
  92.    values() {
  93.         let result = [];
  94.         if(this.count===0){
  95.            return result;
  96.         }
  97.         let ref = this.head.next.prev;// remembers the head in order not to modify the list;
  98.         while(this.head){
  99.            result.push(this.head.value);
  100.            this.head = this.head.next;
  101.         }
  102.         this.head = ref;//resets the head reference
  103.         return result;
  104.       }
  105.     }
  106.  
  107. const list = new DoublyLinkedList();
  108.     for (let i = 0; i <= N; i++) {
  109.         list.addLast(students[i]);
  110.     }
  111. const dict = new Map()
  112. for (const stud of students) {
  113.    dict.set(stud, list.find(stud))
  114. }
  115. for (let i = 1; i <= K; i++) {
  116.         let [leftName, rightName] = gets().split(' ')
  117.        
  118.         let node1 = dict.get(leftName);
  119.  
  120.         list.removeNode(node1);
  121.         dict.delete(leftName);
  122.         //update the dict
  123.         let node2 = dict.get(rightName);
  124.         list.insertBefore(node2,leftName);
  125.         if(node2 === list.head){
  126.          dict.set(leftName,listHead)
  127.         } else {
  128.         dict.set(leftName,node2.prev)
  129.         }  
  130.     }
  131. console.log(list.values().join(' '));
Advertisement
Add Comment
Please, Sign In to add comment