Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. /*jshint esnext: true */
  12.  
  13. "use strict";
  14.  
  15. 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; }; })();
  16.  
  17. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  18.  
  19. var Node = function Node(element) {
  20. _classCallCheck(this, Node);
  21.  
  22. this.element = element;
  23. this.next = null;
  24. };
  25.  
  26. var LinkedList = (function () {
  27. function LinkedList() {
  28. _classCallCheck(this, LinkedList);
  29.  
  30. this.head = null;
  31. this.size = 0;
  32. }
  33.  
  34. // adds an element at the end of list
  35.  
  36. _createClass(LinkedList, [{
  37. key: "add",
  38. value: function add(element) {
  39. var node = new Node(element);
  40. var current = undefined;
  41.  
  42. // if list is Empty add the element and make it head
  43. if (this.head == null) this.head = node;else {
  44. current = this.head;
  45.  
  46. // iterate to the end of the list
  47. while (current.next) {
  48. current = current.next;
  49. }
  50.  
  51. // add node
  52. current.next = node;
  53. }
  54. this.size++;
  55. }
  56. }, {
  57. key: "reverse",
  58. value: function reverse() {
  59. var prev = undefined,
  60. next = null;
  61. var curr = this.head;
  62.  
  63. while (curr) {
  64. next = curr.next;
  65. curr.next = prev;
  66. prev = curr;
  67. curr = next;
  68. }
  69. this.head = prev;
  70. }
  71. }, {
  72. key: "printList",
  73. value: function printList() {
  74. var curr = this.head;
  75. var str = "";
  76.  
  77. while (curr) {
  78. str += curr.element + " ";
  79. curr = curr.next;
  80. }
  81.  
  82. console.log(str);
  83. }
  84. }]);
  85.  
  86. return LinkedList;
  87. })();
  88.  
  89. var ll = new LinkedList();
  90. ll.add(10);
  91. ll.add(20);
  92. ll.add(30);
  93. ll.add(40);
  94. ll.printList();
  95. ll.reverse();
  96. ll.printList();
  97. </script>
  98.  
  99.  
  100.  
  101. <script id="jsbin-source-javascript" type="text/javascript">/*jshint esnext: true */
  102.  
  103. class Node {
  104. constructor(element){
  105. this.element = element;
  106. this.next = null;
  107. }
  108. }
  109.  
  110. class LinkedList {
  111. constructor() {
  112. this.head = null;
  113. this.size = 0;
  114. }
  115.  
  116. // adds an element at the end of list
  117. add(element) {
  118. let node = new Node(element);
  119. let current;
  120.  
  121. // if list is Empty add the element and make it head
  122. if (this.head == null) this.head = node;
  123. else {
  124. current = this.head;
  125.  
  126. // iterate to the end of the list
  127. while (current.next) {
  128. current = current.next;
  129. }
  130.  
  131. // add node
  132. current.next = node;
  133. }
  134. this.size++;
  135. }
  136.  
  137. reverse() {
  138. let prev, next = null;
  139. let curr = this.head;
  140.  
  141. while(curr) {
  142. next = curr.next;
  143. curr.next = prev;
  144. prev = curr;
  145. curr = next;
  146. }
  147. this.head = prev;
  148. }
  149.  
  150. printList() {
  151. let curr = this.head;
  152. let str = "";
  153.  
  154. while (curr) {
  155. str += curr.element + " ";
  156. curr = curr.next;
  157. }
  158.  
  159. console.log(str);
  160. }
  161. }
  162.  
  163.  
  164. let ll = new LinkedList();
  165. ll.add(10);
  166. ll.add(20);
  167. ll.add(30);
  168. ll.add(40);
  169. ll.printList();
  170. ll.reverse();
  171. ll.printList();
  172.  
  173. </script></body>
  174. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement