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

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 7204|回复: 0

javascript实现双端队列

[复制链接]

24

主题

0

回帖

10

积分

新手上路

积分
10
发表于 2021-11-12 08:29:32 | 显示全部楼层 |阅读模式 来自 中国
本文实例为大家分享了javascript实现双端队列的具体代码,供大家参考,具体内容如下
, }: |% Z0 O0 {% ]1.双端队列" d* \! u# I# l7 l5 ~" I

" z. z! u+ k7 m# p+ b; R
& `, g# G, q2 Y" e3 Q双端队列是一种允许我们同时从前端和后端添加和移除元素的特殊队列
* y+ P( i6 g7 s( Y* S# Z2.双端队列的应用+ n. s  x) y6 L

( N5 B6 ]6 F8 j2 [+ z6 H) C% P: m. ]5 R* L: M/ G+ ^
一个刚买了票的入如果只是还需要再问一些简单的信息,就可以直接回到队伍头部,另外队伍末尾的人如果赶时间也可以直接离开队伍" w8 S3 P; K* k- M9 P  [
3.双端队列的方法5 _& Q: ]# [, Y% ~+ d
* u$ K* l$ K& a% i

9 G# Z% i+ \1 Q! H% ~addFront(element):该方法在双端队列前端添加新的元素7 j4 S4 L( N. N2 h
addBack(element):该方法在双端队列后端添加新的元素(实现方法和 Queue 类中的enqueue 方法相同)。( A" U6 L9 |& c8 T' M$ Z+ O
removeFront():该方法会从双端队列前端移除第一个元素
" i* ?9 X, d  ]removeBack():该方法会从双端队列的后端移除第一个元素* I' n8 f% K5 }3 D3 P- V
peekFront():该方法返回双端队列的第一个元素。
$ \3 I0 x0 f& n1 rpeekBack()):该方法返回双端队列后端的第一个元素。) ^8 q  [! Y2 n( V' n2 t# \
4.实现
/ j7 ~% V, o/ f# C0 s2 ?, i) I7 U* k+ g$ b9 d; N6 Z$ {/ F# _4 e
[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, 2026-6-22 21:23 , Processed in 0.038693 second(s), 23 queries .

Powered by Mxzdjyxk! X3.5

© 2001-2026 Discuz! Team.

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