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
87 changes: 87 additions & 0 deletions src/doubly-linked-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,91 @@ class DoublyLinkedList {
this.head = null;
this.tail = null;
}
addToTail(value) {
const newTail = {
value,
next: null,
prev: null,
};
if (this.tail === null) {
this.head = newTail;
this.tail = newTail;
return;
}
const oldTail = this.tail;
oldTail.next = newTail;
this.tail = newTail;
newTail.prev = oldTail;
}
addToHead(value) {
const newNode = {
value,
next: null,
prev: null,
};
if (this.head === null) {
this.head = newNode;
this.tail = newNode;
return;
}
const oldHead = this.head;
oldHead.prev = newNode;
this.head = newNode;
this.head.next = oldHead;
}
removeFromHead() {
const oldHead = this.head;
if (oldHead === null) return null;
if (oldHead.next !== null) {
this.head = oldHead.next;
this.head.prev = null;
} else {
this.head = null;
this.tail = null;
}
return oldHead;
}

removeFromTail() {
const oldTail = this.tail;
if (oldTail === null) return null;
if (oldTail.prev !== null) {
this.tail = oldTail.prev;
this.tail.next = null;
} else {
this.head = null;
this.tail = null;
}
return oldTail;
}
delete(node) {
if (node.value === this.head.value) {
this.removeFromHead();
}
if (node.value === this.tail.value) {
this.removeFromTail();
}
let current = this.head;
while (current !== null) {
if (current.value === node.value) {
current.prev.next = current.next;
current.next.prev = current.prev;
break;
}
current = current.next;
}
return;
}
moveToFront(node) {
if (this.head.value === node.value) return;
this.delete(node);
this.addToHead(node.value);
}
moveToBack(node) {
if (this.tail.value === node.value) return;
this.delete(node);
this.addToTail(node.value);
}
}

module.exports = DoublyLinkedList;
38 changes: 34 additions & 4 deletions src/hash-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,50 @@ 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) {

this.resize();
const index = getIndexBelowMax(key.toString(), this.limit);
let bucket = this.storage.get(index) || [];
bucket = bucket.filter(item => item[0] !== key);
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) {

const index = getIndexBelowMax(key.toString(), this.limit);
let bucket = this.storage.get(index);
if (bucket) {
bucket = bucket.filter(item => item[0] !== key);
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);
let retrieved;
if (bucket) {
retrieved = bucket.filter(item => item[0] === key)[0];
}
return retrieved ? retrieved[1] : undefined;
}

resize() {
if (this.storage.length < (.75 * this.limit)) return;
let oldHT = this.storage;
this.limit *= 2;
this.storage = new LimitedArray(this.limit);
oldHT.each((bucket, index) => {
if (Array.isArray(bucket)) {
for (let i = 0; i < bucket.length; i++) {
this.insert(...bucket[i]);
}
}
})
}
}

Expand Down
32 changes: 29 additions & 3 deletions src/linked-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,43 @@ 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 newNode = {
value,
next: null,
};
if (this.tail === null) {
this.head = newNode;
this.tail = newNode;
return;
}
const holder = this.tail;
holder.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;
}
const node = this.head;
this.head = node.next;
if (this.head === null) {
this.tail = 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) {

let node = this.head;
while (node !== null) {
if (value === node.value) {
return true;
}
node = node.next;
}
return false;
}
}

Expand Down
19 changes: 18 additions & 1 deletion src/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,24 @@
4. Add a `dequeue` method that removes the item in the queue that was added earliest
*/
class Queue {

constructor(items) {
this.items = [];
this.size = this.items.length;
}
enqueue(element) {
this.items.push(element);
this.size++;
}
dequeue() {
if (this.size === 0) {
return this.size;
}
this.size--;
return this.items.shift();
}
size() {
return this.items.length;
}
}

module.exports = Queue;
19 changes: 18 additions & 1 deletion src/stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,24 @@
4. Add a `pop` method that removes the most recently-added item to the stack
*/
class Stack {

constructor() {
this.items = [];
this.size = this.items.length;
}
push(element) {
this.size++;
return this.items.push(element);
}
pop() {
if (this.size === 0) {
return this.size;
}
this.size--;
return this.items.pop();
}
size() {
return this.items.legnth;
}
}

module.exports = Stack;
44 changes: 22 additions & 22 deletions tests/hash-table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,26 @@ describe('HashTable', () => {
});

/* Extra Credit */
// it('should resize the hash table when > 75% full', () => {
// hashTable.insert('a', true);
// hashTable.insert('b', true);
// hashTable.insert('c', true);
// hashTable.insert('d', true);
// hashTable.insert('e', true);
// hashTable.insert('f', true);
// hashTable.insert('g', true);
// expect(hashTable.limit).toBe(16);
// expect(hashTable.storage.length).toBe(8);
// hashTable.insert('h', true);
// hashTable.insert('i', true);
// hashTable.insert('j', true);
// hashTable.insert('k', true);
// hashTable.insert('l', true);
// hashTable.insert('m', true);
// hashTable.insert('n', true);
// hashTable.insert('o', true);
// expect(hashTable.limit).toBe(32);
// expect(hashTable.storage.length).toBe(16);
// });

it('should resize the hash table when > 75% full', () => {
hashTable.insert('a', true);
hashTable.insert('b', true);
hashTable.insert('c', true);
hashTable.insert('d', true);
hashTable.insert('e', true);
hashTable.insert('f', true);
hashTable.insert('g', true);
expect(hashTable.limit).toBe(16);
expect(hashTable.storage.length).toBe(8);
hashTable.insert('h', true);
hashTable.insert('i', true);
hashTable.insert('j', true);
hashTable.insert('k', true);
hashTable.insert('l', true);
hashTable.insert('m', true);
hashTable.insert('n', true);
hashTable.insert('o', true);
expect(hashTable.limit).toBe(32);
expect(hashTable.storage.length).toBe(16);
});
});
Loading