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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 9 additions & 30 deletions
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

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/chapter03/01-Stack.html

Lines changed: 12 additions & 0 deletions
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

Lines changed: 31 additions & 0 deletions
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

Lines changed: 47 additions & 0 deletions
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
Lines changed: 14 additions & 0 deletions
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>
Lines changed: 5 additions & 0 deletions
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
Lines changed: 11 additions & 0 deletions
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>

0 commit comments

Comments
 (0)