Guest User

Untitled

a guest
May 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. // 程序 = 数据结构 + 算法
  2.  
  3. // example:
  4. // a -> b -> c -> null
  5.  
  6. // 单向链表节点
  7. class ListNode {
  8.  
  9. constructor(data) {
  10. // 双向链表
  11. // this.last = null;
  12. this.next = null;
  13. this.data = data;
  14. }
  15.  
  16. // 添加子节点
  17. append(data) {
  18. this.next = new ListNode(data);
  19. return this.next;
  20. }
  21.  
  22. // 是否为尾节点
  23. isTail() {
  24. return !this.next;
  25. }
  26.  
  27. // 获取第n个子节点
  28. nextNth(n) {
  29. if (n < 1 || !this.next) { return null; }
  30. else if (n === 1) { return this.next; }
  31. else { return this.next.nextNth(n - 1); }
  32. }
  33.  
  34. }
  35.  
  36. class LinkedList {
  37.  
  38. constructor(dataSet) {
  39. if (Array.isArray(dataSet)) {
  40. this.buildFromArray(dataSet);
  41. } else {
  42. this.buildFromObject(dataSet);
  43. }
  44. }
  45.  
  46. // 从数组中构建链表
  47. buildFromArray(dataSet) {
  48. if (dataSet.length < 1) { throw 'empty data set'; }
  49.  
  50. this.head = new ListNode(dataSet[0]);
  51. for (let i = 1; i < dataSet.length; i++) {
  52. this.head.append(dataSet[i]);
  53. }
  54. }
  55.  
  56. // 构建单节点链表
  57. buildFromObject(data) {
  58. this.head = new ListNode(data);
  59. }
  60.  
  61. // 判断链表是否有环
  62. hasLoop() {
  63. let fastPointer = this.head;
  64. let slowPointer = this.head;
  65.  
  66. while (fastPointer) {
  67. slowPointer = slowPointer.next;
  68. fastPointer = fastPointer.nextNth(2);
  69. if (slowPointer && fastPointer && slowPointer === fastPointer) { return true; }
  70. }
  71.  
  72. return false;
  73. }
  74.  
  75. }
  76.  
  77. let list = new LinkedList('a');
  78. let a = list.head;
  79. let b = a.append('b');
  80. let c = b.append('c');
  81.  
  82. console.log(`c is tail: ${c.isTail()}`);
  83.  
  84. console.log(`list has no loop yet: ${!list.hasLoop()}`);
  85.  
  86. c.next = a;
  87.  
  88. console.log(`list has loop now: ${list.hasLoop()}`);
  89.  
  90. // 链表实际上有啥用呢?
  91. // 实现更高级的数据结构: 动态数组, 栈, 队列, 哈希表...
  92. // 大致思路
  93.  
  94. // 栈
  95. class Stack {
  96.  
  97. constructor(data) {
  98. this.list = new LinkedList(data);
  99. }
  100.  
  101. push(data) {
  102. let node = new ListNode(data);
  103. node.next = this.list.head;
  104. this.list.head = node;
  105. }
  106.  
  107. // example:
  108. // a -> null
  109. // push b: b -> a -> null
  110.  
  111. pop() {
  112. let node = this.list.head;
  113. if (!node) { return null; }
  114. this.list.head = node.next;
  115. return node.data;
  116. }
  117.  
  118. // example:
  119. // b -> a -> null
  120. // pop b: a -> null
  121. }
  122.  
  123. // 队列
  124.  
  125. class Queue {
  126.  
  127. constructor(data) {
  128. this.list = new LinkedList(data);
  129. this.tail = this.list.head;
  130. }
  131.  
  132. enqueue(data) {
  133. if (this.tail) {
  134. this.tail = this.tail.append(data);
  135. } else {
  136. this.list.head = new ListNode(data);
  137. this.tail = this.list.head;
  138. }
  139. }
  140.  
  141. // example:
  142. // a -> null
  143. // enqueue b: a -> b -> null
  144.  
  145. dequeue() {
  146. let node = this.list.head;
  147. if (node) {
  148. this.list.head = node.next;
  149. if (this.tail === node) { this.tail = null; }
  150. return node.data;
  151. } else {
  152. return null;
  153. }
  154. }
  155.  
  156. // example:
  157. // a -> b -> null
  158. // dequeue a: b -> null
  159. }
Add Comment
Please, Sign In to add comment