Guest User

Untitled

a guest
Oct 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. /**
  2. * @author Joseph Nathan Burton
  3. * @class Node
  4. * @package com.TANGL3SITES.DataStructures
  5. * @param val : <T>
  6. * @description This is just a helper object, which will provide new Nodes for the
  7. * Stack with a (val) and pointer (next).
  8. */
  9. const Node = (function(){
  10.  
  11. function Node(val) {
  12. this.val = val;
  13. this.next = null;
  14. }
  15.  
  16. return Node;
  17. })();
  18.  
  19.  
  20. /**
  21. * @author Joseph Nathan Burton
  22. * @class Stack
  23. * @package com.TANGL3SITES.DataStructures
  24. * @param
  25. * type : <T>
  26. * arguments.length = 1;
  27. * @description The stack is a commmon data structure that inserts elements at beginnig of the structure and removes them from the beginning.
  28. * @throws TypeError exception if the value inserted into the Object does not meet the
  29. * type checking criteria.
  30. */
  31. const Stack = (function(){
  32. let tail = null,
  33. head = null,
  34. spaceComplexity = 0,
  35. length = 0,
  36. TypeCheck = function(type, val) {
  37. if (typeof val !== type && val !== null) {
  38. throw new TypeError(`ERROR: ${this.__proto__.constructor.name} is of type ${type} but you have used ${val} which is of type ${typeof val}`);
  39. }
  40. };
  41.  
  42. function Stack(type) {
  43. this.head = head;
  44. this.tail = tail;
  45. this.length = length;
  46. spaceComplexity++;
  47. this.spaceComplexity = spaceComplexity;
  48. this.type = type;
  49. }
  50.  
  51. /**
  52. * @author Joseph Nathan Burton
  53. * @method Push
  54. * @param val : <T>
  55. * @description Inserts a new Node at the begginning of the list. First we create a new
  56. * Node. If the list is empty we set the head and tail to the newly created Node, else we
  57. * set the new Node's next prop to the current head of the list and then set the head of
  58. * the list to be the newly created node. Increment the list's length and return the list.
  59. * @returns this;
  60. */
  61. Stack.prototype.push = function(val) {
  62. TypeCheck().bind(this)(this.type, val)
  63. let newNode = new Node(val);
  64.  
  65. if (this.head === null) {
  66. this.head = newNode;
  67. this.tail = this.head;
  68. } else {
  69. newNode.next = this.head;
  70. this.head = newNode;
  71. }
  72.  
  73. this.length++;
  74. return this;
  75. }
  76.  
  77. /**
  78. * @author Joseph Nathan Burton
  79. * @method Pop
  80. * @param arguments = undefined
  81. * @description Removes the first element in the list and return it. As always we check
  82. * to see if the list is empty, if so return undefined; Store the current head in variable
  83. * (current). Set the head's prop to be the current head's next prop. Decrement the
  84. * length by 1 and returns current. [the old head of the list].
  85. * ** There is an EDGE CASE **
  86. * If the list is empty after shifting the head of the list, then set the tail to be
  87. * null.
  88. * @returns SinglyLinkedList.head;
  89. */
  90. Stack.prototype.pop = function() {
  91.  
  92. if (this.head === null) {
  93. return undefined;
  94. }
  95.  
  96. let current = this.head;
  97. this.head = current.next;
  98.  
  99. this.length--;
  100.  
  101. if (this.length === 0) {
  102. this.tail = null;
  103. }
  104. return current;
  105. }
  106.  
  107. /**
  108. * @author Joseph Nathan Burton
  109. * @method Peek
  110. * @param arguments = undefined
  111. * @description Peek checks the first element in the @method Queue, and returns the
  112. * element without removing the element from the data structure.
  113. * @returns string representation of the list;
  114. */
  115. Stack.prototype.peek = function() {
  116. if (this.head === null) {
  117. return undefined;
  118. }
  119.  
  120. return this.tail;
  121. }
  122.  
  123. /**
  124. * @author Joseph Nathan Burton
  125. * @method ToString
  126. * @param arguments = undefined
  127. * @description ToString over rides the Object's toString method. First we check to see
  128. * if the list is empty, if so return undefined. Set up a variable (string) and set it
  129. * equal to an empty string. Then loop through the list, storing a stringigfied version
  130. * of the element in a variable (stringified), and concatnate (stringified) with (string).
  131. * Then to prevent thre being an arrow after the last element in the stringified version
  132. * of the list, we only want to append an arrow if i < this.length - 1; Set up the
  133. * condition. Then lastly return (string);
  134. * @returns string representation of the list;
  135. */
  136. Stack.prototype.toString = function() {
  137.  
  138. if (this.head === null) {
  139. return "undefined";
  140. }
  141.  
  142. let string = "";
  143.  
  144. for (let i = 0; i < this.length; i++) {
  145. let stringified = `${this.get(i).val}`;
  146. string += stringified;
  147. if (i < this.length - 1) {
  148. string += " => ";
  149. }
  150. }
  151. return string;
  152. }
  153.  
  154. return Stack;
  155. })();
Add Comment
Please, Sign In to add comment