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

你可能感兴趣的文章
NIFI集群_内存溢出_CPU占用100%修复_GC overhead limit exceeded_NIFI: out of memory error ---大数据之Nifi工作笔记0017
查看>>
NIFI集群_队列Queue中数据无法清空_清除队列数据报错_无法删除queue_解决_集群中机器交替重启删除---大数据之Nifi工作笔记0061
查看>>
NIH发布包含10600张CT图像数据库 为AI算法测试铺路
查看>>
Nim教程【十二】
查看>>
Nim游戏
查看>>
NIO ByteBuffer实现原理
查看>>
Nio ByteBuffer组件读写指针切换原理与常用方法
查看>>
NIO Selector实现原理
查看>>
nio 中channel和buffer的基本使用
查看>>
NIO基于UDP协议的网络编程
查看>>
NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
查看>>
Nitrux 3.8 发布!性能全面提升,带来非凡体验
查看>>
NI笔试——大数加法
查看>>
NLog 自定义字段 写入 oracle
查看>>
NLog类库使用探索——详解配置
查看>>
NLP 基于kashgari和BERT实现中文命名实体识别(NER)
查看>>
NLP 项目:维基百科文章爬虫和分类【01】 - 语料库阅读器
查看>>
NLP_什么是统计语言模型_条件概率的链式法则_n元统计语言模型_马尔科夫链_数据稀疏(出现了词库中没有的词)_统计语言模型的平滑策略---人工智能工作笔记0035
查看>>
NLP学习笔记:使用 Python 进行NLTK
查看>>
NLP的神经网络训练的新模式
查看>>