标签:存储 一个人 queue类 view while 数据 hid 使用 color
1.使用队列:方块舞的舞伴分配问题
<script type="text/javascript"> function Queue(){ this.dataStore = []; this.enqueue = enqueue; this.dequeue = dequeue; this.front = front; this.back = back; this.toString = toString; this.empty = empty; this.count = count; } /** * 向队尾添加一个元素 */ function enqueue(element){ this.dataStore.push(element); } /** * 删除队首的元素: */ function dequeue(){ this.dataStore.shift(); } /** * 读取队首的元素: */ function front(){ return this.dataStore[0]; } /** * 读取队尾的元素: */ function back(){ return this.dataStore[this.dataStore.length - 1]; } /** * 显示队列内的所有元素 */ function toString(){ var retStr = ""; for (var i = 0; i < this.dataStore.length; ++i) { retStr += this.dataStore[i] + "\n"; } return retStr; } /** * 判断队列是否为空 */ function empty(){ if(this.dataStore.length == 0){ return true; }else{ return false; } } /** * 显示队列中有多少个元素 */ function count(){ return this.dataStore.length; } //===================================使用Queue类============================================= /** * 每个舞者信息都被存储在一个Dancer 对象中 */ function Dancer(name, sex) { this.name = name; this.sex = sex; } /** * 将舞者信息从文件中读到程序里来 * trim() 函数除去了每行字符串后的空格 * 根据性别,将舞者加入不同的队列 */ function getDancers(males, females){ var names = read("dancers.txt").split("\n"); for (var i = 0; i < names.length; ++i) { names[i] = names[i].trim(); } for (var i = 0; i < names.length; ++i) { var dancer = names[i].split(" "); var sex = dancer[0]; var name = dancer[1]; if (sex == "F") { females.enqueue(new Dancer(name, sex)); }else{ males.enqueue(new Dancer(name, sex)); } } } /** * 将男性和女性组成舞伴,并且宣布配对结果 */ function dance(males, females){ console.log("The dance partners are: \n"); while (!females.empty() && !males.empty()) { person = females.dequeue(); console.log("Female dancer is: " + person.name); person = males.dequeue(); console.log(" and the male dancer is: " + person.name); } } /** *测试程序: */ var maleDancers = new Queue(); var femaleDancers = new Queue(); getDancers(maleDancers, femaleDancers); dance(maleDancers, femaleDancers); if (!femaleDancers.empty()) { print(femaleDancers.front().name + " is waiting to dance."); } if (!maleDancers.empty()) { print(maleDancers.front().name + " is waiting to dance."); } //显示等候跳舞的人数 if (maleDancers.count() > 0) { print("There are " + maleDancers.count() +" male dancers waiting to dance."); } if (femaleDancers.count() > 0) { print("There are " + femaleDancers.count() +" female dancers waiting to dance."); } </script>
2.
标签:存储 一个人 queue类 view while 数据 hid 使用 color
原文地址:http://www.cnblogs.com/tenWood/p/7215502.html