開発環境
- OS X Yosemite - Apple (OS)
- Emacs (Text Editor)
- JavaScript (プログラミング言語)
- SpiderMonkey (JavaScript engine)
Data Structures and Algorithms With Javascript (Michael McMillan(著)、O'Reilly Media)のChapter 5(Queues)、Exercises 3.(No. 3595)を解いてみる。
Exercises 3.(No. 3595)
JavaScript(Emacs)
/*jslint browser : true, continue : true,
devel : true, indent : 4, maxerr : 50,
newcap : true, nomen : true, plusplus : false,
regexp : true, sloppy : true, vars : false,
white : false
*/
/*global print */
var Queue = function () {
this.data_store = [];
},
Patient = function (name, code) {
this.name = name;
this.code = code;
},
ed = new Queue(),
seen;
Queue.prototype.enqueue = function (element) {
this.data_store.push(element);
};
Queue.prototype.dequeue = function () {
return this.data_store.shift();
};
Queue.prototype.front = function () {
return this.data_store[0];
};
Queue.prototype.back = function () {
return this.data_store[this.data_store.length - 1];
};
Queue.prototype.toString = function () {
var result = '',
i,
max;
for (i = 0, max = this.data_store.length; i < max - 1; i += 1) {
result += this.data_store[i] + ',';
}
result += this.data_store[i];
return result;
};
Queue.prototype.isEmpty = function () {
return this.data_store.length === 0 ? true : false;
};
Queue.prototype.count = function () {
return this.data_store.length;
};
ed.dequeue = function () {
var entry = 0,
i,
max;
for (i = 0, max = this.data_store.length; i < max; i += 1) {
if (this.data_store[i].code > this.data_store[entry].code) {
entry = i;
}
}
return this.data_store.splice(entry, 1);
};
ed.toString = function () {
var result = '',
i,
max;
for (i = 0, max = this.data_store.length; i < max; i += 1) {
result += this.data_store[i].name + ' code: '
+ this.data_store[i].code + '\n';
}
return result;
};
ed.enqueue(new Patient('Smith', 5));
ed.enqueue(new Patient('Jones', 4));
ed.enqueue(new Patient('Fehrenbach', 6));
ed.enqueue(new Patient('Brown', 1));
ed.enqueue(new Patient('Ingram', 1));
print(ed.toString());
seen = ed.dequeue();
print('Patient being treated: ' + seen[0].name);
print('Patients waiting to be seen: ');
print(ed.toString());
seen = ed.dequeue();
print('Patient begin treated: ' + seen[0].name);
print('Patients waiting to be seen: ');
print(ed.toString());
seen = ed.dequeue();
print('Patient begin treated: ' + seen[0].name);
print('Patients waiting to be seen: ');
print(ed.toString());
seen = ed.dequeue();
print('Patient begin treated: ' + seen[0].name);
print('Patients waiting to be seen: ');
print(ed.toString());
出力結果(Terminal, shell, SpiderMonkey)
$ jslint sample3.js
sample3.js is OK.
$ js sample3.js
Smith code: 5
Jones code: 4
Fehrenbach code: 6
Brown code: 1
Ingram code: 1
Patient being treated: Fehrenbach
Patients waiting to be seen:
Smith code: 5
Jones code: 4
Brown code: 1
Ingram code: 1
Patient begin treated: Smith
Patients waiting to be seen:
Jones code: 4
Brown code: 1
Ingram code: 1
Patient begin treated: Jones
Patients waiting to be seen:
Brown code: 1
Ingram code: 1
Patient begin treated: Brown
Patients waiting to be seen:
Ingram code: 1
$
0 コメント:
コメントを投稿