Skip to content

Commit 5d392e6

Browse files
committed
chapter 03: [Stacks]
1 parent 8cf2152 commit 5d392e6

40 files changed

+1292
-119
lines changed

.eslintrc.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"class-methods-use-this": 0,
2222
"no-plusplus": 0,
2323
"arrow-parens": 0,
24-
"no-console": 0
24+
"no-console": 0,
25+
"import/prefer-default-export": 0,
26+
"comma-dangle": 0,
27+
"no-underscore-dangle": 0
2528
}
2629
}

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ node_modules
44
coverage
55
.nyc_output
66
coverage.lcov
7+
mochawesome-report/*
8+
dist/js/*
9+
dist/ts/*

README.md

+9-30
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,22 @@ Learning JavaScript Data Structures and Algorithms
44
[![Build Status](https://travis-ci.org/loiane/javascript-datastructures-algorithms.svg?branch=third-edition)](https://travis-ci.org/loiane/javascript-datastructures-algorithms)
55
[![codecov](https://codecov.io/gh/loiane/javascript-datastructures-algorithms/branch/third-edition/graph/badge.svg)](https://codecov.io/gh/loiane/javascript-datastructures-algorithms)
66

7-
Source code of **Learning JavaScript Data Structures and Algorithms** book.
7+
Source code of **Learning JavaScript Data Structures and Algorithms** book, third edition.
88

99
Work in Progress.
1010

1111
## List of available chapters:
1212

1313
* 01: [JavaScript, ECMAScript and TypeScript: a quick overview](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter01)
1414
* 02: [Arrays](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter02)
15+
* 03: [Stacks](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter03)
1516

17+
## Thrid Edition Updates
18+
19+
* Algorithms using ES2015+
20+
* Creation of a Data Structure and Algorithms library that can be used in the browser or with Node.js
21+
* Algorithms tested with Mocha + Chai (test code available in `test` directory)
22+
* TypeScript version of the source code included
1623

1724
## Installing and running the book examples With Node
1825

@@ -22,36 +29,8 @@ Work in Progress.
2229
* To see the examples, run `http-server html` or `npm run serve`. Open your browser `http:\\localhost:8080` to see the book examples
2330
* Or `cd html/chapter01` and run each javascript file with node: `node 02-Variables`
2431

25-
## Running the book examples in the browser
32+
## Running the examples in the browser
2633

2734
* Right click on the html file you would like to see the examples, right click and 'Open with Chrome (or any other browser)'
2835

29-
## List of all available examples:
30-
31-
* 01: JavaScript, ECMAScript and TypeScript: a quick overview
32-
- 01-HelloWorld
33-
- 02-Variables
34-
- 03-Operators
35-
- 04-TruthyFalsy
36-
- 05-EqualsOperators
37-
- 06-ConditionalStatements
38-
- 07-Loops
39-
- 08-Functions
40-
- 10-ObjectOrientedJS
41-
- 11-ES6letconst
42-
- 12-Es6StringTemplates
43-
- 13-ES6ArrowFunctions
44-
- 14-ES6ParameterHandling
45-
- 15-ES6EnhancedObjectProperties
46-
- 16-ES6Classes
47-
- 17-TypeScript
48-
* 02: Arrays
49-
- 01-Introduction
50-
- 02-CreatingAndInitialingArrays
51-
- 03-AddingRemovingElements
52-
- 04-TwoDimensionalMultiDimensional
53-
- 05-ArrayMethods
54-
- 06-ES6Methods
55-
- 07-TypedArrays
56-
5736
Happy Coding!

examples/PacktDataStructuresAlgorithms.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/chapter03/01-Stack.html

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="./../../dist/js/data-structures/stack.js"></script>
9+
<script src="./../../dist/js/data-structures/stack-array.js"></script>
10+
<script type="module" src="01-Stack.js"></script>
11+
</body>
12+
</html>

examples/chapter03/01-Stack.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Stack from './../../src/js/data-structures/stack.js'; // ES2015 modules
2+
import StackArray from './../../src/js/data-structures/stack-array.js'; // ES2015 modules
3+
// const Stack = require('../../dist/js/data-structures/stack'); // for node
4+
// const Stack = stack; // older browsers - remove from html script import: type="module"
5+
6+
const stack = new Stack(); // new StackArray();
7+
8+
// using WeakMap to store Stack items we ensure true privacy
9+
// console.log(Object.getOwnPropertyNames(stack));
10+
// console.log(Object.keys(stack));
11+
12+
console.log('stack.isEmpty() => ', stack.isEmpty()); // outputs true
13+
14+
stack.push(5);
15+
stack.push(8);
16+
17+
console.log('stack after push 5 and 8 => ', stack.toString());
18+
19+
console.log('stack.peek() => ', stack.peek()); // outputs 8
20+
21+
stack.push(11);
22+
23+
console.log('stack.size() after push 11 => ', stack.size()); // outputs 3
24+
console.log('stack.isEmpty() => ', stack.isEmpty()); // outputs false
25+
26+
stack.push(15);
27+
28+
stack.pop();
29+
stack.pop();
30+
31+
console.log('stack.size() after push 15 and pop twice => ', stack.size()); // outputs 2

examples/chapter03/01-StackSymbol.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const _items = Symbol('stackItems');
2+
3+
class Stack {
4+
constructor() {
5+
this[_items] = [];
6+
}
7+
8+
push(element) {
9+
this[_items].push(element);
10+
}
11+
12+
pop() {
13+
return this[_items].pop();
14+
}
15+
16+
peek() {
17+
return this[_items][this[_items].length - 1];
18+
}
19+
20+
isEmpty() {
21+
return this[_items].length === 0;
22+
}
23+
24+
size() {
25+
return this[_items].length;
26+
}
27+
28+
clear() {
29+
this[_items] = [];
30+
}
31+
32+
print() {
33+
console.log(this.toString());
34+
}
35+
36+
toString() {
37+
return this[_items].toString();
38+
}
39+
}
40+
41+
const stack = new Stack();
42+
const objectSymbols = Object.getOwnPropertySymbols(stack);
43+
console.log(objectSymbols.length); // 1
44+
console.log(objectSymbols); // [Symbol()]
45+
console.log(objectSymbols[0]); // Symbol()
46+
stack[objectSymbols[0]].push(1);
47+
stack.print(); // 5, 8, 1
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<title></title>
7+
</head>
8+
9+
<body>
10+
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
11+
<script src="02-BalancedSymbols.js"></script>
12+
</body>
13+
14+
</html>
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const parenthesesChecker = PacktDataStructuresAlgorithms.parenthesesChecker;
2+
3+
console.log('{([])}', parenthesesChecker('{([])}')); // true
4+
console.log('{{([][])}()}', parenthesesChecker('{{([][])}()}')); // true
5+
console.log('[{()]', parenthesesChecker('[{()]')); // false
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
9+
<script src="03-DecimalToBinary.js"></script>
10+
</body>
11+
</html>
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const decimalToBinary = PacktDataStructuresAlgorithms.parenthesesChecker;
2+
const baseConverter = PacktDataStructuresAlgorithms.baseConverter;
3+
4+
// 233 == 11101001
5+
// 2x(10x10) + 3x(10) + 3x(1)
6+
console.log(decimalToBinary(233));
7+
console.log(decimalToBinary(10));
8+
console.log(decimalToBinary(1000));
9+
10+
console.log(baseConverter(100345, 2));
11+
console.log(baseConverter(100345, 8));
12+
console.log(baseConverter(100345, 16));
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script src="./../PacktDataStructuresAlgorithms.min.js"></script>
9+
<script src="04-TowerOfHanoi.js"></script>
10+
</body>
11+
</html>

examples/chapter03/04-TowerOfHanoi.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const hanoiStack = PacktDataStructuresAlgorithms.hanoiStack;
2+
const hanoi = PacktDataStructuresAlgorithms.hanoi;
3+
4+
console.log(hanoiStack(3));
5+
6+
console.log(hanoi(3, 'source', 'helper', 'dest'));

examples/chapter03/index.html

-11
This file was deleted.

examples/chapter03/index.js

-8
This file was deleted.

examples/index.html

+15-4
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
<a href="#scroll-tab-1" class="mdl-layout__tab is-active">01</a>
3939
<a href="#scroll-tab-2" class="mdl-layout__tab">02</a>
4040
<a href="#scroll-tab-3" class="mdl-layout__tab">03</a>
41-
<a href="#scroll-tab-3" class="mdl-layout__tab">04</a>
42-
<a href="#scroll-tab-3" class="mdl-layout__tab">05</a>
43-
<a href="#scroll-tab-3" class="mdl-layout__tab">06</a>
41+
<a href="#scroll-tab-4" class="mdl-layout__tab">04</a>
42+
<a href="#scroll-tab-4" class="mdl-layout__tab">05</a>
43+
<a href="#scroll-tab-4" class="mdl-layout__tab">06</a>
4444
</div>
4545
</header>
4646
<main class="mdl-layout__content">
@@ -95,9 +95,20 @@
9595
</section>
9696
<section class="mdl-layout__tab-panel" id="scroll-tab-3">
9797
<div class="page-content">
98-
Soon.
98+
<div class="page-content mdl-layout--fixed-drawer">
99+
<div class="mdl-layout__drawer is-visible">
100+
<nav class="mdl-navigation">
101+
<a class="mdl-navigation__link" href="./chapter03/01-Stack.html">01-Stack</a>
102+
</nav>
103+
</div>
104+
</div>
99105
</div>
100106
</section>
107+
<section class="mdl-layout__tab-panel" id="scroll-tab-4">
108+
<div class="page-content">
109+
Soon.
110+
</div>
111+
</section>
101112
</main>
102113
</div>
103114
</body>

package.json

+9-4
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@
1414
},
1515
"homepage": "https://github.com/loiane/javascript-datastructures-algorithms",
1616
"scripts": {
17-
"clean": "rm -rf ./dist ./coverage ./.nyc_output ./coverage.lcov",
17+
"clean": "rm -rf ./dist ./coverage ./.nyc_output ./coverage.lcov ./mochawesome-report",
1818
"build:js": "babel src/js -d dist/js",
1919
"build:ts": "tsc -p ./ --rootDir ./src/ts",
2020
"build": "npm run build:js && npm run build:ts",
2121
"lint:js": "eslint src/js && eslint test/js",
2222
"lint:ts": "tslint -c tslint.json 'src/ts/**/*.ts' && tslint -c tslint.json 'test/ts/**/*.ts'",
2323
"lint": "npm run lint:js && npm run lint:ts",
24-
"test:js": "mocha --compilers js:babel-core/register ./test/js/**/*.spec.js",
24+
"test:js": "mocha --compilers js:babel-core/register ./test/js/**/*.spec.js --reporter mochawesome",
2525
"test:ts": "mocha -r ts-node/register --recursive ./test/ts/**/*.spec.ts",
2626
"test": "npm run test:js && npm run test:ts",
2727
"dev": "npm run clean && npm run lint && npm run generate-report",
2828
"coverage": "npm run generate-report && nyc report --reporter=text-lcov > coverage.lcov && codecov",
2929
"generate-report": "nyc --report-dir coverage npm run test && nyc report --reporter=text",
30-
"go": "npm run clean && npm run lint && npm run build && npm run test"
30+
"go": "npm run clean && npm run lint && npm run build && npm run test",
31+
"webpack": "webpack --env build"
3132
},
3233
"nyc": {
3334
"include": [
@@ -55,6 +56,7 @@
5556
"babel-cli": "^6.26.0",
5657
"babel-core": "^6.26.0",
5758
"babel-eslint": "^8.0.0",
59+
"babel-loader": "^7.1.2",
5860
"babel-plugin-add-module-exports": "^0.2.1",
5961
"babel-plugin-transform-es2015-modules-umd": "^6.24.1",
6062
"babel-preset-env": "^1.6.0",
@@ -65,9 +67,12 @@
6567
"eslint-plugin-import": "^2.7.0",
6668
"istanbul": "^v1.1.0-alpha.1",
6769
"mocha": "^3.5.0",
70+
"mochawesome": "^2.3.1",
6871
"nyc": "^11.2.1",
6972
"ts-node": "^3.3.0",
7073
"tslint": "^5.7.0",
71-
"typescript": "^2.5.2"
74+
"typescript": "^2.5.2",
75+
"webpack": "^3.6.0",
76+
"yargs": "^9.0.1"
7277
}
7378
}

src/js/data-structures/stack-array.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// @ts-check
2+
3+
export default class StackArray {
4+
constructor() {
5+
this._items = [];
6+
}
7+
push(element) {
8+
this._items.push(element);
9+
}
10+
11+
pop() {
12+
return this._items.pop();
13+
}
14+
15+
peek() {
16+
return this._items[this._items.length - 1];
17+
}
18+
19+
isEmpty() {
20+
return this._items.length === 0;
21+
}
22+
23+
size() {
24+
return this._items.length;
25+
}
26+
27+
clear() {
28+
this._items = [];
29+
}
30+
31+
toArray() {
32+
return this._items;
33+
}
34+
35+
toString() {
36+
return this._items.toString();
37+
}
38+
}

0 commit comments

Comments
 (0)