diff --git a/src/hash-table.js b/src/hash-table.js index 7edba9e..5387963 100644 --- a/src/hash-table.js +++ b/src/hash-table.js @@ -15,20 +15,19 @@ class HashTable { // If the key already exists in the bucket, the newer value should overwrite the older value associated with that key insert(key, value) { - } + }; // 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) { - - } + + }; // 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) { - - } + + }; } - module.exports = HashTable; diff --git a/src/linked-list.js b/src/linked-list.js index 267e295..1510cc1 100644 --- a/src/linked-list.js +++ b/src/linked-list.js @@ -9,17 +9,45 @@ 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 node = { + next: null, + value, + }; + if (this.tail === null) { + this.tail = node; + this.head = node; + return; + } + this.tail.next = node; + this.tail = node; } // 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; + } + 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 find = (node) => { + if (node.value === value) { + return true; + } + if (node.next === null) { + return false; + } + return (find(node.next)); + }; + return (find(this.head)); } } diff --git a/src/queue.js b/src/queue.js index dcf7ad7..c392295 100644 --- a/src/queue.js +++ b/src/queue.js @@ -5,7 +5,24 @@ 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(item) { + this.storage.push(item); + return this.size; + } + + dequeue(item) { + return this.storage.shift(item); + } } + module.exports = Queue; diff --git a/src/stack.js b/src/stack.js index 4312b41..9612adf 100644 --- a/src/stack.js +++ b/src/stack.js @@ -5,6 +5,23 @@ 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) { + this.storage.push(item); + return this.counter++; + } + + pop(item) { + return this.storage.pop(item); + } }