diff --git a/src/doubly-linked-list.js b/src/doubly-linked-list.js index 6373ce3..82a7e22 100644 --- a/src/doubly-linked-list.js +++ b/src/doubly-linked-list.js @@ -3,4 +3,107 @@ class DoublyLinkedList { this.head = null; this.tail = null; } + + addToTail(value) { + const node = { + value, + next: null, + prev: null, + }; + if (this.tail) { + const temp = this.tail; + this.tail.next = node; + this.tail = node; + this.tail.prev = temp; + } else { + this.head = node; + this.tail = node; + } + } + + addToHead(value) { + const node = { + value, + next: null, + prev: null, + }; + if (this.head) { + const temp = this.head; + this.head = node; + this.head.next = temp; + } else { + this.head = 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 + removeFromHead() { + if (!this.head) return null; + const node = this.head; + + if (this.head === this.tail) { + this.head = null; + this.tail = null; + } else { + this.head = this.head.next; + this.head.prev = null; + } + return node; + } + + removeFromTail() { + if (!this.tail) return null; + const node = this.tail; + + if (this.head === this.tail) { + this.head = null; + this.tail = null; + } else { + this.tail = this.tail.prev; + this.tail.next = null; + } + return node; + } + // Checks the linked list for the given value + // Returns true if the the value is found in the list, false otherwise + delete(node) { + let current = this.head; + while (current) { + if (current.value === node.value) { + current.prev.next = current.next; + current.next.prev = current.prev; + break; + } + current = current.next; + } + } + + moveToFront(node) { + let current = node; + while (current) { + if (current === this.head) { + const temp = this.head; + this.head = node; + this.head.next = temp; + break; + } + current = current.prev; + } + } + + moveToBack(node) { + let current = node; + while (current) { + if (current === this.tail) { + const temp = this.tail; + this.tail = node; + this.tail.prev = temp; + break; + } + current = current.next; + } + } } + +module.exports = DoublyLinkedList; diff --git a/src/hash-table.js b/src/hash-table.js index 7edba9e..2f40622 100644 --- a/src/hash-table.js +++ b/src/hash-table.js @@ -6,7 +6,6 @@ class HashTable { constructor(limit = 8) { this.limit = limit; this.storage = new LimitedArray(this.limit); - // Do not modify anything inside of the constructor } // Adds the given key, value pair to the hash table @@ -14,20 +13,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) { + let index; + if (typeof key === 'string') index = getIndexBelowMax(key, this.limit) + else index = key; + let bucket = this.storage.get(index); + + if (!bucket) { + bucket = []; + bucket.push([key, value]); + } + for (let i = 0; i < bucket.length; i++) { + let tuple = bucket[i]; + if (tuple[0] === key) tuple[1] = value; + } + bucket.push([key, 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) { + let index; + if (typeof key === 'string') index = getIndexBelowMax(key, this.limit) + else index = key; + let bucket = this.storage.get(index); - } + if (!bucket) return; + for (let i = 0; i < bucket.length; i++) { + let tuple = bucket[i]; + if (tuple[0] === key) bucket.splice(i, 1); + } + bucket = []; + 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) { - + let index; + if (typeof key === 'string') index = getIndexBelowMax(key, this.limit) + else index = key; + let bucket = this.storage.get(index); + + if (!bucket) return; + + for (let i = 0; i < bucket.length; i++) { + let tuple = bucket[i]; + if (tuple[0] === key) return tuple[1]; + } } } diff --git a/src/linked-list.js b/src/linked-list.js index 267e295..895d828 100644 --- a/src/linked-list.js +++ b/src/linked-list.js @@ -9,17 +9,36 @@ 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.tail === null) { + this.head = node; + } else { + 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() { - + const removedValue = this.head.value; + this.head = this.head.next; + return removedValue; } + // Checks the linked list for the given value // Returns true if the the value is found in the list, false otherwise contains(value) { + let current = this.head; + while (current) { + if (current.value === value) return true; + current = current.next; + } + return false; } } diff --git a/src/queue.js b/src/queue.js index dcf7ad7..486d365 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.items = []; + this.size = 0; + } + size() { + return this.size; + } + enqueue(item) { + this.size++; + this.items.push(item); + } + dequeue() { + if (this.size === 0) { + return 0; + } + --this.size; + return this.items.shift(); + } } module.exports = Queue; diff --git a/src/stack.js b/src/stack.js index 4312b41..10a0187 100644 --- a/src/stack.js +++ b/src/stack.js @@ -1,11 +1,28 @@ -/* + /* 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 is the constructor + this.items = []; // this is storage could use linked list or obj + this.size = 0; + } + size() { + return this.size; + } + push(item) { + this.items.push(item); + ++this.size; + } + pop() { + if (this.size === 0) { // this.storage.pop(); + return this.size; + } + this.size--; + return this.items.pop(); + } } module.exports = Stack;