diff --git a/.gitignore b/.gitignore index 6fee6d3a..2458e8b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .idea/* -*.log \ No newline at end of file +*.log +node_modules +coverage diff --git a/README.md b/README.md index 9712708a..64771d99 100644 --- a/README.md +++ b/README.md @@ -3,26 +3,41 @@ Learning JavaScript Data Structures and Algorithms Source code of **Learning JavaScript Data Structures and Algorithms** book. -Book link: +| 1st edition | 2nd edition | +| ------------- |:-------------:| +|  |  | +| [Book link](http://amzn.to/1Y1OWPx)| [Book link](http://amzn.to/1TSkcA1)| + +Book link - first edition: - [Packt](https://www.packtpub.com/application-development/learning-javascript-data-structures-and-algorithms) - - [Amazon](http://www.amazon.com/gp/product/1783554878/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=1783554878&linkCode=as2&tag=loiagron-20&linkId=7VBQ7OMJ47XY7MUV) + - [Amazon](http://amzn.to/1Y1OWPx) + - [Chinese version](http://www.ituring.com.cn/book/1613) + - [Korean version](http://www.acornpub.co.kr/book/javascript-data-structure) +Book link - second edition: + - [Packt](https://www.packtpub.com/web-development/learning-javascript-data-structures-and-algorithms-second-edition) + - [Amazon](http://amzn.to/1TSkcA1) + - [Brazilian Portuguese version](https://novatec.com.br/livros/estruturas-de-dados-algoritmos-em-javascript/) -###List of Chapters: +### List of Chapters: -* 1: JAVASCRIPT – A QUICK OVERVIEW -* 2: ARRAYS -* 3: STACKS -* 4: QUEUES -* 5: LINKED LISTS -* 6: SETS -* 7: DICTIONARIES AND HASHES -* 8: TREES -* 9: GRAPHS -* 10: SORTING AND SEARCHING ALGORITHMS -* 11: More about Algorithms (Recursion, Dynamic Programing, Greedy Algorithms and Big-O Notation) - Extra chapter that can be donwloaded from [Packt](https://www.packtpub.com/application-development/learning-javascript-data-structures-and-algorithms) website -* Apendix A: Big-O Cheat Sheet - Extra chapter that can be donwloaded from [Packt](https://www.packtpub.com/application-development/learning-javascript-data-structures-and-algorithms) website - -###Found an issue or have a question? +* 01: [JavaScript: a quick overview](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter01) +* 02: [Arrays](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter02) +* 03: [Stacks](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter03) +* 04: [Queues](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter04) +* 05: [Linked Lists](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter05) +* 06: [Sets](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter06) +* 07: [Dictionaries and Hashes](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter07) +* 08: [Trees](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter08) +* 09: [Graphs](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter09) +* 10: [Sorting and searching algorithms](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter10) +* 11: [Pattern of algorithms](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter11) +* 12: [Algorithm Complexity](https://github.com/loiane/javascript-datastructures-algorithms/tree/second-edition/chapter12) + +### First Edition source code: + +Please refer to [this link](https://github.com/loiane/javascript-datastructures-algorithms/tree/master) + +### Found an issue or have a question? Please create an [Issue](https://github.com/loiane/javascript-datastructures-algorithms/issues) or [Pull Request](https://github.com/loiane/javascript-datastructures-algorithms/pulls) diff --git a/chapter01/14-ES6ParameterHandling.js b/chapter01/14-ES6ParameterHandling.js index 706c2059..af23585c 100644 --- a/chapter01/14-ES6ParameterHandling.js +++ b/chapter01/14-ES6ParameterHandling.js @@ -14,7 +14,7 @@ function sum2 (x, y, z) { z = 3; return x + y + z; }; -console.log(sum2(4,2)); //outpus 10 +console.log(sum2(4,2)); //outpus 9 //******* EcmaScript 6: spread operator ('...') var params = [3, 4, 5]; @@ -30,8 +30,8 @@ function restParamaterFunction (x, y, ...a) { console.log(restParamaterFunction(1, 2, "hello", true, 7)); // outputs 9; //code above is the same as ES5: -function restParamaterFunction (x, y) { +function restParamaterFunction2 (x, y) { var a = Array.prototype.slice.call(arguments, 2); return (x + y) * a.length; }; -console.log(restParamaterFunction2(1, 2, "hello", true, 7)); \ No newline at end of file +console.log(restParamaterFunction2(1, 2, "hello", true, 7)); diff --git a/chapter02/06-ES6Methods.js b/chapter02/06-ES6Methods.js index 60468aa4..bf4eb36a 100644 --- a/chapter02/06-ES6Methods.js +++ b/chapter02/06-ES6Methods.js @@ -40,7 +40,7 @@ console.log(aKeys.next()); // {value: 1, done: false } console.log(aKeys.next()); // {value: 2, done: false } console.log('Array.values'); -//let aValues = numbers.values(); +let aValues = numbers.values(); console.log(aValues.next()); // {value: 1, done: false } done false means iterator has more values console.log(aValues.next()); // {value: 2, done: false } console.log(aValues.next()); // {value: 3, done: false } diff --git a/chapter03/01-Stack3.js b/chapter03/01-Stack3.js index fd490ffa..d13a87dd 100644 --- a/chapter03/01-Stack3.js +++ b/chapter03/01-Stack3.js @@ -11,13 +11,11 @@ let Stack3 = (function () { push(element){ let s = items.get(this); s.push(element); - items.set(this, s); } pop(){ let s = items.get(this); let r = s.pop(); - items.set(this, s); return r; } diff --git a/chapter03/02-UsingStacks.js b/chapter03/02-UsingStacks.js index a84703a3..9876e892 100644 --- a/chapter03/02-UsingStacks.js +++ b/chapter03/02-UsingStacks.js @@ -1,4 +1,4 @@ -let stack = new Stack2(); +let stack = new Stack3(); console.log(stack.isEmpty()); //outputs true stack.push(5); stack.push(8); @@ -15,10 +15,10 @@ let stack = new Stack2(); //how to ensure true privacy //in case using Stack 2 uncomment code below -let objectSymbols = Object.getOwnPropertySymbols(stack); +/*let objectSymbols = Object.getOwnPropertySymbols(stack); console.log(objectSymbols.length); // 1 console.log(objectSymbols); // [Symbol()] console.log(objectSymbols[0]); // Symbol() stack[objectSymbols[0]].push(1); - stack.print(); //5, 8, 1 \ No newline at end of file + stack.print(); //5, 8, 1*/ \ No newline at end of file diff --git a/chapter04/01-Queue2.js b/chapter04/01-Queue2.js index c35ea102..d4bc9891 100644 --- a/chapter04/01-Queue2.js +++ b/chapter04/01-Queue2.js @@ -11,13 +11,11 @@ let Queue2 = (function () { enqueue(element) { let q = items.get(this); q.push(element); - items.set(this, q) } dequeue() { let q = items.get(this); let r = q.shift(); - items.set(this, q); return r; } diff --git a/chapter04/02-UsingQueues.js b/chapter04/02-UsingQueues.js index 1b3f58ae..5411bad6 100644 --- a/chapter04/02-UsingQueues.js +++ b/chapter04/02-UsingQueues.js @@ -1,4 +1,4 @@ -let queue = new Queue(); +let queue = new Queue2(); console.log(queue.isEmpty()); //outputs true queue.enqueue("John"); queue.enqueue("Jack"); diff --git a/chapter04/03-PriorityQueue.js b/chapter04/03-PriorityQueue.js index f9071ddc..3b029e4d 100644 --- a/chapter04/03-PriorityQueue.js +++ b/chapter04/03-PriorityQueue.js @@ -50,7 +50,5 @@ let priorityQueue = new PriorityQueue(); priorityQueue.enqueue("John", 2); priorityQueue.enqueue("Jack", 1); priorityQueue.enqueue("Camila", 1); -priorityQueue.enqueue("Maxwell", 2); -priorityQueue.enqueue("Ana", 3); priorityQueue.print(); diff --git a/chapter05/01-Linked-List.js b/chapter05/01-LinkedList.js similarity index 88% rename from chapter05/01-Linked-List.js rename to chapter05/01-LinkedList.js index d4f4637f..3e52bc80 100644 --- a/chapter05/01-Linked-List.js +++ b/chapter05/01-LinkedList.js @@ -1,17 +1,17 @@ function LinkedList() { - var Node = function(element){ + let Node = function(element){ this.element = element; this.next = null; }; - var length = 0; - var head = null; + let length = 0; + let head = null; this.append = function(element){ - var node = new Node(element), + let node = new Node(element), current; if (head === null){ //first node on list @@ -37,7 +37,7 @@ function LinkedList() { //check for out-of-bounds values if (position >= 0 && position <= length){ - var node = new Node(element), + let node = new Node(element), current = head, previous, index = 0; @@ -70,7 +70,7 @@ function LinkedList() { //check for out-of-bounds values if (position > -1 && position < length){ - var current = head, + let current = head, previous, index = 0; @@ -100,13 +100,13 @@ function LinkedList() { this.remove = function(element){ - var index = this.indexOf(element); + let index = this.indexOf(element); return this.removeAt(index); }; this.indexOf = function(element){ - var current = head, + let current = head, index = 0; while (current) { @@ -134,11 +134,11 @@ function LinkedList() { this.toString = function(){ - var current = head, + let current = head, string = ''; while (current) { - string += current.element + (current.next ? '\n' : ''); + string += current.element + (current.next ? ', ' : ''); current = current.next; } return string; diff --git a/chapter05/01-LinkedList2.js b/chapter05/01-LinkedList2.js new file mode 100644 index 00000000..98452857 --- /dev/null +++ b/chapter05/01-LinkedList2.js @@ -0,0 +1,170 @@ +let LinkedList2 = (function () { + + class Node { + constructor(element){ + this.element = element; + this.next = null; + } + } + + const length = new WeakMap(); + const head = new WeakMap(); + + class LinkedList2 { + + constructor () { + length.set(this, 0); + head.set(this, null); + } + + append(element) { + + let node = new Node(element), + current; + + if (this.getHead() === null) { //first node on list + head.set(this, node); + } else { + + current = this.getHead(); + + //loop the list until find last item + while (current.next) { + current = current.next; + } + + //get last item and assign next to added item to make the link + current.next = node; + } + + //update size of list + let l = this.size(); + l++; + length.set(this, l); + } + + insert(position, element) { + + //check for out-of-bounds values + if (position >= 0 && position <= this.size()) { + + let node = new Node(element), + current = this.getHead(), + previous, + index = 0; + + if (position === 0) { //add on first position + + node.next = current; + head.set(this, node); + + } else { + while (index++ < position) { + previous = current; + current = current.next; + } + node.next = current; + previous.next = node; + } + + //update size of list + let l = this.size(); + l++; + length.set(this, l); + + return true; + + } else { + return false; + } + } + + removeAt(position) { + + //check for out-of-bounds values + if (position > -1 && position < this.size()) { + + let current = this.getHead(), + previous, + index = 0; + + //removing first item + if (position === 0) { + head.set(this, current.next); + } else { + + while (index++ < position) { + + previous = current; + current = current.next; + } + + //link previous with current's next - skip it to remove + previous.next = current.next; + } + + let l = this.size(); + l--; + length.set(this, l); + + return current.element; + + } else { + return null; + } + } + + remove(element) { + + let index = this.indexOf(element); + return this.removeAt(index); + } + + indexOf(element) { + + let current = this.getHead(), + index = 0; + + while (current) { + if (element === current.element) { + return index; + } + index++; + current = current.next; + } + + return -1; + } + + isEmpty() { + return this.size() === 0; + } + + size() { + return length.get(this); + } + + getHead() { + return head.get(this); + } + + toString() { + + let current = this.getHead(), + string = ''; + + while (current) { + string += current.element + (current.next ? ', ' : ''); + current = current.next; + } + return string; + + } + + print() { + console.log(this.toString()); + } + } + + return LinkedList2; +})(); diff --git a/chapter05/02-UsingLinkedLists.html b/chapter05/02-UsingLinkedLists.html index da977b75..97210e70 100644 --- a/chapter05/02-UsingLinkedLists.html +++ b/chapter05/02-UsingLinkedLists.html @@ -5,7 +5,8 @@