Skip to content

Commit 5eabb7b

Browse files
committed
chapter 04: [Queues and Deques]
1 parent ea30d2c commit 5eabb7b

32 files changed

+1468
-45
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Work in Progress.
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)
1515
* 03: [Stacks](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter03)
16+
* 04: [Queues and Deques](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter04)
1617

1718
## Thrid Edition Updates
1819

examples/PacktDataStructuresAlgorithms.min.js

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

examples/chapter04/01-Queue.html

+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="01-Queue.js"></script>
10+
</body>
11+
</html>

examples/chapter04/01-Queue.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const { Queue } = PacktDataStructuresAlgorithms;
2+
3+
const queue = new Queue();
4+
console.log(queue.isEmpty()); // outputs true
5+
queue.enqueue('John');
6+
queue.enqueue('Jack');
7+
console.log(queue.toString()); // John,Jack
8+
queue.enqueue('Camila');
9+
console.log(queue.toString()); // John,Jack,Camila
10+
console.log(queue.size()); // outputs 3
11+
console.log(queue.isEmpty()); // outputs false
12+
queue.dequeue();
13+
queue.dequeue();
14+
console.log(queue.toString()); // Camila

examples/chapter04/02-Deque.html

+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="02-Deque.js"></script>
10+
</body>
11+
</html>

examples/chapter04/02-Deque.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { Deque } = PacktDataStructuresAlgorithms;
2+
3+
const deque = new Deque();
4+
console.log(deque.isEmpty()); // outputs true
5+
deque.addBack('John');
6+
deque.addBack('Jack');
7+
console.log(deque.toString()); // John,Jack
8+
deque.addBack('Camila');
9+
console.log(deque.toString()); // John,Jack,Camila
10+
console.log(deque.size()); // outputs 3
11+
console.log(deque.isEmpty()); // outputs false
12+
deque.removeFront();
13+
console.log(deque.toString()); // Jack,Camila
14+
deque.removeBack(); // Camila decides to leave
15+
console.log(deque.toString()); // Jack
16+
deque.addFront('John');
17+
console.log(deque.toString()); // John,Jack

examples/chapter04/03-HotPotato.html

+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-HotPotato.js"></script>
10+
</body>
11+
</html>

examples/chapter04/03-HotPotato.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { hotPotato } = PacktDataStructuresAlgorithms;
2+
3+
const names = ['John', 'Jack', 'Camila', 'Ingrid', 'Carl'];
4+
const result = hotPotato(names, 7);
5+
6+
result.eliminated.forEach(name => {
7+
console.log(`${name} was eliminated from the Hot Potato game.`);
8+
});
9+
10+
console.log(`The winner is: ${result.winner}`);
11+
12+
// Camila was eliminated from the Hot Potato game.
13+
// Jack was eliminated from the Hot Potato game.
14+
// Carl was eliminated from the Hot Potato game.
15+
// Ingrid was eliminated from the Hot Potato game.
16+
// The winner is: John
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-PalindromeChecker.js"></script>
10+
</body>
11+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { palindromeChecker } = PacktDataStructuresAlgorithms;
2+
3+
console.log('a', palindromeChecker('a'));
4+
console.log('aa', palindromeChecker('aa'));
5+
console.log('kayak', palindromeChecker('kayak'));
6+
console.log('level', palindromeChecker('level'));
7+
console.log('Was it a car or a cat I saw', palindromeChecker('Was it a car or a cat I saw'));
8+
console.log('Step on no pets', palindromeChecker('Step on no pets'));

examples/index.html

