用于存放学校的作业便于复习。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.6 KiB

  1. const readline = require("readline");
  2. const rl = readline.createInterface(process.stdin, process.stdout);
  3. class Node {
  4. previous;
  5. value;
  6. next;
  7. }
  8. class LinkList {
  9. head;
  10. length;
  11. constructor(length) {
  12. // 还是需要头结点,此时head表示其中第0个结点,length必须大于0
  13. this.length = length;
  14. this.head = new Node();
  15. // this.head.value = 1;
  16. let current, previous = this.head;
  17. for (let i = 1; i <= this.length; i++) {
  18. current = new Node();
  19. current.value = i;
  20. previous.next = current;
  21. current.previous = previous;
  22. previous = current;
  23. }
  24. current.next = this.head.next;
  25. this.head.next.previous = current;
  26. // this.head.next = this.current; // 不能多重赋值,赋值语句会返回undefined
  27. this.head.previous = current;
  28. }
  29. remove(node) {
  30. node.previous.next = node.next;
  31. node.next.previous = node.previous;
  32. // delete node; // JavaScript里应该无法直接删除
  33. this.length--;
  34. return true;
  35. }
  36. // 找到从当前结点开始第n个结点(可以为负数),当前结点是第0个
  37. // 并且再向后返回一个结点
  38. get_n_after_node(node, n) {
  39. let property_name = "next";
  40. if (n < 0) {
  41. property_name = "previous";
  42. n = -n;
  43. }
  44. if (this.length < 3) return false;
  45. for (let i = 0; i < n; i++) { // 向后走n个,例如从0开始,到3结束
  46. node = node[property_name];
  47. }
  48. return node;
  49. }
  50. }
  51. function main(people_num, num1, num2) {
  52. let link_list = new LinkList(people_num);
  53. let current = link_list.head;
  54. let temp;
  55. while (true) {
  56. temp = link_list.get_n_after_node(current, num1);
  57. if (!temp) break;
  58. current = temp;
  59. link_list.remove(current);
  60. temp = link_list.get_n_after_node(current, num2);
  61. if (!temp) break;
  62. current = temp;
  63. link_list.remove(current);
  64. }
  65. // 此时current是最后一个被删掉的结点,它的next和previous都是存在的结点
  66. current = current.next;
  67. link_list.head.next = current;
  68. for (let i = 0; i < link_list.length; i++) {
  69. console.log(current.value);
  70. current = current.next;
  71. }
  72. }
  73. function input(prompt) {
  74. return new Promise((resolve) => {
  75. rl.question(prompt, (answer) => {
  76. resolve(answer);
  77. });
  78. });
  79. }
  80. function visualize(dryrun, link_list) {
  81. if (dryrun) {
  82. return;
  83. }
  84. let result = {
  85. "kind": { "graph": true },
  86. "nodes": [
  87. ],
  88. "edges": [
  89. ]
  90. };
  91. let current = link_list.head.next;
  92. for (let i = 0; i < link_list.length; i++) {
  93. result.nodes.push({
  94. "id": String(i),
  95. "label": String(current.value)
  96. });
  97. current = current.next;
  98. result.edges.push({
  99. "from": String(i),
  100. "to": String(i + 1)
  101. });
  102. }
  103. result.edges[result.edges.length - 1].to = "0";
  104. return result;
  105. }
  106. async function main_() {
  107. visualize(true); // 不加这行可能它转化成Promise的时候不会放到闭包作用域里。
  108. people_num = parseInt(await input("输入人数"));
  109. num1 = parseInt(await input("输入第一个数字"));
  110. num2 = parseInt(await input("输入第二个数字"));
  111. main(people_num, num1, num2);
  112. rl.close();
  113. }
  114. main_()
  115. // 输入人数10000
  116. // 输入第一个数字123
  117. // 输入第二个数字-654
  118. // 1299
  119. // 4114
  120. // 输入人数99999
  121. // 输入第一个数字-848
  122. // 输入第二个数字-351
  123. // 80178
  124. // 23680