Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. class DoublyLinkedList {
  2. static from(array) {
  3. const dll = new DoublyLinkedList()
  4. for (const val of array) {
  5. dll.append(val)
  6. }
  7. return dll
  8. }
  9.  
  10. constructor() {
  11. this.head = null
  12. this.tail = null
  13. this.size = 0
  14. }
  15.  
  16. append(value) {
  17. this.size++
  18.  
  19. if (this.tail == null) {
  20. this.head = this.tail = { value, prev: null, next: null }
  21. } else {
  22. const oldTail = this.tail
  23. const node = { value, prev: oldTail, next: null }
  24. oldTail.next = node
  25. this.tail = node
  26. }
  27.  
  28. return this
  29. }
  30.  
  31. prepend(value) {
  32. this.size++
  33.  
  34. if (this.tail == null) {
  35. this.head = this.tail = { value, prev: null, next: null }
  36. } else {
  37. const oldHead = this.head
  38. const node = { value, prev: null, next: oldHead }
  39. oldHead.prev = node
  40. this.head = node
  41. }
  42.  
  43. return this
  44. }
  45.  
  46. insert(at, value) {
  47. if (at === 0) {
  48. return this.prepend(value)
  49. }
  50.  
  51. if (at === this.size) {
  52. return this.append(value)
  53. }
  54.  
  55. if (at > this.size) {
  56. throw new TypeError('insert index out of bounds')
  57. }
  58.  
  59. this.walk((node, i) => {
  60. if (i === at - 1) {
  61. const oldNext = node.next
  62. node.next = oldNext.prev = { value, next: oldNext, prev: node }
  63. }
  64. })
  65.  
  66. return this
  67. }
  68.  
  69. removeAt(at) {
  70. this.walk((node, i) => {
  71. if (i === at) {
  72. node.prev.next = node.next
  73. node.next.prev = node.prev
  74. }
  75. })
  76.  
  77. return this
  78. }
  79.  
  80. reverse() {
  81. let current = this.tail
  82. while (current != null) {
  83. [current.next, current.prev] = [current.prev, current.next]
  84. current = current.next
  85. }
  86.  
  87. [this.head, this.tail] = [this.tail, this.head]
  88.  
  89. return this
  90. }
  91.  
  92. equals(other) {
  93. if (this.size !== other.size) { // assuming we can rely on this...
  94. return false
  95. }
  96.  
  97. if (this === other) {
  98. return true
  99. }
  100.  
  101. let thisCurrent = this.head,
  102. otherCurrent = other.head
  103. while (thisCurrent != null && otherCurrent != null) {
  104. if (thisCurrent.value !== otherCurrent.value) {
  105. return false
  106. }
  107.  
  108. thisCurrent = thisCurrent.next
  109. otherCurrent = otherCurrent.next
  110. }
  111.  
  112. return true
  113. }
  114.  
  115. walk(callback) {
  116. let current = this.head,
  117. i = 0
  118. while (current != null) {
  119. callback(current, i++)
  120. current = current.next
  121. }
  122. return this
  123. }
  124.  
  125. prettyPrint() {
  126. let output = ''
  127. this.walk(node => {
  128. if (node.next != null) {
  129. output += `${node.value} <-> `
  130. } else {
  131. output += `${node.value}`
  132. }
  133. })
  134. return output
  135. }
  136. }
  137.  
  138. console.log(
  139. DoublyLinkedList
  140. .from([1, 2, 3, 4])
  141. .equals(
  142. DoublyLinkedList
  143. .from([4, 3, 2, 1])
  144. .reverse()
  145. )
  146. )
  147.  
  148. console.log(
  149. DoublyLinkedList
  150. .from(['๐ŸŒ‘', '๐ŸŒ’', '๐ŸŒ“', '๐ŸŒ”', '๐ŸŒ•'])
  151. .reverse()
  152. .prettyPrint()
  153. )
  154.  
  155. console.log(
  156. DoublyLinkedList
  157. .from(['๐Ÿ˜„', '๐Ÿ˜„', '๐Ÿ˜ญ', '๐Ÿ˜„', '๐Ÿ˜„', '๐Ÿ˜„'])
  158. .removeAt(2)
  159. .prettyPrint()
  160. )
  161.  
  162. for (let i = 0; i < 5; i++) {
  163. let dll = DoublyLinkedList
  164. .from(['๐Ÿฅถ', '๐Ÿฅถ', '๐Ÿฅถ', '๐Ÿฅถ'])
  165. .insert(i, '๐Ÿฅต')
  166. console.log(dll.prettyPrint())
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement