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

你可能感兴趣的文章
ng 指令的自定义、使用
查看>>
nghttp3使用指南
查看>>
Nginx
查看>>
nginx + etcd 动态负载均衡实践(一)—— 组件介绍
查看>>
nginx + etcd 动态负载均衡实践(三)—— 基于nginx-upsync-module实现
查看>>
nginx + etcd 动态负载均衡实践(二)—— 组件安装
查看>>
nginx + etcd 动态负载均衡实践(四)—— 基于confd实现
查看>>
Nginx + Spring Boot 实现负载均衡
查看>>
Nginx + Tomcat + SpringBoot 部署项目
查看>>
Nginx + uWSGI + Flask + Vhost
查看>>
Nginx - Header详解
查看>>
Nginx - 反向代理、负载均衡、动静分离、底层原理(案例实战分析)
查看>>
Nginx - 反向代理与负载均衡
查看>>
nginx 1.24.0 安装nginx最新稳定版
查看>>
nginx 301 永久重定向
查看>>
nginx connect 模块安装以及配置
查看>>
nginx css,js合并插件,淘宝nginx合并js,css插件
查看>>
Nginx gateway集群和动态网关
查看>>
Nginx Location配置总结
查看>>
Nginx log文件写入失败?log文件权限设置问题
查看>>