diff --git a/src/hash-table.js b/src/hash-table.js index 7edba9e..d520946 100644 --- a/src/hash-table.js +++ b/src/hash-table.js @@ -8,26 +8,81 @@ class HashTable { this.storage = new LimitedArray(this.limit); // Do not modify anything inside of the constructor } - + checkCapacity() { + let fullcells = 1; + this.storage.each((bucket) => { + if (bucket !== undefined) fullcells++; + }); + return fullCells / this.limit > 0.75; + } + resize() { + this.limit *= 2; + const oldStorage = this.storage; + this.storage = new LimitedArray(this.limit); + oldStorage.each((bucket) => { + if (bucket === undefined) return; + bucket.forEach((pair) => { + this.insert(pair[0], pair[1]); + }); + }); + } // 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 the key already exists in the bucket, the newer value should overwrite the older value associated with that key insert(key, value) { - + if (this.checkCapacity()) this.resize(); + const index = getIndexBelowMax(key.toString(), this.limit); + const bucket = this.storage.get(index); + if (bucket === undefined) { + this.storage.set(index, [[key, value]]); + return; + } + for (let i = 0; i < bucket.length; i++) { + const cell = bucket[i]; + if (cell[0] === key) { + cell[1] = value; + this.storage.set(index, bucket); + return; + } + } + bucket.push([key, value]); + this.storage.set(index, bucket); } +/* bucket.foreach((cell) => { +if (cell[0] === key) { +cell[1] = value; +this.storage.set(index, 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(key) { - + const index = getIndexBelowMax(key.toString(), this.limit); + const bucket = this.storage.get(index); + if (bucket.length === 1) { + const value = bucket[0][1]; + this.storage.set(index, undefined); + return value; + } + bucket.forEach((pair, i) => { + if (pair[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 === undefined) return undefined; + 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..a9ca071 100644 --- a/src/linked-list.js +++ b/src/linked-list.js @@ -9,18 +9,67 @@ 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 = { + value, + next: null + }; + if (this.head === null) { + this.head = node; + this.tail = node; + } else { + this.tail.next = node; + this.tail = node; + } +/* const newNode = { +value: item, +next: null +}; +if (this.head === null) { +this.head = newNode; +this.tail = newNode; +return; +} +if (this.head.next === null) { +this.tail = newNode; +this.head.next = 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 null; + if (this.head === this.tail) { + const value = this.head.value; + this.head = null; + this.tail = null; + return value; +/* if (this.head === null) return; +if (this.head.next === null) { +const reference = this.head; +this.head = null; +this.tail = null; +return reference; +} +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; +//here we are using recursive function.( or you can use while loop) +const searchLinkedList = (node) => { +if (node.value === value) return true; +if (node.next === null) return false; +return searchLinkedList(node.next); +}; +return searchLinkedList(this.head) } } - module.exports = LinkedList; diff --git a/src/queue.js b/src/queue.js index dcf7ad7..8afe116 100644 --- a/src/queue.js +++ b/src/queue.js @@ -5,7 +5,18 @@ 4. Add a `dequeue` method that removes the item in the queue that was added earliest */ class Queue { - + constructor() { + this.storage = []; + } + enqueue(value) { + this.storage.unshift(value); + } + dequeue() { + return this.storage.pop(); + } + get size() { + return this.storage.size; + } } module.exports = Queue; diff --git a/src/stack.js b/src/stack.js index 4312b41..96bc404 100644 --- a/src/stack.js +++ b/src/stack.js @@ -5,7 +5,27 @@ 4. Add a `pop` method that removes the most recently-added item to the stack */ class Stack { - + constructor() { + this.storage = []; + } + add(value) { + this.storage.push(value); + } + remove(value) { + return this.storage.pop(); + } + get size() { + return this.storage.length; + } } +module.exports = Stack; +const stack = new stack(); +/* +what is get size and example- +stack.add(5); +stack.add(10); +stack.add(15); +console.log(stack.size); +*/ module.exports = Stack;