Nina-S

Untitled

Mar 10th, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. let input = [
  2. "5 3",
  3. "Gosho Tosho Penka Miro Stanka",
  4. "Miro Gosho",
  5. "Gosho Stanka",
  6. "Stanka Miro",
  7. ];
  8.  
  9. let print = this.print || console.log;
  10.  
  11. let gets =
  12. this.gets ||
  13. (
  14. (arr, index) => () =>
  15. arr[index++]
  16. )(input, 0);
  17.  
  18. //LinkedListNode
  19. class LinkedListNode {
  20. constructor(value) {
  21. this.value = value;
  22. this.next = null;
  23. this.prev = null;
  24. }
  25. }
  26. //DoublyLinkedList
  27.  
  28. class DoublyLinkedList {
  29. constructor() {
  30. this.head = null;
  31. this.tail = null;
  32. this.count = 0;
  33. this.map = new Map();
  34. }
  35.  
  36. moveBefore(value1, value2) {
  37. let valueToMove = this.find(value1); //miro
  38. let staticValue = this.find(value2); //gosho
  39.  
  40. this.remove(valueToMove);
  41. this.insertBefore(staticValue, value1);
  42. }
  43.  
  44. remove(node) {
  45. this.map.delete(node.value);
  46.  
  47. let firstNodePrevious = node.prev; //penka
  48. let firstNodeNext = node.next; //stanka
  49.  
  50. if (firstNodePrevious === null && firstNodeNext === null) {
  51. //do not handle
  52. } else if (firstNodePrevious === null) {
  53. firstNodeNext.prev = null;
  54. this.head = firstNodeNext;
  55. } else if (firstNodeNext === null) {
  56. firstNodePrevious.next = null;
  57. this.tail = firstNodePrevious;
  58. } else {
  59. firstNodePrevious.next = firstNodeNext; //penka.next = stanka
  60. firstNodeNext.prev = firstNodePrevious; //stanka.prev = penka
  61. }
  62. }
  63.  
  64. addLast(value) {
  65. let node = new LinkedListNode(value);
  66.  
  67. if (this.count === 0) {
  68. this.map.set(value, node);
  69. this.head = node;
  70. this.tail = node;
  71. } else {
  72. this.map.set(value, node);
  73. this.tail.next = node;
  74. node.prev = this.tail;
  75. this.tail = node;
  76. }
  77. this.count++;
  78. }
  79.  
  80. insertBefore(prevNode, value) {
  81. //insert an element with the given value before the given node
  82. let newNode = new LinkedListNode(value);
  83. this.count++;
  84.  
  85. this.map.set(value, newNode);
  86.  
  87. let tempPrevNode = prevNode;
  88. let previousNode = tempPrevNode.prev;
  89.  
  90. if (previousNode !== null) {
  91. newNode.next = tempPrevNode;
  92. tempPrevNode.prev = newNode;
  93. previousNode.next = newNode;
  94. newNode.prev = previousNode;
  95. } else {
  96. newNode.next = tempPrevNode;
  97. tempPrevNode.prev = newNode;
  98. newNode.prev = null;
  99. this.head = newNode;
  100. }
  101. }
  102.  
  103. find(value) {
  104. // returns the first node that has the given value or undefined if no such value exists
  105. if (this.map.has(value)) {
  106. let node = this.map.get(value);
  107. return node;
  108. } else {
  109. return null;
  110. }
  111. }
  112.  
  113. values() {
  114. let values = [];
  115. let tempHead = this.head;
  116.  
  117. while (tempHead !== null) {
  118. values.push(tempHead.value);
  119. tempHead = tempHead.next;
  120. }
  121. return values;
  122. }
  123. }
  124.  
  125. //Solution
  126.  
  127. let firstLine = gets();
  128. let [numberOfStudents, seat] = firstLine.split(" ");
  129.  
  130. let students = gets().split(" ");
  131. //[Gosho Tosho Penka Miro Stanka]
  132.  
  133. let list = new DoublyLinkedList();
  134.  
  135. //add all students into the map
  136. for (let index = 0; index < students.length; index++) {
  137. const student = students[index];
  138. list.addLast(student);
  139. }
  140.  
  141. //move two of the students
  142. for (let index = 0; index < seat; index++) {
  143. const seatChange = gets().split(" "); //Miro Gosho
  144. list.moveBefore(seatChange[0], seatChange[1]);
  145. }
  146.  
  147. print(list.values().join(" "));
  148.  
Advertisement
Add Comment
Please, Sign In to add comment