Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. class LinkedListNode {
  2. constructor(value, next) {
  3. this.value = value;
  4. this.next = next || null;
  5. }
  6. }
  7.  
  8. class LinkedList {
  9. constructor(value) {
  10. this.size = 0;
  11. this.head = null;
  12. this.tail = null;
  13.  
  14. if (value) {
  15. if (Array.isArray(value)) return this.fromArray(value);
  16. return new TypeError(value + ' is not iterable');
  17. };
  18.  
  19. return this;
  20. }
  21.  
  22. prepend(value) {
  23. this.size += 1;
  24.  
  25. const newNode = new LinkedListNode(value, this.head);
  26.  
  27. this.head = newNode;
  28. if (!this.tail) this.tail = newNode;
  29. return this;
  30. }
  31.  
  32. append(value) {
  33. this.size += 1;
  34.  
  35. const newNode = new LinkedListNode(value);
  36.  
  37. if (!this.head) {
  38. this.head = newNode;
  39. this.tail = newNode;
  40. return this;
  41. }
  42.  
  43. this.tail.next = newNode;
  44. this.tail = newNode;
  45. return this;
  46. }
  47.  
  48. fromArray(values) {
  49. values.forEach(value => this.append(value));
  50. return this;
  51. }
  52.  
  53. toArray(useNodes = false) {
  54. const nodes = [];
  55. let currentNode = this.head;
  56. while (currentNode) {
  57. nodes.push(useNodes ? currentNode : currentNode.value);
  58. currentNode = currentNode.next;
  59. };
  60. return nodes;
  61. }
  62.  
  63.  
  64. includes(value) {
  65. if (!this.head) return false;
  66.  
  67. let isNode = value.constructor.name === 'LinkedListNode';
  68. if (isNode) value = value.value;
  69.  
  70. let currentNode = this.head;
  71.  
  72. while (currentNode) {
  73. if (value !== undefined && value === currentNode.value) {
  74. return true;
  75. };
  76. currentNode = currentNode.next;
  77. };
  78.  
  79. return false;
  80. }
  81.  
  82. find(callback) {
  83. if (Object.prototype.toString.call(callback) !== '[object Function]') {
  84. return new TypeError(callback + ' is not a function');
  85. };
  86.  
  87. if (!this.head) return undefined;
  88.  
  89. let currentNode = this.head;
  90.  
  91. while (currentNode) {
  92. if (callback && callback(currentNode.value)) {
  93. return currentNode;
  94. };
  95. currentNode = currentNode.next;
  96. };
  97.  
  98. return undefined;
  99. }
  100.  
  101. delete(value, deleteOne = false) {
  102. if (!this.head) return false;
  103.  
  104. let deletedNode = null;
  105.  
  106. // If the head needs to be deleted
  107. while (this.head && this.head.value === value) {
  108. this.size -= 1;
  109. deletedNode = this.head;
  110. this.head = this.head.next;
  111. if (deleteOne) return true;
  112. };
  113.  
  114. let currentNode = this.head;
  115.  
  116. // If any node except the head or tail needs to be deleted
  117. if (currentNode !== null) {
  118. while (currentNode.next) {
  119. if (currentNode.next.value === value) {
  120. this.size -= 1;
  121. deletedNode = currentNode.next;
  122. currentNode.next = currentNode.next.next;
  123. if (deleteOne) return true;
  124. } else {
  125. currentNode = currentNode.next;
  126. };
  127. };
  128. };
  129.  
  130. // If the tail needs to be deleted
  131. if (this.tail.value === value) {
  132. this.tail = currentNode;
  133. };
  134.  
  135. if (deletedNode === null) {
  136. return false;
  137. } else {
  138. return true;
  139. };
  140. }
  141.  
  142. deleteHead() {
  143. if (!this.head) return null;
  144.  
  145. this.size -= 1;
  146.  
  147. const deletedHead = this.head;
  148.  
  149. if (this.head.next) {
  150. this.head = this.head.next;
  151. } else {
  152. this.head = null;
  153. this.tail = null;
  154. }
  155.  
  156. return deletedHead;
  157. }
  158.  
  159. deleteTail() {
  160. if (this.size === 0) return false;
  161.  
  162. if (this.size === 1) {
  163. if (this.head === null) {
  164. return false;
  165. } else {
  166. this.head = null;
  167. this.tail = null;
  168. this.size -= 1;
  169. return true;
  170. }
  171. }
  172.  
  173. const deletedTail = this.tail;
  174.  
  175. let currentNode = this.head;
  176. while (currentNode.next) {
  177. if (!currentNode.next.next) {
  178. this.size -= 1;
  179. currentNode.next = null;
  180. } else {
  181. currentNode = currentNode.next;
  182. }
  183. }
  184.  
  185. this.tail = currentNode;
  186.  
  187. return deletedTail;
  188. }
  189.  
  190. }
  191.  
  192. module.exports = LinkedList
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement