Guest User

Untitled

a guest
May 24th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. class DListNode {
  2. constructor(val, next = null, prev = null) {
  3. this.val = val
  4. this.next = next
  5. this.prev = prev
  6. }
  7. }
  8.  
  9. /*
  10. Interface:
  11. insertAtHead(val)
  12. insertAtTail(val)
  13.  
  14. removeAtHead(val)
  15. removeAtTail(val)
  16.  
  17. insertAtIndex(index, val)
  18. removeAtIndex(index)
  19.  
  20. insertAfterKey(key, val)
  21. insertBeforeKey(key, val)
  22.  
  23. getIndexByValue(val)
  24. getValueAtIndex(index)
  25. getNodeAtIndex(index)
  26.  
  27. doForEach(fn)
  28. stringify()
  29. */
  30.  
  31. class DLinkedList {
  32. constructor(head = null, tail = null) {
  33. this.head = head
  34. this.tail = tail
  35. }
  36.  
  37. insertAtHead(val) {
  38. if (!this.head) {
  39. this.head = new DListNode(val, null, null)
  40. this.tail = this.head
  41. } else {
  42. this.head = new DListNode(val, this.head, null)
  43. this.head.next.prev = this.head
  44. }
  45. }
  46.  
  47. insertAtTail(val) {
  48. if (!this.head) {
  49. this.insertAtHead(val)
  50. } else {
  51. this.tail.next = new DListNode(val, null, this.tail)
  52. this.tail = this.tail.next
  53. }
  54. }
  55.  
  56. removeAtHead() {
  57. if (this.head && this.head.next) {
  58. this.head = this.head.next
  59. this.head.prev = null
  60. } else {
  61. this.head = null
  62. this.tail = null
  63. }
  64. }
  65.  
  66. removeAtTail() {
  67. if (this.tail && this.tail.prev) {
  68. this.tail = this.tail.prev
  69. this.tail.next = null
  70. } else {
  71. this.head = null
  72. this.tail = null
  73. }
  74. }
  75.  
  76. insertAtIndex(index, val) {
  77. if (index === 0) {
  78. return this.insertAtHead(val)
  79. }
  80.  
  81. let node = this.getNodeAtIndex(index - 1)
  82. if (!node) {
  83. return
  84. }
  85.  
  86. if (node.next) {
  87. let tmp = new DListNode(val, node.next, node)
  88. node.next.prev = tmp
  89. node.next = tmp
  90. } else {
  91. let tmp = new DListNode(val, null, node)
  92. node.next = tmp
  93. this.tail = tmp
  94. }
  95. }
  96.  
  97. removeAtIndex(index) {
  98. let curr = this.getNodeAtIndex(index)
  99. if (!curr) {
  100. return
  101. }
  102.  
  103. if (index === 0) {
  104. return this.removeAtHead()
  105. }
  106.  
  107. if (curr.next) {
  108. curr.next.prev = curr.prev
  109. curr.prev.next = curr.next
  110. } else {
  111. curr.prev.next = null
  112. this.tail = curr.prev
  113. }
  114. }
  115.  
  116. insertAfterKey(key, value) {
  117. let index = this.getIndexByValue(key)
  118. if (index < 0) {
  119. return
  120. }
  121.  
  122. this.insertAtIndex(index + 1, value)
  123. }
  124.  
  125. insertBeforeKey(key, value) {
  126. let index = this.getIndexByValue(key)
  127. if (index < 0) {
  128. return
  129. }
  130.  
  131. this.insertAtIndex(index, value)
  132. }
  133.  
  134. getNodeAtIndex(index) {
  135. let curr = this.head
  136.  
  137. while (curr && index--) {
  138. curr = curr.next
  139. }
  140.  
  141. return curr
  142. }
  143.  
  144. getValueAtIndex(index) {
  145. let curr = this.head
  146.  
  147. while (curr && index--) {
  148. curr = curr.next
  149. }
  150.  
  151. if (curr) {
  152. return curr.data
  153. } else {
  154. return null
  155. }
  156. }
  157.  
  158. getIndexByValue(value) {
  159. let curr = this.head
  160. let index = 0
  161.  
  162. while (curr) {
  163. if (curr.val === value) {
  164. return index
  165. }
  166.  
  167. curr = curr.next
  168. index++
  169. }
  170.  
  171. return -1
  172. }
  173.  
  174. doForEach(fn) {
  175. let curr = this.head
  176.  
  177. while (curr) {
  178. fn(curr)
  179. curr = curr.next
  180. }
  181. }
  182.  
  183. stringify() {
  184. let out = []
  185. let curr = this.head
  186.  
  187. while (curr) {
  188. out.push(curr.val)
  189. curr = curr.next
  190. }
  191.  
  192. return out.join(' -> ')
  193. }
  194. }
  195.  
  196. let list = new DLinkedList()
  197.  
  198. list.insertAtTail(5)
  199. list.insertAtTail(6)
  200. list.insertAtHead(4)
  201.  
  202. list.removeAtIndex(0)
  203. list.insertBeforeKey(5, 9)
  204.  
  205. let arr = list.stringify()
  206. console.log(arr)
Add Comment
Please, Sign In to add comment