Skip to content

Commit 2588ad5

Browse files
committed
adding chapter 5 Queues
1 parent a2bf581 commit 2588ad5

File tree

3 files changed

+209
-8
lines changed

3 files changed

+209
-8
lines changed

04-chapter-Stacks.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ let e2 = exp2.split('');
7070
e2 = e2.filter(item => item !== ' ');
7171
let balanced2 = checkBalancedExpression(e2)
7272

73+
console.log('CHAPTER 4');
7374
console.log('### Excercise 1');
7475
console.log(`Expression example: ${exp}`);
7576
(balanced) ? console.log('The expression is balanced') : console.log('The expression is no balanced');

05-chapter-Queues.js

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
// Queue data structure class example
2+
class Queue {
3+
constructor(dataStore = []) {
4+
this.dataStore = dataStore;
5+
}
6+
7+
enqueue(element) {
8+
this.dataStore.push(element);
9+
}
10+
11+
dequeue() {
12+
this.dataStore.shift();
13+
}
14+
15+
front() {
16+
return this.dataStore[0];
17+
}
18+
19+
back() {
20+
return this.dataStore[this.dataStore.length - 1];
21+
}
22+
23+
empty() {
24+
if(this.dataStore.length === 0) {
25+
return true;
26+
} else {
27+
return false;
28+
}
29+
}
30+
31+
count() {
32+
return this.dataStore.length;
33+
}
34+
35+
toString() {
36+
return JSON.stringify(this.dataStore);
37+
}
38+
}
39+
40+
// Dequeue class example for exercise 1
41+
class Dequeue {
42+
constructor(dataStore = []) {
43+
this.dataStore = dataStore;
44+
}
45+
46+
enqueueFront(element) {
47+
this.dataStore.unshift(element);
48+
}
49+
50+
enqueue(element) {
51+
this.dataStore.push(element);
52+
}
53+
54+
dequeue() {
55+
this.dataStore.shift();
56+
}
57+
58+
dequeueBack() {
59+
this.dataStore.pop();
60+
}
61+
62+
front() {
63+
return this.dataStore[0];
64+
}
65+
66+
back() {
67+
return this.dataStore[this.dataStore.length - 1];
68+
}
69+
70+
empty() {
71+
if(this.dataStore.length === 0) {
72+
return true;
73+
} else {
74+
return false;
75+
}
76+
}
77+
78+
count() {
79+
return this.dataStore.length;
80+
}
81+
82+
toString() {
83+
return JSON.stringify(this.dataStore);
84+
}
85+
86+
}
87+
88+
// Implementation
89+
// ###########################################
90+
/*
91+
1. Modify the Queue class to create a Deque class. A deque is a queue-like structure
92+
that allows elements to be added and removed from both the front and the back of the list.
93+
Test your class in a program.
94+
*/
95+
console.log('CHAPTER 5');
96+
console.log('### Excercise 1');
97+
const deq = new Dequeue();
98+
deq.enqueue('Jane');
99+
deq.enqueue('Jhon');
100+
deq.enqueue('Sam');
101+
deq.enqueueFront('Emilia');
102+
deq.enqueueFront('Mother of Dragons');
103+
deq.enqueueFront('Kalisy');
104+
console.log('Original Queue Stack');
105+
console.log(deq.toString());
106+
deq.dequeue();
107+
deq.dequeue();
108+
deq.dequeueBack();
109+
console.log('2 Dequeue and 1 Dequeue from back');
110+
console.log(deq.toString());
111+
112+
/*
113+
2.- Use the Deque class you created to determine if a given word is a palindrome.
114+
*/
115+
116+
function isPalindrome(word) {
117+
const letter = new RegExp('[a-z]');
118+
let palindrome = word.toLowerCase().split('');
119+
120+
palindrome = palindrome.filter(item => letter.test(item));
121+
const queue = new Dequeue(palindrome);
122+
123+
while (!queue.empty()) {
124+
if(queue.count() > 1) {
125+
a = queue.front();
126+
b = queue.back();
127+
} else {
128+
console.log(`The string '${word.toUpperCase()}' is palindrome!`);
129+
break;
130+
}
131+
if(a === b && queue.count() > 1) {
132+
queue.dequeue();
133+
queue.dequeueBack();
134+
} else {
135+
console.log(`The string '${word.toUpperCase()}' is not palindrome!`);
136+
break;
137+
}
138+
}
139+
}
140+
141+
console.log('\n\n### Excercise 2');
142+
const word = 'racecar';
143+
const word2 = 'A man, a plan, a canal: Panama';
144+
const word3 = 'what is this';
145+
146+
isPalindrome(word);
147+
isPalindrome(word2);
148+
isPalindrome(word3);
149+
150+
/*
151+
3. Modify the priority queue example from Example 5-5 so that the higher-priority
152+
elements have higher numbers rather than lower numbers. Test your implementation
153+
with the example in the chapter.
154+
*/
155+
// Class Patient for Exercise 3
156+
class Patient {
157+
constructor(name = 'generic', code = '0') {
158+
this.name = name;
159+
this.code = code;
160+
}
161+
}
162+
class GenericQueue extends Queue {
163+
constructor() {
164+
super();
165+
}
166+
167+
dequeue() {
168+
let priority = this.front().code;
169+
let position = 0;
170+
for (let i = 0; i < this.count(); i++) {
171+
if (this.dataStore[i].code >= priority) {
172+
priority = this.dataStore[i].code;
173+
position = i;
174+
}
175+
}
176+
return this.dataStore.splice(position,1);
177+
}
178+
}
179+
180+
console.log('\n\n### Excercise 3');
181+
const ed = new GenericQueue();
182+
const p = new Patient("Smith",5);
183+
ed.enqueue(p);
184+
const p2 = new Patient("Jones", 4);
185+
ed.enqueue(p2);
186+
const p3 = new Patient("Fehrenbach", 6);
187+
ed.enqueue(p3);
188+
const p4 = new Patient("Brown", 1);
189+
ed.enqueue(p4);
190+
const p5 = new Patient("Ingram", 1);
191+
ed.enqueue(p5);
192+
console.log(ed.toString());
193+
194+
while (ed.count() > 0) {
195+
let seen = ed.dequeue();
196+
console.log("\nPatient being treated: " + seen[0].name);
197+
console.log("Patients waiting to be seen: ")
198+
console.log(ed.toString(), '\n');
199+
}

