momo3141

3-ta

Mar 28th, 2023
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let input = [
  2.    'Append Steven',
  3.    'Examine 1',
  4.    'Find Ina',
  5.    'Append Niki',
  6.    'Insert 0 Peter',
  7.    'Append Nadya',
  8.    'Insert 3 Grigor',
  9.    'Examine 5',
  10.    'Append Asya',
  11.    'Insert 4 Steven',
  12.    'Append Steven',
  13.    'Find Asya',
  14.    'Find Steven',
  15.    'Examine 3',
  16.    'Find Peter',
  17.    'Examine 4',
  18.    'Find Steven',
  19.    'Insert 1 Ina',
  20.    'End'
  21.    
  22. ];
  23. let print = this.print || console.log;
  24. let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  25. // Code
  26.  
  27. let myResult = []
  28.  
  29. class LinkedListNode {
  30.    constructor() {
  31.        this.value = null;
  32.        this.next = null;
  33.        this.prev = null;
  34.    }
  35. }
  36.  
  37.  class WaitingList {
  38.     head =null
  39.     tail =null
  40.     count = 0
  41.     constructor(){
  42.       this.nameMap = new Map();
  43.     }
  44.  
  45.    
  46.  
  47.     addFirst(value) {
  48.       const node = new LinkedListNode();
  49.       node.value = value
  50.       if(!this.head){
  51.          this.head=node;
  52.          this.tail = node;
  53.       } else {
  54.       node.next = this.head;
  55.       this.head.prev = node;
  56.       this.head = node;
  57.       }
  58.       this.count++;
  59.       const count = this.nameMap.get(value) || 0;
  60.       this.nameMap.set(value, count+1);
  61.       myResult.push('OK');
  62.  
  63.     }
  64.  
  65.     append(value) {
  66.       const node = new LinkedListNode();
  67.       node.value = value
  68.       if(!this.head) {
  69.          this.head = node;
  70.       } else {
  71.          this.tail.next = node;
  72.          node.prev = this.tail;
  73.       }
  74.       this.tail = node;
  75.       this.count++;
  76.       const counter = this.nameMap.get(value) || 0;
  77.       this.nameMap.set(value, counter+1);
  78.       myResult.push('OK');
  79.     }
  80.  
  81.     removeFirst(){
  82.      
  83.       if(this.count === 1){
  84.          const val = this.head.value;
  85.          this.head = null;
  86.          this.tail = null;
  87.          this.count--;
  88.          return val;
  89.       } else {
  90.       const val = this.head.value;
  91.       this.head = this.head.next;
  92.       this.head.prev = null;
  93.       this.count--;
  94.       return val;
  95.       }
  96.     }
  97.  
  98.     removeLast() {
  99.      
  100.       if(this.count === 1){
  101.          const val = this.tail.value;
  102.          this.head = null;
  103.          this.tail = null;
  104.          this.count--;
  105.          return val;
  106.       } else {
  107.          const val = this.tail.value;
  108.          this.tail.prev.next = null;
  109.          this.count--;
  110.          return val;
  111.       }
  112.     }
  113.    
  114.    
  115.     insertBefore(node,val){
  116.       const newNode = new LinkedListNode();
  117.       newNode.value = val
  118.       if(node === this.head){
  119.          newNode.next = this.head;
  120.          this.head.prev = newNode;
  121.          this.head = newNode;
  122.          this.count++;
  123.       } else {
  124.       node.prev.next = newNode;
  125.       newNode.next = node;
  126.       node.prev = newNode;
  127.       this.count++;
  128.       }
  129.       const counter = this.nameMap.get(val) || 0;
  130.       this.nameMap.set(val, counter+1);
  131.       myResult.push('OK')
  132.  
  133.     }
  134.  
  135.  
  136.     // new methods
  137.  
  138.     insert(pos, name) {
  139.       if(pos < 0 || pos  > this.count) {
  140.          myResult.push('Error');
  141.       } else {
  142.          // const counter = this.nameMap.get(name) || 0;
  143.          // this.nameMap.set(name, counter+1);
  144.          if(Number(pos) === 0){
  145.             this.addFirst(name)
  146.          } else if (Number(pos) === this.count) {
  147.             this.append(name)
  148.          } else {
  149.             let ref = this.head
  150.             for (let i = 1; i<= pos; i++){
  151.                ref = ref.next
  152.             }
  153.             this.insertBefore(ref, name)
  154.          }
  155.    
  156.       }
  157.    }
  158.  
  159.       find(name){
  160.          let a = this.nameMap.get(name) || 0;
  161.          myResult.push(a)
  162.       }
  163.  
  164.       examine(val){
  165.          if(val > this.count){
  166.            myResult.push('Error')
  167.          } else {
  168.          let outPut = [];
  169.          for (let i = 1; i <= val;i++){
  170.             let patient = this.removeFirst()
  171.             let counter = this.nameMap.get(patient);
  172.             if (counter === 1){
  173.                this.nameMap.delete(patient)
  174.             } else {
  175.                this.nameMap.set(patient, counter-1)
  176.             }
  177.             outPut.push(patient)
  178.          }
  179.          myResult.push(outPut.join(' '));
  180.          }
  181.       }
  182.    
  183. }
  184.  
  185.  
  186.  
  187.  
  188. const list = new WaitingList();
  189. outer:
  190. while (true){
  191.    command = gets().split(' ')
  192.    let methodName = command[0].toLowerCase()
  193.    switch(methodName) {
  194.       case 'end':
  195.          break outer;
  196.       case 'append':
  197.          list.append(command[1]);
  198.          break;
  199.       case 'insert':
  200.          list.insert(command[1], command[2]);
  201.          break;
  202.       case 'find':
  203.          list.find(command[1]);
  204.          break;
  205.       case 'examine':
  206.          list.examine(command[1]);
  207.    }
  208.  
  209. }
  210. for (const el of myResult){
  211.    console.log(el);
  212. }
Advertisement
Add Comment
Please, Sign In to add comment