Vassil_Iliev

doctor's office

Jun 15th, 2022
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Node {
  2.  
  3.     constructor(val) {
  4.         this.next = null;
  5.         this.prev = null;
  6.         this.val = val;
  7.     }
  8. }
  9.  
  10. class doctorsOffice {
  11.  
  12.     constructor() {
  13.         this.head = null;
  14.         this.tail = null;
  15.         this.count = 0;
  16.         this.occurMap = new Map()
  17.     }
  18.  
  19.     Append(name) {
  20.         if (this.head === null) {
  21.             this.head = new Node(name);
  22.             this.tail = this.head;
  23.             this.count++
  24.             this.occurMap.set(name, this.occurMap.has(name) ? this.occurMap.get(name) + 1 : 1);
  25.         } else {
  26.             const newNode = new Node(name);
  27.             newNode.prev = this.tail;
  28.             this.tail.next = newNode;
  29.             this.tail = this.tail.next;
  30.             this.count++
  31.             this.occurMap.set(name, this.occurMap.has(name) ? this.occurMap.get(name) + 1 : 1);
  32.         }
  33.         return 'OK';
  34.     }
  35.  
  36.     Insert(position, name) {
  37.         if (+position > this.count) return 'Error';
  38.  
  39.         if (position == 0 && this.head) {
  40.             const newNode = new Node(name);
  41.             this.head.prev = newNode;
  42.             newNode.next = this.head;
  43.             this.head = this.head.prev;
  44.             this.count++
  45.             this.occurMap.set(name, this.occurMap.has(name) ? this.occurMap.get(name) + 1 : 1);
  46.             return 'OK';
  47.         } else if (position == 0) {
  48.             const newNode = new Node(name);
  49.             this.head = newNode;
  50.             this.tail = newNode;
  51.             this.count++
  52.             this.occurMap.set(name, this.occurMap.has(name) ? this.occurMap.get(name) + 1 : 1);
  53.             return 'OK'
  54.         } else if (position == this.count) {
  55.             const newNode = new Node(name);
  56.             newNode.prev = this.tail;
  57.             this.tail.next = newNode;
  58.             this.tail = this.tail.next;
  59.             this.count++
  60.             this.occurMap.set(name, this.occurMap.has(name) ? this.occurMap.get(name) + 1 : 1);
  61.             return 'OK';
  62.         } else {
  63.             let temp = this.head;
  64.             for (let i = 0; i < position - 1; i++) {
  65.                 temp = temp.next;
  66.             }
  67.  
  68.             const newNode = new Node(name);
  69.             newNode.prev = temp;
  70.             newNode.next = temp.next;
  71.             temp.next.prev = newNode;
  72.             temp.next = newNode;
  73.             this.occurMap.set(name, this.occurMap.has(name) ? this.occurMap.get(name) + 1 : 1);
  74.             this.count++
  75.             return 'OK'
  76.         }
  77.     }
  78.  
  79.     Find(name) {
  80.         return this.occurMap.get(name) || 0
  81.     }
  82.  
  83.     Examine(count) {
  84.         if (this.count < count) return 'Error';
  85.         let str = ''
  86.         while (count) {
  87.             if (this.head != this.tail) {
  88.                 this.occurMap.set(this.head.val, this.occurMap.get(this.head.val) - 1)
  89.                 str += this.head.val + ' ';
  90.                 this.head = this.head.next;
  91.  
  92.                 count--
  93.             } else {
  94.                 this.occurMap.set(this.head.val, this.occurMap.get(this.head.val) - 1)
  95.                 str += this.head.val + ' ';
  96.                 this.head = null;
  97.                 this.tail = null;
  98.                 count--
  99.             }
  100.  
  101.             this.count--
  102.         }
  103.  
  104.  
  105.         return str.trim();
  106.     }
  107. }
  108.  
  109. const test = new doctorsOffice();
  110.  
  111. while (true) {
  112.     const data = gets().split(' ')
  113.     const command = data[0];
  114.     const par1 = data[1];
  115.     const par2 = data[2];
  116.  
  117.     if (command === 'End') break;
  118.  
  119.     print(test[command](par1, par2))
  120. }
Advertisement
Add Comment
Please, Sign In to add comment