README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33

44
![](https://i.blogs.es/545cf8/es6-logo/original.png) ![](https://lh3.googleusercontent.com/a4Xrc-8oQLu05mOrNPuvA_o2nZEIEnOoTH4wB91Slw_hCvuIu_Qgi440bK9mC8ml-KA=w300)
55

6-
This exercises are based from the book [Data Structures and Algorithms with JavaScript](http://shop.oreilly.com/product/0636920029557.do) - [Michael McMillian](http://www.oreilly.com/pub/au/518).
6+
This exercises are based from the book [Data Structures and Algorithms with JavaScript](http://shop.oreilly.com/product/0636920029557.do) - [by Michael McMillian (O’Reilly)](http://www.oreilly.com/pub/au/518) ISBN - 978-1-449-36493-9.
77

8-
The purpose of this repo is to update the exercises to ES6 standards.
8+
The purpose of this repo is to update the exercises to ES6 standards and to correct the errors from the examples that this book has.
99

10-
## Examples covers
10+
## Examples in this repo
1111

12-
| **Num** | **Type** | **Excercises** |
13-
--- | ---
14-
| 1.- | Array | 4 |
15-
| 2.- | Lists | 5 |
16-
| 3.- | Stacks | 3 |
12+
| **Num** | **Type** | **Exercises** | **Description** |
13+
--- | --- | --- | ---
14+
| 1.- | Array | 4 | The `Array` is the most common data structure in computer programming
15+
| 2.- | Lists | 5 | A `List` is an ordered sequence of data, where elements are not meant to be ordered.
16+
| 3.- | Stacks | 3 | A `Stack` is an example of Last-in, First-Out (LIFO)
17+
| 4.- | Queues | | A `Queue` is an example of First-in, First-Out (FIFO)

0 commit comments

Comments
 (0)