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

本文共 2881 字,大约阅读时间需要 9 分钟。

队列的实现

队列是一种先进先出的数据结构,常用于操作系统和网络编程中。以下是基于 JavaScript 的队列实现代码:

function Deque() {    this.dataStore = [];    this.enqueueFront = enqueueFront;    this.enqueueBack = enqueueBack;    this.dequeueFront = dequeueFront;    this.dequeueBack = dequeueBack;    this.front = function() {        return this.dataStore[0];    };    this.back = function() {        return this.dataStore[this.dataStore.length - 1];    };    this.toString = function() {        let retStr = '';        for (let i = 0; i < this.dataStore.length; i++) {            retStr += `${this.dataStore[i]}\n`;        }        return retStr;    };    this.empty = function() {        return this.dataStore.length === 0;    };}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 Deque() {    this.dataStore = [];    this.enqueueFront = enqueueFront;    this.enqueueBack = enqueueBack;    this.dequeueFront = dequeueFront;    this.dequeueBack = dequeueBack;    this.front = function() {        return this.dataStore[0];    };    this.back = function() {        return this.dataStore[this.dataStore.length - 1];    };    this.toString = function() {        let retStr = '';        for (let i = 0; i < this.dataStore.length; i++) {            retStr += `${this.dataStore[i]}\n`;        }        return retStr;    };    this.empty = function() {        return this.dataStore.length === 0;    };}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);}

测试代码

let d = new Deque();d.enqueueFront('a');d.enqueueFront('b');d.enqueueFront('c');d.enqueueFront('d');d.enqueueFront('e');console.log(d.toString()); // 输出: e, d, c, b, ad.enqueueBack('a');d.enqueueBack('b');d.enqueueBack('c');d.enqueueBack('d');d.enqueueBack('e');console.log(d.toString()); // 输出: e, d, c, b, a, a, b, c, d, ed.dequeueFront();d.dequeueFront();console.log(d.toString()); // 输出: c, b, a, a, b, c, d, ed.dequeueBack();d.dequeueBack();d.dequeueBack();console.log(d.toString()); // 输出: c, b, a, a, b

练习二:判断单词是否是回文

使用双向队列,可以轻松判断一个单词是否为回文。以下是实现代码:

function isPalindrome(word) {    let d = new Deque();    for (let i = 0; i < word.length; i++) {        d.enqueueBack(word[i]);    }    while (d.dataStore.length > 1) {        if (d.dequeueFront() !== d.dequeueBack()) {            return false;        }    }    return true;}// 测试示例console.log(isPalindrome('racecar')); // trueconsole.log(isPalindrome('ada'));    // trueconsole.log(isPalindrome('mazey')); // false

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

你可能感兴趣的文章
Nginx 配置解析:从基础到高级应用指南
查看>>
Nginx下配置codeigniter框架方法
查看>>
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:128
查看>>
nginx添加模块与https支持
查看>>
Nginx用户认证
查看>>
Nginx的Rewrite正则表达式,匹配非某单词
查看>>
Nginx的使用总结(一)
查看>>
Nginx的可视化神器nginx-gui的下载配置和使用
查看>>
Nginx的是什么?干什么用的?
查看>>
Nginx访问控制_登陆权限的控制(http_auth_basic_module)
查看>>
nginx负载均衡器处理session共享的几种方法(转)
查看>>
nginx负载均衡的5种策略(转载)
查看>>
nginx负载均衡的五种算法
查看>>
Nginx运维与实战(二)-Https配置
查看>>
Nginx配置ssl实现https
查看>>
Nginx配置TCP代理指南
查看>>
Nginx配置——不记录指定文件类型日志
查看>>
Nginx配置代理解决本地html进行ajax请求接口跨域问题
查看>>
Nginx配置参数中文说明
查看>>
Nginx配置好ssl,但$_SERVER[‘HTTPS‘]取不到值
查看>>