标签:ack while 出队 += 数据 OLE name amp -o
//算法——队列 /* * 队列是一种列表,不同的是队列只能在队尾插入元素,在队首删除元素。 * 队列是一种先进先出(First-In-First-Out,FIFO)的数据结构 * 向队列中插入新元素和删除队列中的元素。插入操作也叫做入 队,删除操作也叫做出队 enqueue: 入列 dequeue: 出列 front: 获取列首元素 back: 获取列尾元素 toString: 显示队列所有元素 empty: 判断队列是否为空 * * */ function enqueue(element) { this.dataStore.push(element); } function dequeue() { return this.dataStore.shift(); } function front() { return this.dataStore[0]; } function back() { var len = this.dataStore.length; return this.dataStore[len-1]; } function toString() { var retStr = ""; for(var i = 0; i< this.dataStore.length; i++){ retStr += this.dataStore[i] + "\n"; } return retStr; } function empty() { return this.dataStore.length === 0; } function Queue() { this.dataStore = []; this.enqueue = enqueue; this.dequeue = dequeue; this.front = front; this.back = back; this.toString = toString; this.empty = empty; } // var q = new Queue(); // q.enqueue("张三"); // q.enqueue("李四"); // q.enqueue("王五"); var fs = require("fs"), females = new Queue(), males = new Queue(); var danceList = fs.readFileSync("./dance.txt","utf-8").split("\n"), len = danceList.length; for(var i = 0; i< len ; i++) { danceList[i] = danceList[i].trim(); var sex = danceList[i].split(" ")[0]; var name = danceList[i].split(" ")[1]; if (sex === "F") { females.enqueue({sex: sex,name: name}); }else { males.enqueue({sex: sex,name: name}); } } (function dance() { console.log("The dance partners are: "); while (!females.empty() && !males.empty()) { console.log("男舞伴是" + females.dequeue().name,"女舞伴是" + males.dequeue().name); } })();
标签:ack while 出队 += 数据 OLE name amp -o
原文地址:https://www.cnblogs.com/yunnex-xw/p/9831005.html