Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Joseph Nathan Burton
- * @class Node
- * @package com.TANGL3SITES.DataStructures
- * @param val : <T>
- * @description This is just a helper object, which will provide new Nodes for the
- * Stack with a (val) and pointer (next).
- */
- const Node = (function(){
- function Node(val) {
- this.val = val;
- this.next = null;
- }
- return Node;
- })();
- /**
- * @author Joseph Nathan Burton
- * @class Stack
- * @package com.TANGL3SITES.DataStructures
- * @param
- * type : <T>
- * arguments.length = 1;
- * @description The stack is a commmon data structure that inserts elements at beginnig of the structure and removes them from the beginning.
- * @throws TypeError exception if the value inserted into the Object does not meet the
- * type checking criteria.
- */
- const Stack = (function(){
- let tail = null,
- head = null,
- spaceComplexity = 0,
- length = 0,
- TypeCheck = function(type, val) {
- if (typeof val !== type && val !== null) {
- throw new TypeError(`ERROR: ${this.__proto__.constructor.name} is of type ${type} but you have used ${val} which is of type ${typeof val}`);
- }
- };
- function Stack(type) {
- this.head = head;
- this.tail = tail;
- this.length = length;
- spaceComplexity++;
- this.spaceComplexity = spaceComplexity;
- this.type = type;
- }
- /**
- * @author Joseph Nathan Burton
- * @method Push
- * @param val : <T>
- * @description Inserts a new Node at the begginning of the list. First we create a new
- * Node. If the list is empty we set the head and tail to the newly created Node, else we
- * set the new Node's next prop to the current head of the list and then set the head of
- * the list to be the newly created node. Increment the list's length and return the list.
- * @returns this;
- */
- Stack.prototype.push = function(val) {
- TypeCheck().bind(this)(this.type, val)
- let newNode = new Node(val);
- if (this.head === null) {
- this.head = newNode;
- this.tail = this.head;
- } else {
- newNode.next = this.head;
- this.head = newNode;
- }
- this.length++;
- return this;
- }
- /**
- * @author Joseph Nathan Burton
- * @method Pop
- * @param arguments = undefined
- * @description Removes the first element in the list and return it. As always we check
- * to see if the list is empty, if so return undefined; Store the current head in variable
- * (current). Set the head's prop to be the current head's next prop. Decrement the
- * length by 1 and returns current. [the old head of the list].
- * ** There is an EDGE CASE **
- * If the list is empty after shifting the head of the list, then set the tail to be
- * null.
- * @returns SinglyLinkedList.head;
- */
- Stack.prototype.pop = function() {
- if (this.head === null) {
- return undefined;
- }
- let current = this.head;
- this.head = current.next;
- this.length--;
- if (this.length === 0) {
- this.tail = null;
- }
- return current;
- }
- /**
- * @author Joseph Nathan Burton
- * @method Peek
- * @param arguments = undefined
- * @description Peek checks the first element in the @method Queue, and returns the
- * element without removing the element from the data structure.
- * @returns string representation of the list;
- */
- Stack.prototype.peek = function() {
- if (this.head === null) {
- return undefined;
- }
- return this.tail;
- }
- /**
- * @author Joseph Nathan Burton
- * @method ToString
- * @param arguments = undefined
- * @description ToString over rides the Object's toString method. First we check to see
- * if the list is empty, if so return undefined. Set up a variable (string) and set it
- * equal to an empty string. Then loop through the list, storing a stringigfied version
- * of the element in a variable (stringified), and concatnate (stringified) with (string).
- * Then to prevent thre being an arrow after the last element in the stringified version
- * of the list, we only want to append an arrow if i < this.length - 1; Set up the
- * condition. Then lastly return (string);
- * @returns string representation of the list;
- */
- Stack.prototype.toString = function() {
- if (this.head === null) {
- return "undefined";
- }
- let string = "";
- for (let i = 0; i < this.length; i++) {
- let stringified = `${this.get(i).val}`;
- string += stringified;
- if (i < this.length - 1) {
- string += " => ";
- }
- }
- return string;
- }
- return Stack;
- })();
Add Comment
Please, Sign In to add comment