From 65cec2023ef584e93c12476f1712026e45a91de8 Mon Sep 17 00:00:00 2001 From: Justin Ruiz Date: Thu, 25 Jan 2018 19:38:45 -0600 Subject: [PATCH 1/2] Stack queue and LL done. Had some computer issues and did not get around to hash tables. i am not sure i would have known where to begin anyways. will keep working after. --- src/linked-list.js | 29 ++++++++++++++++++++++++++--- src/queue.js | 15 ++++++++++++++- src/stack.js | 15 ++++++++++++++- 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/linked-list.js b/src/linked-list.js index 267e295..e9d5a3b 100644 --- a/src/linked-list.js +++ b/src/linked-list.js @@ -9,17 +9,40 @@ 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 placeHolder = this.tail; + placeHolder.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; + 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 !== this.head) { + if (value === node.value) { + return true; + } + node = node.next; + } + return false; } } diff --git a/src/queue.js b/src/queue.js index dcf7ad7..1a9f10f 100644 --- a/src/queue.js +++ b/src/queue.js @@ -5,7 +5,20 @@ 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); + return this.size; + } + dequeue(item) { + return this.storage.shift(item); + } } module.exports = Queue; diff --git a/src/stack.js b/src/stack.js index 4312b41..36bf983 100644 --- a/src/stack.js +++ b/src/stack.js @@ -5,7 +5,20 @@ 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); + } } module.exports = Stack; From 5b1f891b4cdc35bb4a19b41cbb0ae75837e5d9f2 Mon Sep 17 00:00:00 2001 From: Justin Ruiz Date: Tue, 30 Jan 2018 17:59:56 -0600 Subject: [PATCH 2/2] worked on some more problems and got a little done on Doubly Linked --- src/doubly-linked-list.js | 31 ++++++++++++++++++++++++++ src/hash-table.js | 47 ++++++++++++++++++++++++++++++++++----- src/linked-list.js | 30 ++++++++++++++----------- src/queue.js | 4 +++- src/stack.js | 20 +++++++++++++++++ 5 files changed, 113 insertions(+), 19 deletions(-) diff --git a/src/doubly-linked-list.js b/src/doubly-linked-list.js index 6373ce3..e3e08e7 100644 --- a/src/doubly-linked-list.js +++ b/src/doubly-linked-list.js @@ -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; + } } diff --git a/src/hash-table.js b/src/hash-table.js index 7edba9e..2a69494 100644 --- a/src/hash-table.js +++ b/src/hash-table.js @@ -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; } } diff --git a/src/linked-list.js b/src/linked-list.js index e9d5a3b..6853738 100644 --- a/src/linked-list.js +++ b/src/linked-list.js @@ -13,31 +13,35 @@ class LinkedList { value, next: null, }; - if (this.tail === null) { - this.head = newNode; + 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; - } - const placeHolder = this.tail; - placeHolder.next = newNode; - 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 (this.head === null) { // if the head is null that means there's no nodes return null; } - const node = this.head; - this.head = node.next; - return node.value; + 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 !== this.head) { - if (value === node.value) { + while (node !== null) { + if (node.value === value) { return true; } node = node.next; diff --git a/src/queue.js b/src/queue.js index 1a9f10f..2d3a6fa 100644 --- a/src/queue.js +++ b/src/queue.js @@ -14,9 +14,11 @@ class Queue { } enqueue(item) { this.storage.push(item); - return this.size; + this.count++; } dequeue(item) { + if (this.count === 0) return null; + this.count--; return this.storage.shift(item); } } diff --git a/src/stack.js b/src/stack.js index 36bf983..388e59b 100644 --- a/src/stack.js +++ b/src/stack.js @@ -19,6 +19,26 @@ class Stack { 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;