
JavaScript数据结构与算法-队列练习
发布日期:2021-05-09 05:14:09
浏览次数:11
分类:博客文章
本文共 3185 字,大约阅读时间需要 10 分钟。
队列的实现
// 队列类function Deque () { this.dataStore = []; this.enqueueFront = enqueueFront; this.enqueueBack = enqueueBack; this.dequeueFront = dequeueFront; this.dequeueBack = dequeueBack; this.front = front; this.back = back; this.toString = toString; this.empty = empty;}// 入队 - 队首function enqueueFront (element) { this.dataStore.unshift(element);}// 出队 - 队首function dequeueFront () { return this.dataStore.shift();}// 入队 - 队尾function enqueueBack (element) { this.dataStore.push(element);}// 出队 - 队尾function dequeueBack (element) { return this.dataStore.pop(element);}// 读取队首的元素function front () { return this.dataStore[0];}// 读取队尾的元素function back () { return this.dataStore[this.dataStore.length - 1];}// 显示队列内所有元素function toString () { let retStr = ``; for (let 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; }}
练习
一. 修改Queue类,形成一个Deque类。这是一个和队列类似的数据结构,允许从队列两端添加和删除元素,因此也叫双向队列。写一段测试程序测试该类。
// 双向队列类function Deque () { this.dataStore = []; this.enqueueFront = enqueueFront; this.enqueueBack = enqueueBack; this.dequeueFront = dequeueFront; this.dequeueBack = dequeueBack; this.front = front; this.back = back; this.toString = toString; this.empty = empty;}// 入队 - 队首function enqueueFront (element) { this.dataStore.unshift(element);}// 出队 - 队首function dequeueFront () { return this.dataStore.shift();}// 入队 - 队尾function enqueueBack (element) { this.dataStore.push(element);}// 出队 - 队尾function dequeueBack (element) { return this.dataStore.pop(element);}// 读取队首的元素function front () { return this.dataStore[0];}// 读取队尾的元素function back () { return this.dataStore[this.dataStore.length - 1];}// 显示队列内所有元素function toString () { let retStr = ``; for (let 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; }}// 测试let d = new Deque();d.enqueueFront(`a`);d.enqueueFront(`b`);d.enqueueFront(`c`);d.enqueueFront(`d`);d.enqueueFront(`e`);console.log(d.dataStore); // ["e", "d", "c", "b", "a"]d.enqueueBack(`a`);d.enqueueBack(`b`);d.enqueueBack(`c`);d.enqueueBack(`d`);d.enqueueBack(`e`);console.log(d.dataStore); // ["e", "d", "c", "b", "a", "a", "b", "c", "d", "e"]d.dequeueFront();d.dequeueFront();console.log(d.dataStore); // ["c", "b", "a", "a", "b", "c", "d", "e"]d.dequeueBack();d.dequeueBack();d.dequeueBack();console.log(d.dataStore); // ["c", "b", "a", "a", "b"]
二. 使用前面完成的Deque类来判断一个给定单词是否为回文。
function isPalindrom (word) { let d = new Deque(); let max = word.length; for (let i = 0; i < max; ++i) { d.enqueueBack(word[i]); } while (d.dataStore.length > 1) { if (d.dequeueFront() !== d.dequeueBack()) { return false; } } return true;}// 示例console.log(isPalindrom(`racecar`)); // trueconsole.log(isPalindrom(`ada`)); // trueconsole.log(isPalindrom(`mazey`)); // false
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2025年04月08日 22时16分32秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
别乱提交代码了,看下大厂 Git 提交规范是怎么做的!
2021-05-09
在滴滴和头条干了 2 年后端开发,太真实…
2021-05-09
送给你 12 个 Git 使用技巧!
2021-05-09
使用 Redis 实现一个轻量级的搜索引擎,牛逼!
2021-05-09
你还在用分页?试试 MyBatis 流式查询,真心强大!
2021-05-09
你还在用命令看日志?快用 Kibana 吧,一张图片胜过千万行日志!
2021-05-09
python进阶(3)json文件与python字典的转化
2021-05-09
Linux中对用户操作
2021-05-09
大数据整理——数据集成
2021-05-09
Linux查看CUDA和cuDNN版本
2021-05-09
centos修改mysql5.7默认端口后启动异常
2021-05-09
java面试系列<4>——IO
2021-05-09
来讲讲你对ThreadLocal的理解
2021-05-09
No.017:Letter Combinations of a Phone Number
2021-05-09
No.021:Merge Two Sorted Lists
2021-05-09
RESTful API 介绍,设计
2021-05-09
asp.net中用FileStream类实现下载文件功能,自定义下载路径,像IE下载一样
2021-05-09
C#获取Excel中所有的Sheet名称
2021-05-09
unity3d由于Camera.main.transform报空引用错误的解决方案
2021-05-09
SQL Syscolumns
2021-05-09