数据结构与算法JavaScript描述[第五章](队列)
2017-01-04 16:42
399 查看
//队列基类 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();
相关文章推荐
- 队列的实现 -- 数据结构与算法的javascript描述 第五章
- 数据结构 C#描述 第五章 栈和队列
- 数据结构与算法C#语言描述第五章栈和队列
- 数据结构与算法C#语言描述第五章栈和队列
- 队列 - C语言实现(摘自数据结构与算法分析 C语言描述)
- c语言描述的链队列的基本操作
- 第五章Linux消息队列
- C# 线程手册 第五章 多线程应用程序 一个多线程微软消息队列(MSMQ)监听器
- [翻译]C#数据结构与算法 – 第五章栈与队列(Part 1)
- 队列-Java描述
- [C++]数据结构:公式化描述的队列Queue类
- 数据结构——c语言描述 第三章(3) 队列
- 数据结构与算法分析(Java 语言描述)(35)—— 使用两个栈实现一个队列
- 数据结构——13 队列链表描述
- [翻译]C#数据结构与算法 – 第五章栈与队列(Part 1)
- [翻译]C#数据结构与算法 – 第五章栈与队列(Part 2)
- PVFS2 源代码分析之输入输出src/io/job/job-desc-queue任务描述符队列
- 优先级队列-Java描述
- 第五章:最后一步准备,1.8的Json模型、状态描述机制详解
- [C++]数据结构:链表描述的队列LinkedQueue类