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
103 changes: 103 additions & 0 deletions src/doubly-linked-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
42 changes: 39 additions & 3 deletions src/hash-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,64 @@ 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
// 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) {
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];
}
}
}

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

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() {
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;
21 changes: 19 additions & 2 deletions src/stack.js
Original file line number Diff line number Diff line change
@@ -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;