linkedList.js 6.0 KB

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