+60-41
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
.mdl-layout__content {
1414
padding: 10px;
1515
}
16+
1617
.mdl-layout__drawer {
1718
width: 290px;
1819
}
20+
1921
.iframe-padding {
2022
padding-left: 295px;
2123
}
24+
2225
.mdl-layout__drawer .mdl-navigation .mdl-navigation__link {
2326
color: rgb(103, 58, 183);
2427
}
@@ -39,37 +42,39 @@
3942
<a href="#scroll-tab-2" class="mdl-layout__tab">02</a>
4043
<a href="#scroll-tab-3" class="mdl-layout__tab">03</a>
4144
<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>
45+
<a href="#scroll-tab-5" class="mdl-layout__tab">05</a>
46+
<a href="#scroll-tab-5" class="mdl-layout__tab">06</a>
4447
</div>
4548
</header>
4649
<main class="mdl-layout__content">
4750
<span class="iframe-padding">Please open the Developer Tools Console to see the output</span>
4851
<!-- Where the examples will be displayed -->
49-
<div id="myFrame" class="iframe-padding"><iframe name="myFrame" src=""></iframe></div>
52+
<div id="myFrame" class="iframe-padding">
53+
<iframe name="myFrame" src=""></iframe>
54+
</div>
5055
<!-- Where the examples will be displayed - end -->
5156
<section class="mdl-layout__tab-panel is-active" id="scroll-tab-1">
5257
<div class="page-content mdl-layout--fixed-drawer">
5358
<div class="mdl-layout__drawer is-visible">
5459
<nav class="mdl-navigation">
5560
<a class="mdl-navigation__link" href="chapter01/01-HelloWorld.html">01-HelloWorld</a>
56-
<a class="mdl-navigation__link" href="chapter01/02-Variables.html" >02-Variables</a>
57-
<a class="mdl-navigation__link" href="chapter01/03-Operators.html" >03-Operators</a>
58-
<a class="mdl-navigation__link" href="chapter01/04-TruthyFalsy.html" >04-Truthy Falsy</a>
59-
<a class="mdl-navigation__link" href="chapter01/05-EqualsOperators.html" >05-Equals Operators</a>
60-
<a class="mdl-navigation__link" href="chapter01/06-ConditionalStatements.html" >06-Conditional Statements</a>
61-
<a class="mdl-navigation__link" href="chapter01/07-Loops.html" >07-Loops</a>
62-
<a class="mdl-navigation__link" href="chapter01/08-Functions.html" >08-Functions</a>
63-
<a class="mdl-navigation__link" href="chapter01/09-ObjectOrientedJS.html" >09-Object Oriented JS</a>
64-
<a class="mdl-navigation__link" href="chapter01/10-ES2015-ES6-letconst.html" >10-ES2015-letconst</a>
65-
<a class="mdl-navigation__link" href="chapter01/11-ES2015-ES6-variableScope.html" >11-ES2015-variableScope</a>
66-
<a class="mdl-navigation__link" href="chapter01/12-ES2015-ES6-StringTemplates.html" >12-ES2015-String Templates</a>
67-
<a class="mdl-navigation__link" href="chapter01/13-ES2015-ES6-ArrowFunctions.html" >13-ES2015-Arrow Functions</a>
68-
<a class="mdl-navigation__link" href="chapter01/14-ES2015-ES6-ParameterHandling.html" >14-ES2015-Parameter Handling</a>
69-
<a class="mdl-navigation__link" href="chapter01/15-ES2015-ES6-EnhancedObjectProperties.html" >15-ES2015-EnhancedObject Properties</a>
70-
<a class="mdl-navigation__link" href="chapter01/16-ES2015-ES6-Classes.html" >16-ES2015-Classes</a>
71-
<a class="mdl-navigation__link" href="chapter01/17-ES2015-ES6-Modules.html" >17-ES2015-Modules</a>
72-
<a class="mdl-navigation__link" href="chapter01/18-ES2016-ES7-ExponentiationOperator.html" >18-ES2016-ExponentiationOperator</a>
61+
<a class="mdl-navigation__link" href="chapter01/02-Variables.html">02-Variables</a>
62+
<a class="mdl-navigation__link" href="chapter01/03-Operators.html">03-Operators</a>
63+
<a class="mdl-navigation__link" href="chapter01/04-TruthyFalsy.html">04-Truthy Falsy</a>
64+
<a class="mdl-navigation__link" href="chapter01/05-EqualsOperators.html">05-Equals Operators</a>
65+
<a class="mdl-navigation__link" href="chapter01/06-ConditionalStatements.html">06-Conditional Statements</a>
66+
<a class="mdl-navigation__link" href="chapter01/07-Loops.html">07-Loops</a>
67+
<a class="mdl-navigation__link" href="chapter01/08-Functions.html">08-Functions</a>
68+
<a class="mdl-navigation__link" href="chapter01/09-ObjectOrientedJS.html">09-Object Oriented JS</a>
69+
<a class="mdl-navigation__link" href="chapter01/10-ES2015-ES6-letconst.html">10-ES2015-letconst</a>
70+
<a class="mdl-navigation__link" href="chapter01/11-ES2015-ES6-variableScope.html">11-ES2015-variableScope</a>
71+
<a class="mdl-navigation__link" href="chapter01/12-ES2015-ES6-StringTemplates.html">12-ES2015-String Templates</a>
72+
<a class="mdl-navigation__link" href="chapter01/13-ES2015-ES6-ArrowFunctions.html">13-ES2015-Arrow Functions</a>
73+
<a class="mdl-navigation__link" href="chapter01/14-ES2015-ES6-ParameterHandling.html">14-ES2015-Parameter Handling</a>
74+
<a class="mdl-navigation__link" href="chapter01/15-ES2015-ES6-EnhancedObjectProperties.html">15-ES2015-EnhancedObject Properties</a>
75+
<a class="mdl-navigation__link" href="chapter01/16-ES2015-ES6-Classes.html">16-ES2015-Classes</a>
76+
<a class="mdl-navigation__link" href="chapter01/17-ES2015-ES6-Modules.html">17-ES2015-Modules</a>
77+
<a class="mdl-navigation__link" href="chapter01/18-ES2016-ES7-ExponentiationOperator.html">18-ES2016-ExponentiationOperator</a>
7378
</nav>
7479
</div>
7580
</div>
@@ -80,38 +85,52 @@
8085
<div class="mdl-layout__drawer is-visible">
8186
<nav class="mdl-navigation">
8287
<a class="mdl-navigation__link" href="chapter02/01-Introduction.html">01-Introduction</a>
83-
<a class="mdl-navigation__link" href="chapter02/02-CreatingAndInitialingArrays.html" >02-Creating Initialing Arrays</a>
84-
<a class="mdl-navigation__link" href="chapter02/03-AddingRemovingElements.html" >03-Adding Removing Elements</a>
85-
<a class="mdl-navigation__link" href="chapter02/04-TwoDimensionalMultiDimensional.html" >04-Two Dimensional - MultiDimensional</a>
86-
<a class="mdl-navigation__link" href="chapter02/05-ArrayMethods.html" >05-ArrayMethods</a>
87-
<a class="mdl-navigation__link" href="chapter02/06-ES6Methods.html" >06-ES2015+ Methods</a>
88-
<a class="mdl-navigation__link" href="chapter02/07-Sorting.html" >07-Sorting</a>
89-
<a class="mdl-navigation__link" href="chapter02/08-Searching.html" >08-Searching</a>
90-
<a class="mdl-navigation__link" href="chapter02/09-TypedArrays.html" >09-TypedArrays</a>
88+
<a class="mdl-navigation__link" href="chapter02/02-CreatingAndInitialingArrays.html">02-Creating Initialing Arrays</a>
89+
<a class="mdl-navigation__link" href="chapter02/03-AddingRemovingElements.html">03-Adding Removing Elements</a>
90+
<a class="mdl-navigation__link" href="chapter02/04-TwoDimensionalMultiDimensional.html">04-Two Dimensional - MultiDimensional</a>
91+
<a class="mdl-navigation__link" href="chapter02/05-ArrayMethods.html">05-ArrayMethods</a>
92+
<a class="mdl-navigation__link" href="chapter02/06-ES6Methods.html">06-ES2015+ Methods</a>
93+
<a class="mdl-navigation__link" href="chapter02/07-Sorting.html">07-Sorting</a>
94+
<a class="mdl-navigation__link" href="chapter02/08-Searching.html">08-Searching</a>
95+
<a class="mdl-navigation__link" href="chapter02/09-TypedArrays.html">09-TypedArrays</a>
9196
</nav>
9297
</div>
9398
</div>
9499
</div>
95100
</section>
96101
<section class="mdl-layout__tab-panel" id="scroll-tab-3">
97102
<div class="page-content">
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-
<a class="mdl-navigation__link" href="chapter03/02-BalancedSymbols.html">02-BalancedSymbols</a>
103-
<a class="mdl-navigation__link" href="chapter03/03-DecimalToBinary.html">03-DecimalToBinary</a>
104-
<a class="mdl-navigation__link" href="chapter03/04-TowerOfHanoi.html">04-TowerOfHanoi</a>
105-
</nav>
106-
</div>
107-
</div>
103+
<div class="page-content mdl-layout--fixed-drawer">
104+
<div class="mdl-layout__drawer is-visible">
105+
<nav class="mdl-navigation">
106+
<a class="mdl-navigation__link" href="chapter03/01-Stack.html">01-Stack</a>
107+
<a class="mdl-navigation__link" href="chapter03/02-BalancedSymbols.html">02-BalancedSymbols</a>
108+
<a class="mdl-navigation__link" href="chapter03/03-DecimalToBinary.html">03-DecimalToBinary</a>
109+
<a class="mdl-navigation__link" href="chapter03/04-TowerOfHanoi.html">04-TowerOfHanoi</a>
110+
</nav>
111+
</div>
112+
</div>
108113
</div>
109114
</section>
110115
<section class="mdl-layout__tab-panel" id="scroll-tab-4">
111-
<div class="page-content">
112-
Soon.
116+
<div class="page-content">
117+
<div class="page-content mdl-layout--fixed-drawer">
118+
<div class="mdl-layout__drawer is-visible">
119+
<nav class="mdl-navigation">
120+
<a class="mdl-navigation__link" href="chapter04/01-Queue.html">01-Queue</a>
121+
<a class="mdl-navigation__link" href="chapter04/02-Deque.html">02-Deque</a>
122+
<a class="mdl-navigation__link" href="chapter04/03-HotPotato.html">03-HotPotato</a>
123+
<a class="mdl-navigation__link" href="chapter04/04-PalindromeChecker.html">04-PalindromeChecker</a>
124+
</nav>
125+
</div>
113126
</div>
114-
</section>
127+
</div>
128+
</section>
129+
<section class="mdl-layout__tab-panel" id="scroll-tab-5">
130+
<div class="page-content">
131+
Soon.
132+
</div>
133+
</section>
115134
</main>
116135
</div>
117136
</body>

