開発環境
- 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 4.(No. 2421)を解いてみる。
Exercises 4.(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.data_store.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;
};
List.prototype.displayList = function () {
this.front();
while (this.hasNext()) {
print(this.next().toString());
}
};
var movies,
movieList = new List(),
customers = new List(),
Customer = function (name, movie) {
this.name = name;
this.movie = movie;
},
rentedMovieList = new List(),
checkOut = function (name, movie, movieList, customerList) {
var c;
if (movieList.contains(movie)) {
c = new Customer(name, movie);
customerList.append(c);
movieList.remove(movie);
rentedMovieList.append(c);
print('movie list');
rentedMovieList.displayList();
} else {
print(movie + ' is not available.');
}
},
createAttr = function () {
var arr = ['The Shawshank Redemption',
'The Godfather',
'The Godfather: Part II',
'Pulp Fiction',
'The Good, the Bad and the Ugly'];
return arr;
},
i,
max;
Customer.prototype.toString = function () {
return this.name + ', ' + this.movie;
};
movies = createAttr();
for (i = 0, max = movies.length; i < max; i += 1) {
movieList.append(movies[i]);
}
checkOut("Jane Doe", "The Godfather", movieList, customers);
checkOut('Pulp Fiction', 'The Godfather: Part II', movieList, customers);
checkOut("Jane Doe", "The Godfather", movieList, customers);
checkOut("Jane Doe", "The Good, the Bad and the Ugly", movieList, customers);
出力結果(Terminal, shell, SpiderMonkey)
$ js sample4.js
movie list
Jane Doe, The Godfather
movie list
Jane Doe, The Godfather
Pulp Fiction, The Godfather: Part II
The Godfather is not available.
movie list
Jane Doe, The Godfather
Pulp Fiction, The Godfather: Part II
Jane Doe, The Good, the Bad and the Ugly
$
0 コメント:
コメントを投稿