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
46 changes: 43 additions & 3 deletions src/linked-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
57 changes: 55 additions & 2 deletions src/queue.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,64 @@
/* 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
3. Add an `enqueue` method that accepts an item as input and adds it to the storage structure
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;
// }
// }
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.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;