Skip to content
This repository has been archived by the owner on May 8, 2019. It is now read-only.

Commit

Permalink
Stacks, Queues, Tree, Sorts (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
DennySoul committed Jul 13, 2018
1 parent 813224e commit fd03aca
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 62 deletions.
10 changes: 10 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "airbnb",
"rules": {
"max-len": 0,
"func-names": "off",
"indent": 0,
"nonblock-statement-body-position": "off",
"curly": 0
}
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules

test*
test*
/ds.js
117 changes: 102 additions & 15 deletions data-structures/linkedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,68 +77,155 @@ Reimplement stack and queue data structures using linked lists.
*/


// PART 1

function Node(value) {
this.next = null;
this.value = value;
this.next = null;
this.value = value;
}

function LinkedList(headValue) {
if (headValue === undefined) console.log('Must provide value for first node');
this.head = new Node(headValue);
if (headValue === undefined) console.log('Must provide value for first node');
this.head = new Node(headValue);
}

LinkedList.prototype.forEach = function(callback) {
// implement me...
// implement me...
let node = this.head;

while (node) {
callback(node.value);
node = node.next;
}
};
// Time complexity:

LinkedList.prototype.print = function() {
// implement me...
// implement me...
let node = this.head;
const values = [];

while (node) {
values.push(node.value);
node = node.next;
}

return values;
};
// Time complexity:

LinkedList.prototype.insertAfter = function(node, value) {
// implement me...
// implement me...
const oldNext = node.next;
const newNext = new Node(value);

node.next = newNext;
newNext.next = oldNext;

if (this.tail === node)
this.tail = newNext;

return newNext;
};
// Time complexity:

LinkedList.prototype.removeAfter = function(node) {
// implement me...
// implement me...
const currentNode = this.findNode(node.value);
const tmp = currentNode.next;

currentNode.next = node;
node.next = tmp;
};
// Time complexity:

LinkedList.prototype.insertHead = function(value) {
// implement me...
// implement me...
const tmp = this.head;
this.head = new Node(value);
this.head.next = tmp;
};
// Time complexity:

LinkedList.prototype.removeHead = function() {
// implement me...
// implement me...
const tmp = this.head.next;
this.head = tmp;
}

LinkedList.prototype.findNode = function(value) {
// implement me...
// implement me...
let node = this.head;

while (node.value !== value) {
node = node.next;

if (node.value === value)
return node;
}
};
// Time complexity:

LinkedList.prototype.appendToTail = function(value) {
// implement me...
// implement me...
let existingNode = this.head;

while (existingNode.next !== null) {
existingNode = existingNode.next;

if (existingNode.next === null) {
const tmp = existingNode.next;

existingNode.next = node;
node.next = null;
break;
}
}
};
// Time complexity:


// PART 2:

LinkedList.prototype.insertBefore = function(node, value) {
// implement me...
// implement me...
let existingNode = this.head;
while (existingNode.next !== node) {
existingNode = existingNode.next;

if (existingNode.next === node) {
const tmp = existingNode.next;

existingNode.next = node;
node.next = tmp;
break;
}
}

const currentNode = this.findNode(node.value);
const tmp = currentNode.next;
const newNode = new Node(value);

newNodet.next = tmp;
};
// Time complexity:

LinkedList.prototype.removeBefore = function(node) {
// implement me...
// implement me...
// let existingNode = this.head;
// let parent = existingNode.next;
//
// while (parent.next !== node) {
// existingNode = existingNode.next;
//
// if (parent.next === node) {
// const tmp = parent.next;
//
// existingNode.next = node;
// node.next = tmp;
// break;
// }
// }
};
// Time complexity:

Expand Down
39 changes: 15 additions & 24 deletions data-structures/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,26 @@ queue values - (first)2-5-7-3-6-9(last)
myQueue.until(7)
=> 3
What's the time complexity?
*/

function Queue(capacity) {
// implement me...
}

Queue.prototype.enqueue = function(value) {
// implement me...
};
// Time complexity:
class Queue {
constructor() {
this.size = 0;
this.storage = {};
}

Queue.prototype.dequeue = function() {
// implement me...
};
// Time complexity:
enqueue(newItem) {
this.storage[this.size] = (newItem);
}

Queue.prototype.peek = function() {
// implement me...
};

Queue.prototype.count = function() {
// implement me...
};
// Time complexity:
dequeue() {
return this.storage[0];
}

size() {
return this.size;
}
}


/*
Expand Down
36 changes: 15 additions & 21 deletions data-structures/stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ Similiar to pop, but do not remove element from collection
myStack.count()
=> number of elements in stack
*** Additional Exercises:
Modify your stack to take a max capacity and return a string if you try to add an element when there's no more room:
Expand All @@ -46,32 +45,27 @@ myStack.until(7)
=> 4
What's the time complexity?
*/

function Stack(capacity) {
// implement me...
}
class Stack {
constructor() {
this.storage = [];
}

Stack.prototype.push = function(value) {
// implement me...
};
// Time complexity:
push(newItem) {
this.storage.push(newItem);
return this.storage;
}

Stack.prototype.pop = function() {
// implement me...
};
// Time complexity:
pop() {
return this.storage.pop();
}

Stack.prototype.peek = function() {
// implement me...
};
// Time complexity:
size() {
return this.storage.length;
}
}

Stack.prototype.count = function() {
// implement me...
};
// Time complexity:


Expand Down
22 changes: 21 additions & 1 deletion data-structures/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,36 @@ https://en.wikipedia.org/wiki/Trie

function Tree (value) {
// implement me...
this.value = value;
this.children = [];
}

Tree.prototype.addChild = function(value) {
// implement me...
const child = new Tree(value);
this.children.push(child);

return child;
};
// Time complexity:


Tree.prototype.contains = function(value) {
// implement me...
const loop = (value) => {
this.children.forEach((childNode) => {
if (childNode.children.length === 0) {
return false;
}

if (childNode.value === value)
return true;

if (childNode.children.length > 0)
return loop(value);
});
};

return loop(value);
};
// Time complexity:

Expand Down
26 changes: 26 additions & 0 deletions sorting-algorithms/bubble.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,29 @@ Variants:
(https://en.wikipedia.org/wiki/Cocktail_sort)
*/

const bubbleSort = (list = []) => {
const listLength = list.length;
let currentElement;
let nextElement;
let previousElement;
let i = 0;

while (listLength >= 0) {
currentElement = list[i];
nextElement = list[i + 1];

for (let i = 0; i < listLength; i++) {
if (currentElement > nextElement) {
const tmp = currentElement;

list[i] = nextElement;
list[i + 1] = tmp;
}
}

i--;
}

return list;
};
Loading

0 comments on commit fd03aca

Please sign in to comment.