-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueueObject.html
More file actions
82 lines (79 loc) · 2.02 KB
/
PriorityQueueObject.html
File metadata and controls
82 lines (79 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<script>
function Patient(name, code) {
this.name = name;
this.code = code; //优先码越小优先级越高, 比如,1 比 5 的优先级高
}
function Queue(){
this.dataStore = [];
}
Queue.prototype = {
enqueue: function(element){
this.dataStore.push(element);
},
dequeue: function(){
var priority = this.dataStore[0].code;
for (var i = 1; i < this.dataStore.length; ++i) {
if (this.dataStore[i].code < priority) {
priority = i;
}
}
return this.dataStore.splice(priority,1);
},
front: function(){
return this.dataStore[0];
},
back: function(){
return this.dataStore[this.dataStore.length-1]
},
toString: function(){
var retStr = "";
for (var i = 0; i < this.dataStore.length; ++i) {
retStr += this.dataStore[i].name + " code: "
+ this.dataStore[i].code + "</br>" ;
}
return retStr;
},
empty: function(){
if(this.dataStore.length == 0){
return true;
} else {
return false;
}
},
count: function(){
return this.dataStore.length;
}
}
var ed = new Queue();
var p = new Patient("Smith",5);
ed.enqueue(p);
p = new Patient("Jones", 4);
ed.enqueue(p);
p = new Patient("Fehrenbach", 6);
ed.enqueue(p);
p = new Patient("Brown", 1);
ed.enqueue(p);
p = new Patient("Ingram", 1);
ed.enqueue(p);
document.writeln(ed.toString());
document.writeln('</br>')
var seen = ed.dequeue();
document.writeln("Patient being treated: " + seen[0].name);
document.writeln("</br>")
document.writeln("Patients waiting to be seen: ")
document.writeln(ed.toString());
document.writeln("</br>")
// 下一轮
var seen = ed.dequeue();
document.writeln("Patient being treated: " + seen[0].name);
document.writeln("</br>")
document.writeln("Patients waiting to be seen: ")
document.writeln(ed.toString());
document.writeln("</br>")
var seen = ed.dequeue();
document.writeln("Patient being treated: " + seen[0].name);
document.writeln("</br>")
document.writeln("Patients waiting to be seen: ")
document.writeln(ed.toString());
document.writeln("</br>")
</script>