API Docs for: 0.0.4
Show:

File: Datastructures/DoublyLinkedList.ts

  1. /**
  2. * The DoublyLinkedList class provides the main functionality of a doubly linked list.
  3. *
  4. * @class DoublyLinkedList
  5. */
  6. class DoublyLinkedList {
  7.  
  8. /**
  9. * Count of elements in list
  10. *
  11. * @property _length
  12. * @type number
  13. * @private
  14. */
  15. private _length = 0;
  16.  
  17. /**
  18. * Iteration pointer
  19. *
  20. * @property _key
  21. * @type number
  22. * @private
  23. */
  24. private _key = 0;
  25.  
  26. /**
  27. * Reference to head(first) element in list
  28. *
  29. * @property _head
  30. * @type DoublyLinkedListNode
  31. * @private
  32. */
  33. private _head:DoublyLinkedListNode = null;
  34.  
  35. /**
  36. * Reference to tail(last) element in list
  37. *
  38. * @property _tail
  39. * @type DoublyLinkedListNode
  40. * @private
  41. */
  42. private _tail:DoublyLinkedListNode = null;
  43.  
  44. /**
  45. * Reference to iterated element in list
  46. *
  47. * @property _current
  48. * @type DoublyLinkedListNode
  49. * @private
  50. */
  51. private _current:DoublyLinkedListNode = null;
  52.  
  53. /**
  54. * Insert a new value at the specified index
  55. *
  56. * @method add
  57. * @param index The index where the new value is to be inserted.
  58. * @param value The new value for the index.
  59. * @return void
  60. */
  61. public add(index:any, value:any):void {
  62. // TODO:
  63. }
  64.  
  65. /**
  66. * Pops a node from the end of the doubly linked list
  67. *
  68. * @method pop
  69. * @return any The value of the popped node.
  70. */
  71. public pop():any {
  72. if (this._length === 0) {
  73. throw new Error("Can't pop from an empty data structure");
  74. }
  75.  
  76. var value = this._tail.value;
  77.  
  78. this._tail = this._tail.prev;
  79. if (this._tail) {
  80. delete this._tail.next;
  81. this._tail.next = null;
  82. }
  83.  
  84. this._length--;
  85.  
  86. return value;
  87. }
  88.  
  89. /**
  90. * Shifts a node from the beginning of the doubly linked list
  91. *
  92. * @method shift
  93. * @return any The value of the shifted node.
  94. */
  95. public shift():any {
  96. if (this._length === 0) {
  97. throw new Error("Can't shift from an empty data structure");
  98. }
  99.  
  100. var value = this._head.value;
  101.  
  102. this._head = this._head.next;
  103. if (this._head) {
  104. delete this._head.prev;
  105. this._head.prev = null;
  106. }
  107.  
  108. this._length--;
  109.  
  110. return value;
  111. }
  112.  
  113. /**
  114. * Pushes an element at the end of the doubly linked list
  115. *
  116. * @method push
  117. * @param value The value to push.
  118. * @return void
  119. */
  120. public push(value:any):void {
  121. // allocate new node
  122. var node:DoublyLinkedListNode = {
  123. value: value,
  124. prev: this._tail,
  125. next: null
  126. };
  127.  
  128. if (this._length === 0) {
  129. this._head = this._tail = node;
  130. } else {
  131. this._tail.next = node;
  132. this._tail = this._tail.next;
  133. }
  134.  
  135. this._length++;
  136. }
  137.  
  138. /**
  139. * Prepends the doubly linked list with an element
  140. *
  141. * @method unshift
  142. * @param value The value to unshift.
  143. * @return void
  144. */
  145. public unshift(value:any):void {
  146. // allocate new node
  147. var node:DoublyLinkedListNode = {
  148. value: value,
  149. prev: null,
  150. next: this._head
  151. };
  152.  
  153. if (this._length === 0) {
  154. this._head = this._tail = node;
  155. } else {
  156. this._head.prev = node;
  157. this._head = this._head.prev;
  158. }
  159.  
  160. this._length++;
  161. }
  162.  
  163. /**
  164. * Peeks at the node from the end of the doubly linked list
  165. *
  166. * @method top
  167. * @return any The value of the last node.
  168. */
  169. public top():any {
  170. return this._tail.value;
  171. }
  172.  
  173. /**
  174. * Peeks at the node from the beginning of the doubly linked list
  175. *
  176. * @method bottom
  177. * @return any The value of the first node.
  178. */
  179. public bottom():any {
  180. return this._head.value;
  181. }
  182.  
  183. /**
  184. * Counts the number of elements in the doubly linked list
  185. *
  186. * @method count
  187. * @return number the number of elements in the doubly linked list.
  188. */
  189. public count():number {
  190. return this._length;
  191. }
  192.  
  193. /**
  194. * Checks whether the doubly linked list is empty
  195. *
  196. * @method isEmpty
  197. * @return boolean whether the doubly linked list is empty.
  198. */
  199. public isEmpty():boolean {
  200. return (this._length === 0);
  201. }
  202.  
  203. /**
  204. * Rewind iterator back to the start
  205. *
  206. * @method rewind
  207. * @return void
  208. */
  209. public rewind():void {
  210. this._key = 0;
  211. this._current = this._head;
  212. }
  213.  
  214. /**
  215. * Return current list entry
  216. *
  217. * @method current
  218. * @return any The current node value.
  219. */
  220. public current():any {
  221. if (this._current) {
  222. return this._current.value;
  223. }
  224. return null;
  225. }
  226.  
  227. /**
  228. * Return current node index
  229. *
  230. * @method key
  231. * @return any The current node index.
  232. */
  233. public key():any {
  234. return this._key;
  235. }
  236.  
  237. /**
  238. * Move to next entry
  239. *
  240. * @method next
  241. * @return void
  242. */
  243. public next():void {
  244. this._current = this._current.next;
  245. this._key++;
  246. }
  247.  
  248. /**
  249. * Move to previous entry
  250. *
  251. * @method prev
  252. * @return void
  253. */
  254. public prev():void {
  255. this._current = this._current.prev;
  256. this._key--;
  257. }
  258.  
  259. /**
  260. * Check whether the doubly linked list contains more nodes
  261. *
  262. * @method valid
  263. * @return boolean true if the doubly linked list contains any more nodes, false otherwise.
  264. */
  265. public valid():boolean {
  266. return (this._key >= 0 && this._key < this._length);
  267. }
  268.  
  269. /**
  270. * Export the list to array
  271. *
  272. * @method toArray
  273. * @return Array The exported array
  274. */
  275. public toArray():Array<any> {
  276. var list = [];
  277. var current = this._head;
  278. while (current) {
  279. list.push(current.value);
  280. current = current.next;
  281. }
  282. return list;
  283. }
  284.  
  285. /**
  286. * Serializes the list to string
  287. *
  288. * @method toString
  289. * @return string The serialized string.
  290. */
  291. public toString():string {
  292. return "{" + this.toArray().join("->") + "}";
  293. }
  294. }
  295.  
  296. /**
  297. * DoublyLinkedList element
  298. */
  299. interface DoublyLinkedListNode {
  300. value:any;
  301. prev:DoublyLinkedListNode;
  302. next:DoublyLinkedListNode;
  303. }
  304.  
  305. export = DoublyLinkedList;