diff --git a/src/hash-table.js b/src/hash-table.js index 7edba9e..6da3fae 100644 --- a/src/hash-table.js +++ b/src/hash-table.js @@ -1,5 +1,5 @@ /* eslint-disable no-unused-vars */ -/* eslint-disable */ + const { LimitedArray, getIndexBelowMax } = require('./hash-table-helpers'); class HashTable { @@ -14,20 +14,57 @@ 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) { - + // use the key in this.limit to find the index + const hashIndex = getIndexBelowMax(key.toString(), this.limit); // gives index where key:value pair is in the hashtable + let bucket = this.storage.get(hashIndex) !== undefined ? this.storage.get(hashIndex) : []; + const tuple = [key, value]; // to handle when keys are in the same index + let replaced = false; + bucket = bucket.map((item) => { + if (item[0] === key) { + replaced = true; // this if tuple is replaced + return tuple; // tuple is how key:value pairs are stored + } + return item; + }); + if (!replaced) { + bucket.push(tuple); // if not replaced, push new tuple into the bucket + } + + // to put the bucket back into the hash table + this.storage.set(hashIndex, bucket); } // 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 the key, value pair from the bucket. put the bucket back + // after removing the key from the bucket remove(key) { - + if (this.storage.length === 0) return; // check if hash table is empty + const hashIndex = getIndexBelowMax(key.toString(), this.limit); // uses hash index to get bucket + const bucket = this.storage.get(hashIndex); // gets the bucket + if (bucket === undefined || bucket.length === 0) return; // bucket is not there if undefined, means no key is stored there + for (let i = 0; i < bucket.length; i++) { + const tuple = bucket[i]; + if (tuple[0] === key) { + bucket.splice(i, 1); // affects the original array + } + } + this.storage.set(hashIndex, bucket); // puts bucket back into the hash table } - + // 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) { - + if (this.storage.length === 0) return; // checks for the bucket + const hashIndex = getIndexBelowMax(key.toString(), this.limit); + const bucket = this.storage.get(hashIndex); + for (let i = 0; i < bucket.length; i++) { + const tuple = bucket[i]; + if (tuple[0] === key) { + return tuple[1]; + } + } + return undefined; } } diff --git a/src/linked-list.js b/src/linked-list.js index 267e295..15dcbad 100644 --- a/src/linked-list.js +++ b/src/linked-list.js @@ -8,18 +8,52 @@ class LinkedList { // 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 - addToTail(value) { + addToTail(value) { // creates new node + const newNode = { + value, // when key and value have same name, eslint allows just name + next: null, + }; + if (this.tail === null) { // checks if there is a tail + this.tail = newNode; // adds new node to head and tail + this.head = newNode; + return; + } + const holder = this.tail; // adds tail to exising node + holder.next = newNode; // changes pointer from null to the new node + 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 null; // determine if there is a head + const oldHead = this.head; + this.head = this.head.next; // moves pointer from old head to next + // if the head is the last node, both head and tail must be removed + if (this.head === null) { + this.tail = null; + } + return oldHead.value; } + /* const node = this.head; // if there is a head, moves the pointer to next head + this.head = node.next; + if (this.head === null) { + this.tail = null; // removes the tail by setting it to null + } + return node.value; */ + // } // Checks the linked list for the given value // Returns true if the the value is found in the list, false otherwise - contains(value) { - + contains(value) { // checks the entire linked list to see if contains the // value use an iterative process to check each node on the list + let node = this.head; + while (node !== null) { + if (node.value === value) { return true; } + node = node.next; // if node does not equal requested value, resets to next node in list + } + // for (let key in node) { // linked list - do not use + // this.head {value: 1, next: {value: 2, next: null}} // linked list do not use + // } + return false; } } diff --git a/src/queue.js b/src/queue.js index dcf7ad7..65a9ba0 100644 --- a/src/queue.js +++ b/src/queue.js @@ -4,8 +4,20 @@ 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 */ +// Queues are First in, First out FIFO class Queue { - + constructor() { + this.storage = []; + } + get size() { + return this.storage.length; + } + enqueue(value) { + this.storage.push(value); + } + dequeue(value) { + return this.storage.shift(); + } } module.exports = Queue; diff --git a/src/stack.js b/src/stack.js index 4312b41..5b5a3c1 100644 --- a/src/stack.js +++ b/src/stack.js @@ -4,8 +4,22 @@ 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 */ +// stacks are Last In, First Out LIFO class Stack { + constructor() { + this.storage = []; + } + get size() { + return this.storage.length; + } + push(value) { + this.storage.push(value); + } + pop() { + return this.storage.pop(); + } } + module.exports = Stack;