Skip to content

Commit ff8d43f

Browse files
committed
refactored from ES6 classes to factory function to Queue Data Strucutre
1 parent a3d5768 commit ff8d43f

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

05-chapter-Queue/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(function (exports) {
2+
const {queue} = require('./queue.module')
3+
Object.assign(exports, {queue})
4+
}((typeof module.exports !== undefined) ? module.exports : window))

05-chapter-Queue/queue.module.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
(function (exports) {
2+
const queue = (data = []) => {
3+
let dataStore = data
4+
5+
const queueProto = {
6+
enqueue: (element) => dataStore.push(element),
7+
dequeue: () => dataStore.shift(),
8+
front: () => dataStore[0],
9+
back: () => dataStore[dataStore.length - 1],
10+
isEmpty: () => dataStore.length === 0,
11+
length: () => dataStore.length,
12+
getQueue: () => dataStore,
13+
setQueue: (data) => {
14+
dataStore = data
15+
},
16+
toString: () => JSON.stringify(dataStore, null, 2)
17+
}
18+
19+
return Object.create(queueProto)
20+
}
21+
22+
Object.assign(exports, {queue})
23+
}((typeof module.exports !== undefined) ? module.exports : window))

05-chapter-Queue/readme.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Queue with ES6
2+
3+
### Code Examples
4+
- [Queue Data Structure](./queue.module.js)
5+
- [Import Module](./index.js)
6+
7+
### Description
8+
A queue is a type of list where data are inserted at the end and are removed from the front. Queues are used to store data in the order in which they occur, as opposed to a stack, in which the last piece of data entered is the first element used for processing. Think of a queue like the line at your bank, where the first person into the line is the first person served, and as more customers enter a line, they wait in the back until it is their turn to be served.
9+
10+
A queue is an example of a first-in, first-out (FIFO) data structure. Queues are used to order processes submitted to an operating system or a print spooler, and simulation applications use queues to model scenarios such as customers standing in the line at a bank or a grocery store.
11+
12+
![](https://cdn-images-1.medium.com/max/800/0*SFoM_gyGXk8MIfl4.png)
13+
14+
common operations you can perform on graphs:
15+
- `enqueue`: adds a new element to the queue at the last position
16+
- `dequeue`: remove the first item from the queue
17+
- `front`: returns the first item pushed to queue
18+
- `back`: returns the last item pushed to queue
19+
- `length`: returns the size of the queue
20+
- `isEmpty`: returns true if the queue has elements or false if it has no elements
21+
- `getQueue`: returns the data of the queue
22+
23+
### Example use cases
24+
- Resolving simultaneous server requests from multiple users, such as 3 people buying the last ticket for a plane at almost the same time
25+
- Queuing up data during a breadth-first search.

05-chapter-Queue/test.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const {queue} = require('./index')
2+
3+
test('Queue test 1', assert => {
4+
const patient = (name = 'generic', code = '0') => Object.assign({}, {name, code})
5+
6+
const ed = queue()
7+
const p = patient('Smith', 5)
8+
ed.enqueue(p)
9+
const p2 = patient('Jones', 4)
10+
ed.enqueue(p2)
11+
const p3 = patient('Fehrenbach', 6)
12+
ed.enqueue(p3)
13+
const p4 = patient('Brown', 1)
14+
ed.enqueue(p4)
15+
const p5 = patient('Ingram', 1)
16+
ed.enqueue(p5)
17+
18+
const dequeue = function () {
19+
let priority = this.front().code
20+
let position = 0
21+
let dataStore = this.getQueue()
22+
for (let i = 0; i < this.length(); i++) {
23+
if (dataStore[i].code >= priority) {
24+
priority = dataStore[i].code
25+
position = i
26+
}
27+
}
28+
return dataStore.splice(position, 1)
29+
}
30+
31+
Object.assign(ed, {dequeue})
32+
33+
let seen = []
34+
35+
while (ed.length() > 0) {
36+
let {name} = ed.dequeue()[0]
37+
seen.push(name)
38+
// console.log('\nPatient being treated: ' + seen[seen.length - 1])
39+
}
40+
41+
assert.equal(seen[0], 'Fehrenbach', 'dequeue fn override and dequeue by code')
42+
})
43+
44+
test('Queue test 2', assert => {
45+
function isPalindrome (word) {
46+
const letter = new RegExp('[a-z]')
47+
let palindrome = word.toLowerCase().split('')
48+
let a, b
49+
palindrome = palindrome.filter(item => letter.test(item))
50+
const q = queue(palindrome)
51+
52+
const dequeueBack = function (element) {
53+
const dataStore = this.getQueue()
54+
const data = dataStore.unshift(element)
55+
this.setQueue(data)
56+
return data
57+
}
58+
59+
Object.assign(q, {dequeueBack})
60+
61+
while (!q.isEmpty()) {
62+
if (q.length() > 1) {
63+
a = q.front()
64+
b = q.back()
65+
} else {
66+
// console.log(`The string '${word.toUpperCase()}' is palindrome!`)
67+
return true
68+
}
69+
if (a === b && q.length() > 1) {
70+
q.dequeue()
71+
q.dequeueBack()
72+
} else {
73+
// console.log(`The string '${word.toUpperCase()}' is not palindrome!`)
74+
return false
75+
}
76+
}
77+
}
78+
79+
const word = 'racecar'
80+
const word2 = 'A man, a plan, a canal: Panama'
81+
const word3 = 'what is this'
82+
83+
assert.equal(true, isPalindrome(word), `the string = '${word}' is palindrome`)
84+
assert.equal(true, isPalindrome(word2), `the string = '${word2}' is palindrome`)
85+
assert.equal(false, isPalindrome(word3), `the string = '${word3}' is not a palindrome`)
86+
})

0 commit comments

Comments
 (0)