博客
关于我
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/

你可能感兴趣的文章
Node.js 在个推的微服务实践:基于容器的一站式命令行工具链
查看>>
Node.js 实现类似于.php,.jsp的服务器页面技术,自动路由
查看>>
Node.js 异步模式浅析
查看>>
node.js 怎么新建一个站点端口
查看>>
Node.js 文件系统的各种用法和常见场景
查看>>
Node.js 模块系统的原理、使用方式和一些常见的应用场景
查看>>
Node.js 的事件循环(Event Loop)详解
查看>>
node.js 简易聊天室
查看>>
Node.js 线程你理解的可能是错的
查看>>
Node.js 调用微信公众号 API 添加自定义菜单报错的解决方法
查看>>
node.js 配置首页打开页面
查看>>
node.js+react写的一个登录注册 demo测试
查看>>
Node.js中环境变量process.env详解
查看>>
Node.js之async_hooks
查看>>
Node.js也分裂了-开源社区动态
查看>>
Node.js初体验
查看>>
Node.js升级工具n
查看>>
Node.js卸载超详细步骤(附图文讲解)
查看>>
Node.js卸载超详细步骤(附图文讲解)
查看>>
Node.js基于Express框架搭建一个简单的注册登录Web功能
查看>>