博客
关于我
JavaScript数据结构与算法-队列练习
阅读量:438 次
发布时间:2019-03-06

本文共 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

转载地址:http://gxmyz.baihongyu.com/

你可能感兴趣的文章
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:128
查看>>
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in usrlocalnginxconfnginx.conf128
查看>>
nginx日志分割并定期删除
查看>>
Nginx日志分析系统---ElasticStack(ELK)工作笔记001
查看>>
Nginx映射本地json文件,配置解决浏览器跨域问题,提供前端get请求模拟数据
查看>>
nginx最最最详细教程来了
查看>>
Nginx服务器---正向代理
查看>>
Nginx服务器上安装SSL证书
查看>>
Nginx服务器基本配置
查看>>
Nginx服务器的安装
查看>>
Nginx模块 ngx_http_limit_conn_module 限制连接数
查看>>
Nginx模块 ngx_http_limit_req_module 限制请求速率
查看>>
nginx添加模块与https支持
查看>>
Nginx用户认证
查看>>
Nginx的location匹配规则的关键问题详解
查看>>
Nginx的Rewrite正则表达式,匹配非某单词
查看>>
Nginx的使用总结(一)
查看>>
Nginx的使用总结(三)
查看>>
Nginx的使用总结(二)
查看>>
Nginx的可视化神器nginx-gui的下载配置和使用
查看>>