015b807b5004451af521ed53070a7a5be4b399cabcb82c22a7838f3d15e5d8a1bb137e8acdef2b2369268512349afb3d7306dbf3ed73a22a62aa8a5a44e028 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. "use strict";
  2. exports.__esModule = true;
  3. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  4. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  5. /**
  6. * Refactored implementation of LinkedList (part of javascript-algorithms project) by Github users:
  7. * mgechev, AndriiHeonia, Microfed and Jakeh (part of javascript-algorithms project - all project contributors
  8. * at repository website)
  9. *
  10. * Link to repository: https://github.com/mgechev/javascript-algorithms
  11. */
  12. /**
  13. * Linked list node.
  14. *
  15. * @class NodeStructure
  16. * @util
  17. */
  18. var NodeStructure = function NodeStructure(data) {
  19. _classCallCheck(this, NodeStructure);
  20. /**
  21. * Data of the node.
  22. * @member {Object}
  23. */
  24. this.data = data;
  25. /**
  26. * Next node.
  27. * @member {NodeStructure}
  28. */
  29. this.next = null;
  30. /**
  31. * Previous node.
  32. * @member {NodeStructure}
  33. */
  34. this.prev = null;
  35. };
  36. /**
  37. * Linked list.
  38. *
  39. * @class LinkedList
  40. * @util
  41. */
  42. var LinkedList = function () {
  43. function LinkedList() {
  44. _classCallCheck(this, LinkedList);
  45. this.first = null;
  46. this.last = null;
  47. }
  48. /**
  49. * Add data to the end of linked list.
  50. *
  51. * @param {Object} data Data which should be added.
  52. */
  53. _createClass(LinkedList, [{
  54. key: "push",
  55. value: function push(data) {
  56. var node = new NodeStructure(data);
  57. if (this.first === null) {
  58. this.first = node;
  59. this.last = node;
  60. } else {
  61. var temp = this.last;
  62. this.last = node;
  63. node.prev = temp;
  64. temp.next = node;
  65. }
  66. }
  67. /**
  68. * Add data to the beginning of linked list.
  69. *
  70. * @param {Object} data Data which should be added.
  71. */
  72. }, {
  73. key: "unshift",
  74. value: function unshift(data) {
  75. var node = new NodeStructure(data);
  76. if (this.first === null) {
  77. this.first = node;
  78. this.last = node;
  79. } else {
  80. var temp = this.first;
  81. this.first = node;
  82. node.next = temp;
  83. temp.prev = node;
  84. }
  85. }
  86. /**
  87. * In order traversal of the linked list.
  88. *
  89. * @param {Function} callback Callback which should be executed on each node.
  90. */
  91. }, {
  92. key: "inorder",
  93. value: function inorder(callback) {
  94. var temp = this.first;
  95. while (temp) {
  96. callback(temp);
  97. temp = temp.next;
  98. }
  99. }
  100. /**
  101. * Remove data from the linked list.
  102. *
  103. * @param {Object} data Data which should be removed.
  104. * @returns {Boolean} Returns true if data has been removed.
  105. */
  106. }, {
  107. key: "remove",
  108. value: function remove(data) {
  109. if (this.first === null) {
  110. return false;
  111. }
  112. var temp = this.first;
  113. var next = void 0;
  114. var prev = void 0;
  115. while (temp) {
  116. if (temp.data === data) {
  117. next = temp.next;
  118. prev = temp.prev;
  119. if (next) {
  120. next.prev = prev;
  121. }
  122. if (prev) {
  123. prev.next = next;
  124. }
  125. if (temp === this.first) {
  126. this.first = next;
  127. }
  128. if (temp === this.last) {
  129. this.last = prev;
  130. }
  131. return true;
  132. }
  133. temp = temp.next;
  134. }
  135. return false;
  136. }
  137. /**
  138. * Check if linked list contains cycle.
  139. *
  140. * @returns {Boolean} Returns true if linked list contains cycle.
  141. */
  142. }, {
  143. key: "hasCycle",
  144. value: function hasCycle() {
  145. var fast = this.first;
  146. var slow = this.first;
  147. while (true) {
  148. if (fast === null) {
  149. return false;
  150. }
  151. fast = fast.next;
  152. if (fast === null) {
  153. return false;
  154. }
  155. fast = fast.next;
  156. slow = slow.next;
  157. if (fast === slow) {
  158. return true;
  159. }
  160. }
  161. }
  162. }, {
  163. key: "pop",
  164. /**
  165. * Return last node from the linked list.
  166. *
  167. * @returns {NodeStructure} Last node.
  168. */
  169. value: function pop() {
  170. if (this.last === null) {
  171. return null;
  172. }
  173. var temp = this.last;
  174. this.last = this.last.prev;
  175. return temp;
  176. }
  177. }, {
  178. key: "shift",
  179. /**
  180. * Return first node from the linked list.
  181. *
  182. * @returns {NodeStructure} First node.
  183. */
  184. value: function shift() {
  185. if (this.first === null) {
  186. return null;
  187. }
  188. var temp = this.first;
  189. this.first = this.first.next;
  190. return temp;
  191. }
  192. }, {
  193. key: "recursiveReverse",
  194. /**
  195. * Reverses the linked list recursively
  196. */
  197. value: function recursiveReverse() {
  198. function inverse(current, next) {
  199. if (!next) {
  200. return;
  201. }
  202. inverse(next, next.next);
  203. next.next = current;
  204. }
  205. if (!this.first) {
  206. return;
  207. }
  208. inverse(this.first, this.first.next);
  209. this.first.next = null;
  210. var temp = this.first;
  211. this.first = this.last;
  212. this.last = temp;
  213. }
  214. }, {
  215. key: "reverse",
  216. /**
  217. * Reverses the linked list iteratively
  218. */
  219. value: function reverse() {
  220. if (!this.first || !this.first.next) {
  221. return;
  222. }
  223. var current = this.first.next;
  224. var prev = this.first;
  225. var temp = void 0;
  226. while (current) {
  227. temp = current.next;
  228. current.next = prev;
  229. prev.prev = current;
  230. prev = current;
  231. current = temp;
  232. }
  233. this.first.next = null;
  234. this.last.prev = null;
  235. temp = this.first;
  236. this.first = prev;
  237. this.last = temp;
  238. }
  239. }]);
  240. return LinkedList;
  241. }();
  242. ;
  243. exports.NodeStructure = NodeStructure;
  244. exports.default = LinkedList;