Skip to content

Commit ea30d2c

Browse files
committed
chapter 03: [Stacks]
1 parent 95fe4fa commit ea30d2c

File tree

7 files changed

+105
-52
lines changed

7 files changed

+105
-52
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ coverage.lcov
77
mochawesome-report/*
88
dist/js/*
99
dist/ts/*
10+
snippet.js

examples/chapter03/01-Stack.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Stack from './../../src/js/data-structures/stack.js'; // ES2015 modules
2-
import StackArray from './../../src/js/data-structures/stack-array.js'; // ES2015 modules
2+
// import StackArray from './../../src/js/data-structures/stack-array.js'; // ES2015 modules
33
// const Stack = require('../../dist/js/data-structures/stack'); // for node
44
// const Stack = stack; // older browsers - remove from html script import: type="module"
55

@@ -8,6 +8,7 @@ const stack = new Stack(); // new StackArray();
88
// using WeakMap to store Stack items we ensure true privacy
99
// console.log(Object.getOwnPropertyNames(stack));
1010
// console.log(Object.keys(stack));
11+
// console.log(stack.items);
1112

1213
console.log('stack.isEmpty() => ', stack.isEmpty()); // outputs true
1314

examples/chapter03/01-StackWeakMap.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// @ts-check
2+
3+
4+
const _items = new WeakMap();
5+
const _count = new WeakMap();
6+
7+
class Stack {
8+
constructor() {
9+
_count.set(this, 0);
10+
_items.set(this, {});
11+
}
12+
13+
push(element) {
14+
const items = _items.get(this);
15+
const count = _count.get(this);
16+
items[count] = element;
17+
_count.set(this, count + 1);
18+
}
19+
20+
pop() {
21+
if (this.isEmpty()) {
22+
return undefined;
23+
}
24+
const items = _items.get(this);
25+
let count = _count.get(this);
26+
count--;
27+
_count.set(this, count);
28+
const result = items[count];
29+
delete items[count];
30+
return result;
31+
}
32+
33+
peek() {
34+
if (this.isEmpty()) {
35+
return undefined;
36+
}
37+
const items = _items.get(this);
38+
const count = _count.get(this);
39+
return items[count - 1];
40+
}
41+
42+
isEmpty() {
43+
return _count.get(this) === 0;
44+
}
45+
46+
size() {
47+
return _count.get(this);
48+
}
49+
50+
clear() {
51+
/* while (!this.isEmpty()) {
52+
this.pop();
53+
} */
54+
_count.set(this, 0);
55+
_items.set(this, {});
56+
}
57+
58+
toString() {
59+
if (this.isEmpty()) {
60+
return '';
61+
}
62+
const items = _items.get(this);
63+
const count = _count.get(this);
64+
let objString = `${items[0]}`;
65+
for (let i = 1; i < count; i++) {
66+
objString = `${objString},${items[i]}`;
67+
}
68+
return objString;
69+
}
70+
}

examples/chapter03/03-DecimalToBinary.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ const baseConverter = PacktDataStructuresAlgorithms.baseConverter;
33

44
// 233 == 11101001
55
// 2x(10x10) + 3x(10) + 3x(1)
6-
console.log(decimalToBinary(233));
7-
console.log(decimalToBinary(10));
8-
console.log(decimalToBinary(1000));
6+
console.log(decimalToBinary(233)); // 11101001
7+
console.log(decimalToBinary(10)); // 1010
8+
console.log(decimalToBinary(1000)); // 1111101000
99

10-
console.log(baseConverter(100345, 2));
11-
console.log(baseConverter(100345, 8));
12-
console.log(baseConverter(100345, 16));
10+
console.log(baseConverter(100345, 2)); // 11000011111111001
11+
console.log(baseConverter(100345, 8)); // 303771
12+
console.log(baseConverter(100345, 16)); // 187F9
13+
console.log(baseConverter(100345, 35)); // 2BW0

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"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",
27-
"dev": "npm run clean && npm run lint && npm run generate-report",
27+
"dev": "npm run clean && npm run lint && npm run webpack && 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",
3030
"go": "npm run clean && npm run lint && npm run build && npm run test",

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,37 @@
22

33
export default class StackArray {
44
constructor() {
5-
this._items = [];
5+
this.items = [];
66
}
77
push(element) {
8-
this._items.push(element);
8+
this.items.push(element);
99
}
1010

1111
pop() {
12-
return this._items.pop();
12+
return this.items.pop();
1313
}
1414

1515
peek() {
16-
return this._items[this._items.length - 1];
16+
return this.items[this.items.length - 1];
1717
}
1818

1919
isEmpty() {
20-
return this._items.length === 0;
20+
return this.items.length === 0;
2121
}
2222

2323
size() {
24-
return this._items.length;
24+
return this.items.length;
2525
}
2626

2727
clear() {
28-
this._items = [];
28+
this.items = [];
2929
}
3030

3131
toArray() {
32-
return this._items;
32+
return this.items;
3333
}
3434

3535
toString() {
36-
return this._items.toString();
36+
return this.items.toString();
3737
}
3838
}

src/js/data-structures/stack.js

+15-35
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,49 @@
11
// @ts-check
22

3-
4-
const _items = new WeakMap();
5-
const _count = new WeakMap();
6-
73
export default class Stack {
84
constructor() {
9-
_count.set(this, 0);
10-
_items.set(this, {});
5+
this.count = 0;
6+
this.items = {};
117
}
12-
138
push(element) {
14-
const items = _items.get(this);
15-
const count = _count.get(this);
16-
items[count] = element;
17-
_count.set(this, count + 1);
9+
this.items[this.count] = element;
10+
this.count++;
1811
}
19-
2012
pop() {
2113
if (this.isEmpty()) {
2214
return undefined;
2315
}
24-
const items = _items.get(this);
25-
let count = _count.get(this);
26-
count--;
27-
_count.set(this, count);
28-
const result = items[count];
29-
delete items[count];
16+
this.count--;
17+
const result = this.items[this.count];
18+
delete this.items[this.count];
3019
return result;
3120
}
32-
3321
peek() {
3422
if (this.isEmpty()) {
3523
return undefined;
3624
}
37-
const items = _items.get(this);
38-
const count = _count.get(this);
39-
return items[count - 1];
25+
return this.items[this.count - 1];
4026
}
41-
4227
isEmpty() {
43-
return _count.get(this) === 0;
28+
return this.count === 0;
4429
}
45-
4630
size() {
47-
return _count.get(this);
31+
return this.count;
4832
}
49-
5033
clear() {
5134
/* while (!this.isEmpty()) {
5235
this.pop();
5336
} */
54-
_count.set(this, 0);
55-
_items.set(this, {});
37+
this.items = {};
38+
this.count = 0;
5639
}
57-
5840
toString() {
5941
if (this.isEmpty()) {
6042
return '';
6143
}
62-
const items = _items.get(this);
63-
const count = _count.get(this);
64-
let objString = `${items[0]}`;
65-
for (let i = 1; i < count; i++) {
66-
objString = `${objString},${items[i]}`;
44+
let objString = `${this.items[0]}`;
45+
for (let i = 1; i < this.count; i++) {
46+
objString = `${objString},${this.items[i]}`;
6747
}
6848
return objString;
6949
}

0 commit comments

Comments
 (0)