diff --git a/src/hash-table.js b/src/hash-table.js index 7edba9e..2e6f3c4 100644 --- a/src/hash-table.js +++ b/src/hash-table.js @@ -11,15 +11,47 @@ class HashTable { // Adds the given key, value pair to the hash table // Fetch the bucket associated with the given key using the getIndexBelowMax function - // If no bucket has been created for that index, instantiate a new bucket and add the key, value pair to that new bucket + // 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) { + // fetch index here + // if the index in the storage is empty, put (key, value) + // creates bucket ^^^^^ + const hIndex = getIndexBelowMax(key, this.limit); + const thisBucket = this.storage.get(hIndex) + if (thisBucket === undefined) { + this.storage.set(hIndex, [key, value]); + } else { + for(let i = 0; i < thisBucket.length; i++) { + if (thisBucket[i][0] === key) { + this.storage.set(hIndex, value); + } + } + if(thisBucket[i][0] !== key) { + this.storage.set([key, value]); + } + + } + + // if key exists, iterate through the storage at the index point, through the tuples and search for the key, if the key is foind + // put the value at that point [ 1 ]; } // 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 hIndex = getIndexBelowMax(key, this.limit); + const thisBucket = this.storage.get(hIndex) + if (this.storage[hIndex][0][0] === key) { + this.storage.set(hIndex, null); + } else { + for (let i = 0; i < this.storage[hIndex].length; i++) { + if (this.storage[hIndex][i][0] === key) { + this.storag.set(hIndex, null); + } + } + } } @@ -27,7 +59,17 @@ class HashTable { // 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 hIndex = getIndexBelowMax(key, this.limit); + if (this.storage[hIndex] === undefined) { + return undefined; + } else { + for(let i = 0; i < this.storage[hIndex].length; i++) { + if (this.storage[hIndex][i][0] === key) { + return this.storage[hIndex][i][1]; + } + } + } + } } diff --git a/src/linked-list.js b/src/linked-list.js index 267e295..b6bee03 100644 --- a/src/linked-list.js +++ b/src/linked-list.js @@ -6,20 +6,48 @@ class LinkedList { // Do not modify anything inside of the constructor } // Wraps the given value in a node object and adds the node to the tail of the list - // 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 + // 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 objNode = { + v: value, + next: null, + }; + if (this.tail === null) { + this.head = objNode; + this.tail = objNode; + return; + } + + const tailHolder = this.tail; + tailHolder.next = objNode; + this.tail = objNode; } // 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 null; + } + const headHolder = this.head; + this.head = headHolder.next; + if (headHolder.next === null) { + this.tail = headHolder.next; + } + return headHolder.value; } // Checks the linked list for the given value // Returns true if the the value is found in the list, false otherwise contains(value) { - + let node = this.head; + while (node !== null) { + if (node.value === value) { + return true; + } + node = node.next; + } + return false; } } diff --git a/src/queue.js b/src/queue.js index dcf7ad7..272172d 100644 --- a/src/queue.js +++ b/src/queue.js @@ -1,10 +1,25 @@ /* - 1. Add a constructor with a storage structure; there are multiple options you could use for this - 2. Add a size getter that returns the number of items the queue is storing - 3. Add an `enqueue` method that accepts an item as input and adds it to the storage structure - 4. Add a `dequeue` method that removes the item in the queue that was added earliest + 1. Add a constructor with a storage structure; there are multiple options you could use for this **** + 2. Add a size getter that returns the number of items the queue is storing**** + 3. Add an `enqueue` method that accepts an item as input and adds it to the storage structure **** + 4. Add a `dequeue` method that removes the item in the queue that was added earliest **** */ class Queue { + constructor() { + this.storage = []; + } + enqueue(item) { + this.storage.push(item); + } + + dequeue() { + return this.storage.shift(); + } + + get size() { + return this.storage.length; + } + } diff --git a/src/stack.js b/src/stack.js index 4312b41..fbf903d 100644 --- a/src/stack.js +++ b/src/stack.js @@ -1,11 +1,33 @@ /* - 1. Add a constructor with a storage structure; there are multiple options you could use for this - 2. Add a size getter that returns the number of items the stack is storing - 3. Add a `push` method that accepts an item as input and adds it to the storage structure + 1. Add a constructor with a storage structure; there are multiple options you could use for this **** + 2. Add a size getter that returns the number of items the stack is storing **** + 3. Add a `push` method that accepts an item as input and adds it to the storage structure **** 4. Add a `pop` method that removes the most recently-added item to the stack */ class Stack { + constructor() { + this.incrementStack = 0; + this.storage = {}; + } + push(item) { + this.storage[this.incrementStack] = item; + this.incrementStack++; + } + + pop() { + if (this.incrementStack === 0) { + return null; + } + this.incrementStack--; + const poppedResult = this.storage[this.incrementStack]; + this.storage[this.incrementStack] = null; + return poppedResult; + } + + get size() { + return this.incrementStack; + } } module.exports = Stack;