京东6.18大促主会场领京享红包更优惠

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 7143|回复: 0

javascript实现双端队列

[复制链接]

24

主题

0

回帖

10

积分

新手上路

积分
10
发表于 2021-11-12 08:29:32 | 显示全部楼层 |阅读模式 来自 中国
本文实例为大家分享了javascript实现双端队列的具体代码,供大家参考,具体内容如下% I3 \; _& P6 Q6 J+ P$ X4 ~
1.双端队列+ @4 R$ r7 C& z# f, k
% I8 t" B0 }$ _! y6 Q+ R  F

7 G8 P5 J/ b- G! o2 `1 S, W双端队列是一种允许我们同时从前端和后端添加和移除元素的特殊队列5 S! k2 Q5 `& M( l
2.双端队列的应用% j/ E, a9 K, {

* u3 Y6 T' O( \( ]& ?* Z( s& H1 [8 b. V# m
一个刚买了票的入如果只是还需要再问一些简单的信息,就可以直接回到队伍头部,另外队伍末尾的人如果赶时间也可以直接离开队伍& @! W# y' ]  L( u+ }! H# w
3.双端队列的方法5 r5 T" ^! F, {+ g0 f& U

* P: L1 S( y1 Y6 l! z! ?
" r& g6 d; t9 L# e7 f3 V" gaddFront(element):该方法在双端队列前端添加新的元素
6 l+ {4 y! F4 `$ oaddBack(element):该方法在双端队列后端添加新的元素(实现方法和 Queue 类中的enqueue 方法相同)。
1 g5 K  J- z) ~& P5 d( GremoveFront():该方法会从双端队列前端移除第一个元素9 X7 z# a7 u  Z: m" Z! d( P
removeBack():该方法会从双端队列的后端移除第一个元素  d0 X. g( x7 ?' o0 j, A, A) s- [3 p
peekFront():该方法返回双端队列的第一个元素。6 L( p2 g! b0 R; [6 M7 `
peekBack()):该方法返回双端队列后端的第一个元素。
+ S* s+ V+ D5 Y4.实现
+ U# c  N1 g* b0 V
- j$ A) _1 E6 L[code]class Deque{           constructor(){               this.items = {};               this.count = 0;               this.lowestCount = 0;            }        // 在双端队列前端添加新元素        addFront(element){            if(this.isEmpty()){                this.addBack(element);            }            else if(this.lowestCount > 0){                this.lowestCount -- ;                this.items[this.lowestCount] = element;            }            else{                for(let i=this.count;i>0;i--){                    this.items = this.items[i-1];                 }                this.lowestCount = 0;                this.items[this.lowestCount] = element;                this.count++;            }        };        addBack(element){            this.count++;            this.items[this.count-1] = element;        };        removeFront(){            if(this.isEmpty()){                return undefined;            }            const result = this.items[this.lowestCount];            delete this.items[this.lowestCount];            this.lowestCount++;            return result;        };        removeBack(){            if(this.isEmpty()){                return undefined;            }            const result = this.items[this.count-1];            delete this.items[this.count-1];            this.count--;            return result;        };        peekFront(){            if(this.isEmpty()){                return null;            }          return   this.items[this.lowestCount];        };        peekBack(){            if(this.isEmpty()){                return null;            }            return this.items[this.count-1];        };        isEmpty(){            return this.count - this.lowestCount == 0;        }        size(){            return  this.count - this.lowestCount;        }        toString(){            if(this.isEmpty()){                return '';            }            let objString = `${this.items[this.lowestCount]}`;            for(var i=this.lowestCount+1;i

帖子地址: 

梦想之都-俊月星空 优酷自频道欢迎您 http://i.youku.com/zhaojun917
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|梦想之都-俊月星空 ( 粤ICP备18056059号 )|网站地图

GMT+8, 2025-11-10 10:34 , Processed in 0.039360 second(s), 21 queries .

Powered by Mxzdjyxk! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表