diff --git a/src/hash-table.js b/src/hash-table.js index abe7fde..7671ca1 100644 --- a/src/hash-table.js +++ b/src/hash-table.js @@ -16,19 +16,54 @@ class HashTable { // If no bucket has been created for that index, instantiate a new bucket and add the key, value pair to that new bucket // If the key already exists in the bucket, the newer value should overwrite the older value associated with that key insert(key, value) { + const index = getIndexBelowMax(key.toString(), this.limit); + const tempBucket = this.storage.get(index); + if (tempBucket === undefined) { + this.storage.set(index, [[key, value]]); + return; + } + for (let i = 0; i < tempBucket.length; i++) { + if (tempBucket[i][0] === key) { + tempBucket[i][1] = value; + this.storage.set(index, tempBucket); + return; + } + } + + tempBucket.push([key, value]); + this.storage.set(index.tempBucket); } // Removes the key, value pair from the hash table // Fetch the bucket associated with the given key using the getIndexBelowMax function // Remove the key, value pair from the bucket remove(key) { - + const index = getIndexBelowMax(key.toString(), this.limit); + const bucket = this.storage.get(index); + if (bucket === undefined) return undefined; + if (bucket.length === 1 && bucket[0][0] === key) { + this.storage.set(index, undefined); + return; + } + for (let i = 0; i < bucket.length; i++) { + if (bucket[i][0] === key) { + bucket.splice(i, 1); + this.storage.set(index, bucket); + } + } } // Fetches the value associated with the given key from the hash table // Fetch the bucket associated with the given key using the getIndexBelowMax function // Find the key, value pair inside the bucket and return the value retrieve(key) { - + const index = getIndexBelowMax(key.toString(), this.limit); + const bucket = this.storage.get(index); + if (!bucket) return; + for (let i = 0; i < bucket.length; i++) { + if (bucket[i][0] === key) { + return bucket[i][1]; + } + } } } diff --git a/src/linked-list.js b/src/linked-list.js index 267e295..4bb9574 100644 --- a/src/linked-list.js +++ b/src/linked-list.js @@ -9,17 +9,43 @@ class LinkedList { // If the list is empty, the new element is considered the tail as well as the head // If there is one element in the list before the new element is added, the new element becomes the tail of the list addToTail(value) { + const newNode = { + next: null, + value, + }; + if (this.head === null) { + this.head = newNode; + this.tail = newNode; + return; + } + this.tail.next = newNode; + this.tail = newNode; } // Removes the current head node from the list, replacing it with the next element in the list // Returns the value of the removed node removeHead() { - + if (this.head === null) return; + if (this.head.next === null) { + const value = this.head.value; + this.head = null; + this.tail = null; + return; + } + const value = this.head.value; + this.head = this.head.next; + return value; } // Checks the linked list for the given value // Returns true if the the value is found in the list, false otherwise contains(value) { - + if (this.head === null) return false; + const searchLinkedList = (node) => { + if (node.value === value) return true; + if (node.next === null) return false; + return searchLinkedList(node.text); + }; + return searchLinkedList(this.head); } } diff --git a/src/queue.js b/src/queue.js index dcf7ad7..8781329 100644 --- a/src/queue.js +++ b/src/queue.js @@ -5,6 +5,22 @@ 4. Add a `dequeue` method that removes the item in the queue that was added earliest */ class Queue { + constructor() { + this.storage = []; + } + + get size() { + return this.storage.length; + } + + enqueue(input) { + this.storage.push(input); + return this.size(); + } + + dequeue() { + return this.storage.shift(); + } } diff --git a/src/stack.js b/src/stack.js index 4312b41..d82636a 100644 --- a/src/stack.js +++ b/src/stack.js @@ -5,7 +5,30 @@ 4. Add a `pop` method that removes the most recently-added item to the stack */ class Stack { + constructor() { + this.storage = []; + this.counter = 0; + } + get size() { + return this.counter; + } + + push(item) { + const len = this.size; + this.storage[len] = item; + this.counter++; + return this.counter; + } + + pop() { + const len = this.size; + if (len === 0) return null; + const popped = this.storage[len - 1]; + this.storage[len - 1] = null; + this.counter--; + return popped; + } } module.exports = Stack;