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
31 changes: 31 additions & 0 deletions src/doubly-linked-list.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
// need to add to tail and head, remove from tail and head, contains.
class DoublyLinkedList {
constructor() {
this.head = null;
this.tail = null;
}
addToTail(value) = {
const newTail = {
value,
next = null;
previous = null;
}
if(this.tail === null) {
this.head = newTail;
this.tail = newTail;
}
const oldTail = this.tail;
oldTail.next = newTail;
this.tail = newTail;
newTail.previous = oldTail;
}
addToHead(value) = {
const newHead = {
value,
next = null;
previous = null;
}
if(this.head === null) {
this.head = newHead;
this.tail = newHead;
}
const oldHead = this.head;
oldHead.previous = newHead;
this.head = newHead;
this.head.next = oldHead;
}
}
47 changes: 42 additions & 5 deletions src/hash-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,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) {

const hashIndex = getIndexBelowMax(key.toString(), this.limit) // get the index (hash table location)
let bucket = this.storage.get(hashIndex) ? [] : this.storage.get(hashIndex); // fetch bucket, with limited array, pass hash
// need to avoid collisions...
let newTuple = [key, value];
let replaced = false;
bucket = bucket.map((tuple) => {
if (newTuple[0] === key) {
replaced = true;
return newtuple;
}
return tuple;
})
if (!replaced) {
bucket.push(tuple);
}
this.storage.set(hashIndex, 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) {

}

if (this.storage.length === 0) return;
const hashIndex = getIndexBelowMax(key.toString(), this.limit)
let bucket = this.storage.get(hashIndex);
if(bucket === undefined || bucket.length === 0) return;

for (let i = 0; i < bucket.length; i++) {
let tuple = item;
if (tuple[0] === key) {
bucket.splice(i, 1);
}
}
this.storage.set(hashIndex, 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) {

if (this.storage.length === 0) return undefined;
const hashIndex = getIndexBelowMax(key.toString(), this.limit)
let bucket = this.storage.get(hashIndex);
if(bucket === undefined || bucket.length === 0) return undefined;

for (let i = 0; i < bucket.length; i++) {
let tuple = bucket[i];
if (tuple[0] === key) {
return tuple[1];
}
}
return undefined;
}
}

Expand Down
33 changes: 30 additions & 3 deletions src/linked-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,44 @@ 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) { // means there's no node
this.head = newNode; // set head to newNode (value of whatever) and tail to newNode (next to null)
this.tail = newNode;
return; // exit because we only needed to create a new node
} // otherwise...you need to add to tail
const placeHolder = this.tail; // let a placeHolder have whatever value of the tail.
placeHolder.next = newNode; // set the next property to newNode (next to null)
this.tail = newNode; // set this.tail to have value of newNode (value of whatever)
}
// 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) { // if the head is null that means there's no nodes
return null;
}
const current = this.head; // otherwise, define a node and set it to this.head(node will have a value of whatever)
this.head = current.next; // set the value of whatever to be the node's pointer
if (this.head === null) { // if that value is null,
this.tail = current; // define the tail to inherit node, which is a value of whatever. basically you're just moving pointers around
}
return current.value; // want to see the value of the node you removed
}

// 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
17 changes: 16 additions & 1 deletion src/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,22 @@
4. Add a `dequeue` method that removes the item in the queue that was added earliest
*/
class Queue {

constructor() {
this.storage = [];
this.count = 0;
}
get size() {
return this.storage.length;
}
enqueue(item) {
this.storage.push(item);
this.count++;
}
dequeue(item) {
if (this.count === 0) return null;
this.count--;
return this.storage.shift(item);
}
}

module.exports = Queue;
33 changes: 33 additions & 0 deletions src/stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,40 @@
4. Add a `pop` method that removes the most recently-added item to the stack
*/
class Stack {
constructor() {
this.storage = [];
this.count = 0;
}
get size() {
return this.storage.length;
}
push(item) {
this.storage.push(item);
this.count++;
}
pop(item) {
return this.storage.pop(item);
}

// return the last item added to the stack.
/* peek(item) {
return this.storage[this.storage.length - 1];
}

// verify if the stack is empty or not
isEmpty() {
return size === 0;

//clear all elements
clear() {
this.storage = [];
// or call pop() until it's clear
}

//print out items
print(item) {
return this.storage[item];
} */
}

module.exports = Stack;