Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions src/hash-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,65 @@ 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);
}
}
}

}

// 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 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];
}
}
}

}
}

Expand Down
36 changes: 32 additions & 4 deletions src/linked-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
23 changes: 19 additions & 4 deletions src/queue.js
Original file line number Diff line number Diff line change
@@ -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;
}


}

Expand Down
28 changes: 25 additions & 3 deletions src/stack.js
Original file line number Diff line number Diff line change
@@ -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;