Skip to content

Commit 467f43d

Browse files
author
Carlos Green
committed
queue
1 parent 3d523ad commit 467f43d

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

Chapter5Queues/Dancing.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function Dancer(name, sex) {
2+
this.name = name
3+
this.sex = sex;
4+
}
5+
6+
function getDancer(males, females) {
7+
var names = console.
8+
}

Chapter5Queues/priorityQueues.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const Queue = require('./queue.js');
2+
3+
function Patient(name, code) {
4+
this.name = name;
5+
this.code = code;
6+
}
7+
8+
var p = new Patient('Smith', 5);
9+
var ed = new Queue();
10+
11+
ed.enqueue(p);
12+
13+
p = new Patient('Jones', 4);
14+
ed.enqueue(p);
15+
16+
p = new Patient('Fehrenbach', 6);
17+
ed.enqueue(p);
18+
19+
p = new Patient('Brown', 1);
20+
ed.enqueue(p);
21+
22+
p = new Patient('Ingram', 1);
23+
ed.enqueue(p);
24+
25+
var seen = ed.dequeue();
26+
console.log(seen, 'seen');
27+
console.log(' ');
28+
console.log('Patient being treated: ' + seen[0].name);
29+
console.log(' ');
30+
console.log('Patients waiting to be seen: ');
31+
console.log(' ');
32+
console.log(ed.toString(), 'string');
33+
34+
/*
35+
var seen = ed.dequeue()
36+
console.log(seen, 'seen')
37+
console.log("Patient being treated: " + seen[0].name)
38+
console.log("Patients waiting to be seen: ")
39+
console.log(ed.toString(), 'string')
40+
*/

Chapter5Queues/queue.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
function Queue() {
2+
this.dataStore = []
3+
this.enqueue = (element) => {
4+
this.dataStore.push(element)
5+
}
6+
this.dequeue = () => {
7+
//return this.dataStore.shift() // not priority queue
8+
var priority = this.dataStore[0].code
9+
for (var i = 1; i < this.dataStore.length; ++i) {
10+
if (this.dataStore[i].code < priority) {
11+
priority = i
12+
}
13+
}
14+
return this.dataStore.splice(priority, 1)
15+
}
16+
this.front = () => {
17+
return this.dataStore[0]
18+
}
19+
this.back = () => {
20+
return this.dataStore[this.dataStore.length - 1]
21+
}
22+
this.toString = () => {
23+
var priority = this.dataStore[0].code
24+
for (var i = 1; i < this.dataStore.length; ++i) {
25+
if (this.dataStore[i].code < priority) {
26+
priority = i
27+
}
28+
}
29+
return this.dataStore.splice(priority, 1)
30+
}
31+
/*
32+
* Not priority queue
33+
this.toString = () => {
34+
var retStr = ""
35+
for (var i = 0; i < this.dataStore.length; ++i) {
36+
retStr += this.dataStore[i] + "\n"
37+
}
38+
return retStr
39+
}
40+
*/
41+
this.empty = () => {
42+
if (this.dataStore.length == 0) {
43+
return true
44+
} else {
45+
return false
46+
}
47+
}
48+
}
49+
50+
module.exports = Queue

Chapter5Queues/sortingQueues.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const Queue = require("./queue.js")
2+
3+
function distribute(nums, queues, n, digit) {
4+
for (var i = 0; i < n; i++) {
5+
if (digit == 1) {
6+
queues[nums[i]%10].enqueue(nums[i])
7+
} else {
8+
queues[Math.floor(nums[i] / 10)].enqueue(nums[i])
9+
}
10+
}
11+
}
12+
13+
function collect(queues, nums) {
14+
var i = 0;
15+
for (var digit = 0; digit < 10; ++digit) {
16+
while(!queues[digit].empty()) {
17+
nums[i++] = queues[digit].dequeue()
18+
}
19+
}
20+
}
21+
22+
function disArray(arr) {
23+
for (var i = 0; i < arr.length; ++i) {
24+
console.log(arr[i] + " ")
25+
}
26+
}
27+
28+
var queues = []
29+
30+
for (var i = 0; i < 10; ++i) {
31+
queues[i] = new Queue()
32+
}
33+
34+
var nums = []
35+
for (var i = 0; i < 10; ++i) {
36+
nums[i] = Math.floor(Math.floor(Math.random() * 101))
37+
}
38+
39+
console.log("Before radix sort: ")
40+
console.log(disArray(nums))
41+
console.log(distribute(nums, queues, 10, 1))
42+
console.log(collect(queues, nums))
43+
console.log(distribute(nums, queues, 10, 10))
44+
console.log(collect(queues, nums))
45+
46+
console.log(disArray(nums))

0 commit comments

Comments
 (0)