開発環境
- OS X Yosemite - Apple (OS)
- Emacs (Text Editor)
- JavaScript (プログラミング言語)
- SpiderMonkey (JavaScript engine)
Data Structures and Algorithms With Javascript (Michael McMillan(著)、O'Reilly Media)のChapter 3(Lists)、Exercises 3.(No. 2421)を解いてみる。
Exercises 3.(No. 2421)
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 : true
*/
var List = function () {
this.list_size = 0;
this.pos = 0;
this.data_store = [];
};
List.prototype.append = function (element) {
this.data_store[this.list_size] = element;
this.list_size += 1;
};
List.prototype.find = function (element) {
var i,
max;
for (i = 0, max = this.data_store.length; i < max; i += 1) {
if (this.data_store[i] === element) {
return i;
}
}
return -1;
};
List.prototype.remove = function (element) {
var found_at = this.find(element);
if (found_at > -1) {
this.data_store.splice(found_at, 1);
this.list_size -= 1;
return true;
}
return false;
};
List.prototype.length = function () {
return this.list_size;
};
List.prototype.toString = function () {
return this.data_store;
};
List.prototype.insert = function (element, after) {
var insert_pos = this.find(after);
if (insert_pos > -1) {
this.data_store.splice(insert_pos + 1, 0, element);
this.list_size += 1;
return true;
}
return false;
};
List.prototype.clear = function () {
this.data_store = [];
this.list_size = this.pos = 0;
};
List.prototype.contains = function (element) {
var i,
max;
for (i = 0, max = this.dataStore.length; i < max; i += 1) {
if(this.data_store[i] === element) {
return true;
}
}
return false;
};
List.prototype.moveTo = function (pos) {
this.pos = pos;
};
List.prototype.getElement = function () {
return this.data_store[this.pos];
};
List.prototype.previous = function () {
this.pos -= 1;
return this.data_store[this.pos];
};
List.prototype.next = function () {
var element = this.data_store[this.pos];
this.pos += 1;
return element;
};
List.prototype.hasNext = function () {
if (this.pos > this.list_size - 1) {
return false;
}
return true;
};
List.prototype.hasPrevious = function () {
if (this.ps <= 0) {
return false;
}
return true;
};
List.prototype.front = function () {
this.pos = 0;
};
List.prototype.end = function () {
this.pos = this.list_size - 1;
};
List.prototype.currPos = function () {
return this.pos;
};
var Person = function (name, gender) {
this.name = name;
this.gender = gender;
},
m1 = new Person('male1', 'male'),
m2 = new Person('male2', 'male'),
f1 = new Person('female1', 'female'),
f2 = new Person('female2', 'female'),
o1 = new Person('other1', 'other'),
people = new List(),
displayPeople = function (people, gender) {
var person;
people.front();
while (people.hasNext()) {
person = people.next();
if (person.gender === gender) {
print(person.name);
}
}
};
people.append(m1);
people.append(m2);
people.append(f1);
people.append(f2);
people.append(o1);
print('male');
displayPeople(people, 'male');
print('female');
displayPeople(people, 'female');
print('others');
displayPeople(people, 'other');
出力結果(Terminal, shell, SpiderMonkey)
$ js sample3.js
male
male1
male2
female
female1
female2
others
other1
$
0 コメント:
コメントを投稿