constructor() { this._storage = {} this._length = 0 } /* * Adds a new value at the end of the * stack * @param {*} value - the value to push */ push(value) { // TODO: Add typechecking and arguments this._storage[this._length] = value this._length += 1 }
/* * Removes the value at the end of the stack and returns it * @return {*} the last and newest value in the stack */ pop() { // What if it is empty if(!this._length) { returnundefined } const last = this._storage[this._length - 1] this._storage[this._length - 1] = undefined this._length -= 1 return last } /* * Returns the value at the end of the stack without removing it * @return {*} the last and newest value in the stack */ peek() { if(!this._length) { returnundefined } returnthis._storage[this._length - 1] } }
constructor() { this._storage = {}; this._length = 0 this._headIndex = 0 } /* * Enqueues a new value at the end of the queue * @param {*} value the value to enqueue */ enqueue(value) { const lastIndex = this._length + this._headIndex this._storage[lastIndex] = value this._length += 1 }
/* * Dequeues the value from the beginning of the queue and returns it * @return {*} the first and oldest value in the queue */ dequeue() { const first = this._storage[this._headIndex] deletethis._storage[this._headIndex] this._length -= 1 this._headIndex += 1 return first } /* * Returns the value at the beginning of the queue without removing it from the queue * @return {*} the first and oldest value in the queue */ peek() {
this.tail = this.head } /* * Inserts a new value to the end of the linked list * @param {*} value - the value to insert */ insert(value) { const node = {value, next: null} this.tail.next = node this.tail = node } /* * Deletes a node * @param {*} node - the node to remove * @return {*} value - the deleted node's value */ remove(node) { let currentNode = this.head while(currentNode.next !== node) { currentNode = currentNode.next } currentNode.next = node.next } /* * Removes the value at the end of the linked list * @return {*} - the removed value */ removeTail() { let currentNode = this.head while(currentNode.next !== this.tail) { currentNode = currentNode.next } this.tail = currentNode this.tail.next = null } /* * Searches the linked list and returns true if it contains the value passed * @param {*} value - the value to search for * @return {boolean} - true if value is found, otherwise false */ contains(value) { let currentNode = this.head while(currentNode.value !== value) { currentNode = currentNode.next } return currentNode.value === value } /* * Checks if a node is the head of the linked list * @param {{prev:Object|null, next:Object|null}} node - the node to check * @return {boolean} - true if node is the head, otherwise false */ isHead(node) { return node === this.head } /* * Checks if a node is the tail of the linked list * @param {{prev:Object|null, next:Object|null}} node - the node to check * @return {boolean} - true if node is the tail, otherwise false */ isTail(node) { return node === this.tail } }
constructor(val) { this._storage = [] this._size = val } /* * Inserts a new key-value pair * @param {string} key - the key associated * with the value * @param {*} value - the value to insert */ insert(key, value) { const index = this._hash(key, this._size) if(!this._storage[index]) { this._storage[index] = [] } this._storage[index].push([key,value]) } /* * Deletes a key-value pair * @param {string} key - the key associated with the value * @return {*} value - the deleted value */ remove() {
} /* * Returns the value associated with a key * @param {string} key - the key to search for * @return {*} - the value associated with the key */ retrieve(key) { const index = this._hash(key, this._size) if(this._storage[index]) { returnthis._storage[index].filter(item => item[0] === key)[0][1] } } /* * Hashes string value into an integer that can be mapped to an array index * @param {string} str - the string to be hashed * @param {number} n - the size of the storage array * @return {number} - an integer between 0 and n */ _hash(str, n) { let sum = 0; for (let i = 0; i < str.length; i++) sum += str.charCodeAt(i) * 3