//队列基类 function Queue(){ var _this = this; this.dataStore = []; this.enqueue = function(element){ this.dataStore.push(element); }; this.dequeue = function () { this.dataStore.shift(); } this.front = function () { return this.dataStore[0]; } this.back = function () { return this.dataStore[this.dataStore.length - 1]; } this.toString = function () { var str = ''; for(var i = 0; i < this.dataStore.length; i++){ str += this.dataStore[i]; } return str; } this.empty = function () { if(this.dataStore.length == 0){ return true; }else{ return false; } } }
1.修改Queue类,行程一个Deque类。这是一个和队列类似的数据结构,允许从队列两端添加和删除元素,因此也叫双向队列。写一段测试程序测试该类。
//双向队列
function Deque() {};
//继承队列
Deque.prototype = new Queue();
//重写队列 dequeue 方法
Deque.prototype.dequeue = function (direction) {
var _this = this;
var strategy = {
'l':function () {
return _this.dataStore.shift();
},
'r':function () {
return _this.dataStore.pop();
}
};
return strategy[direction]();
}
//重写队列 enqueue 方法
Deque.prototype.enqueue = function (direction,element) {
var _this = this;
var strategy = {
'l':function () {
_this.dataStore.unshift(element);
},
'r':function () {
_this.dataStore.push(element);
}
};
strategy[direction]();
}
var d = new Deque();
//两端添加测试
d.enqueue('r','right');
d.enqueue('r','right');
d.enqueue('l','left');
d.enqueue('r','right');
//两端删除测试
d.dequeue('l');
d.dequeue('r');
console.log(d.dataStore);
2.使用前面完成的Deque类来判断一个给定单词是否为回文。
//判断是否为回文 function Palindrome() {}; Palindrome.prototype = new Deque(); //判断是否为回文 Palindrome.prototype.isPalindrome = function (word) { this.dataStore = []; for(var i = 0; i < word.length; i++){ this.enqueue('l',word.charAt(i)); } var left = new Deque(); var right = new Deque(); left.dataStore = this.dataStore.concat(); right.dataStore = this.dataStore.concat(); var lt = []; var rt = []; for(var i = 0,len = left.dataStore.length; i < len; i++){ lt.push(left.dequeue('l')); rt.push(right.dequeue('r')); } if(lt.join('') == rt.join('')){ console.log('is'); }else{ console.log('no'); } } new Palindrome().isPalindrome('abc'); new Palindrome().isPalindrome('aca'); new Palindrome().isPalindrome('aca');
3.修改例5-5中的有限队列,使得优先级搞得元素优先吗也打。写一段程序测试你的改动。var PriorQueue = function () {}; PriorQueue.prototype = new Queue(); PriorQueue.prototype.dequeue = function () { var prior = this.dataStore[0].code; var index = 0; var max = this.dataStore[0].code; for(var i = 0; i < this.dataStore.length; i++){ var item = this.dataStore[i]; if(item.code > max){ max = item.code; index = i; } } return this.dataStore.splice(index,1); } var priorQueue = new PriorQueue(); priorQueue.enqueue(new Patient('a',30)); priorQueue.enqueue(new Patient('b',40)); priorQueue.enqueue(new Patient('c',20)); priorQueue.enqueue(new Patient('d',20)); priorQueue.enqueue(new Patient('e',80)); console.log(priorQueue.dequeue());
4.修改5-5中的候诊室程序,使得候诊室内的活动可以被控制。写一个类似菜单系统,让用户可以进行如下选择:a.患者进入候诊室
b.患者就诊
c.显示等待就诊患者名单
//就诊程序 var Visits = function () {}; Visits.prototype = new PriorQueue(); //让患者进入候诊室 // params 1 代表患者名字 // params 2 代表患者优先级 Visits.prototype.in = function (name,prior) { this.enqueue(new Patient(name,prior)); } //患者就诊 Visits.prototype.visit = function () { return this.dequeue(); } //显示患者名单 Visits.prototype.showList= function () { console.log(this.dataStore); } /*假设有以下患者 * */ var visit = new Visits(); visit.in('a',30); visit.in('b',40); visit.in('c',20); visit.in('e',10); console.log(visit.dequeue()); visit.showList();