src/js/data-structures/deque.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// @ts-check
2+
3+
export default class Deque {
4+
constructor() {
5+
this.count = 0;
6+
this.lowestCount = 0;
7+
this.items = {};
8+
}
9+
10+
addFront(element) {
11+
if (this.isEmpty()) {
12+
this.addBack(element);
13+
} else if (this.lowestCount > 0) {
14+
this.lowestCount--;
15+
this.items[this.lowestCount] = element;
16+
} else {
17+
for (let i = this.count; i > 0; i--) {
18+
this.items[i] = this.items[i - 1];
19+
}
20+
this.count++;
21+
this.items[0] = element;
22+
}
23+
}
24+
25+
addBack(element) {
26+
this.items[this.count] = element;
27+
this.count++;
28+
}
29+
30+
removeFront() {
31+
if (this.isEmpty()) {
32+
return undefined;
33+
}
34+
const result = this.items[this.lowestCount];
35+
delete this.items[this.lowestCount];
36+
this.lowestCount++;
37+
return result;
38+
}
39+
40+
removeBack() {
41+
if (this.isEmpty()) {
42+
return undefined;
43+
}
44+
this.count--;
45+
const result = this.items[this.count];
46+
delete this.items[this.count];
47+
return result;
48+
}
49+
50+
peekFront() {
51+
if (this.isEmpty()) {
52+
return undefined;
53+
}
54+
return this.items[this.lowestCount];
55+
}
56+
57+
peekBack() {
58+
if (this.isEmpty()) {
59+
return undefined;
60+
}
61+
return this.items[this.count - 1];
62+
}
63+
64+
isEmpty() {
65+
return this.size() === 0;
66+
}
67+
68+
clear() {
69+
this.items = {};
70+
this.count = 0;
71+
this.lowestCount = 0;
72+
}
73+
74+
size() {
75+
return this.count - this.lowestCount;
76+
}
77+
78+
toString() {
79+
if (this.isEmpty()) {
80+
return '';
81+
}
82+
let objString = `${this.items[this.lowestCount]}`;
83+
for (let i = this.lowestCount + 1; i < this.count; i++) {
84+
objString = `${objString},${this.items[i]}`;
85+
}
86+
return objString;
87+
}
88+
}

0 commit comments

Comments
 (0)