diff --git a/src/linked-list.js b/src/linked-list.js index 267e295..8e24093 100644 --- a/src/linked-list.js +++ b/src/linked-list.js @@ -9,17 +9,57 @@ 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 = { + next: null, + value, + }; + // check if a head already exists + if (this.head === null) { + // our list is empty + this.head = newNode; + this.tail = newNode; + } else { + // list is not empty + this.tail.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() { - + // there may be nothing in our list + if (this.head === null) return; + // there may be one node in our list + if (this.head.next === null) { + // create a new reference to our current head, so that we can return the value + const head = this.head; + // set this.head to null + this.head = null; + // set this.tail to null + this.tail = null; + // return the value from our head + return head.value; + } + // there may be multiple nodes in our list + const head = this.head; + this.head = this.head.next; + return head.value; } // Checks the linked list for the given value // Returns true if the the value is found in the list, false otherwise contains(value) { - + // if the linked list is emtpy, then automatically return false + if (this.head === null) return false; + const searchLinkedList = (node) => { + // base case 1: if the current node we're looking at has the value we're looking for + if (node.value === value) return true; + // base case 2: we've reached the end of our linked list + if (node.next === null) return false; + // call our function recursively, passing it the next node in our linked list + return searchLinkedList(node.next); + }; + // call our recursive function on our first linked list node + return searchLinkedList(this.head); } } diff --git a/src/queue.js b/src/queue.js index dcf7ad7..c7fdd03 100644 --- a/src/queue.js +++ b/src/queue.js @@ -1,3 +1,4 @@ +/* eslint-disable */ /* 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 @@ -5,7 +6,59 @@ 4. Add a `dequeue` method that removes the item in the queue that was added earliest */ class Queue { + constructor() { + this.length = 0; -} + this.head = null; + this.tail = null; + } + enqueue(value) { + this.length++; + const newItem = { + value, + next: null, + }; + + if (!this.head) { + this.head = newItem; + this.tail = newItem; + } else { + this.tail.next = newItem; + this.tail = newItem; + } + } + dequeue() { + if (this.length === 0) return 0; + + this.length--; -module.exports = Queue; + if (!this.head) return null; + if (!this.head.next) this.tail = null; + + const returnVal = this.head.value; + this.head = this.head.next; + return returnVal; + } + get size() { + return this.length; + } +} +// class Queue { +// constructor() { +// this.queue = []; +// this.size = 0; +// } +// enqueue(item) { +// this.queue.push(item); +// this.size++; +// } +// dequeue(item) { +// if (this.queue.length === 0) return 'empty'; +// this.size--; +// return this.queue.shift(); +// } +// size() { +// if (this.size < 0) return 0; +// return this.size; +// } +// } diff --git a/src/stack.js b/src/stack.js index 4312b41..8881533 100644 --- a/src/stack.js +++ b/src/stack.js @@ -5,7 +5,40 @@ 4. Add a `pop` method that removes the most recently-added item to the stack */ class Stack { + constructor() { + this.length = 0; + this.head = null; + this.tail = null; + } + push(value) { + this.length++; + const newItem = { + value, + next: null, + }; + + if (this.head) { + this.head = newItem; + this.tail = newItem; + } + } + pop() { + if (this.length === 0) return 0; + + this.length--; + + if (!this.head) return null; + if (!this.head.next) this.tail = null; + + const returnVal = this.head.value; + this.head = this.head.next; + return returnVal; + } + get size() { + return this.length; + } } + module.exports = Stack;