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

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 7236|回复: 0

javascript实现双端队列

[复制链接]

24

主题

0

回帖

10

积分

新手上路

积分
10
发表于 2021-11-12 08:29:32 | 显示全部楼层 |阅读模式 来自 中国
本文实例为大家分享了javascript实现双端队列的具体代码,供大家参考,具体内容如下
  _8 m4 a* U1 z/ c& ~& m1.双端队列
6 u; M- s3 o/ S* _5 y

3 a, Q' p- P* J+ g8 W. X1 }6 c5 O& q7 U
双端队列是一种允许我们同时从前端和后端添加和移除元素的特殊队列4 Y4 k$ J& v& a0 I0 o  C
2.双端队列的应用
& Z- r8 J( x& {8 r) ]( c5 w
& p/ b& z. d2 G* ?7 h5 y

% ^4 R" ^/ y; L9 f& v7 x一个刚买了票的入如果只是还需要再问一些简单的信息,就可以直接回到队伍头部,另外队伍末尾的人如果赶时间也可以直接离开队伍( [3 V- ^1 K' u6 |$ {7 h
3.双端队列的方法
! z- A; R, u6 S6 H+ D

0 W) g: y2 S: V1 k" E+ Y2 Y( K1 S4 W: b
addFront(element):该方法在双端队列前端添加新的元素
' n) x8 M3 Z" s& p0 W6 waddBack(element):该方法在双端队列后端添加新的元素(实现方法和 Queue 类中的enqueue 方法相同)。/ j8 W; J1 r, Q
removeFront():该方法会从双端队列前端移除第一个元素% f3 a+ Q% j, \3 w
removeBack():该方法会从双端队列的后端移除第一个元素
( w" |2 K* f$ ?3 w* Z+ |peekFront():该方法返回双端队列的第一个元素。  A$ N, B; N/ P1 j; y! o
peekBack()):该方法返回双端队列后端的第一个元素。
) ~1 J+ P+ u3 x. ?! W+ G: O4.实现& s/ d' Z: S; O& G6 X" s

# u) f7 U& t; E! a  T% t[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-10-21 16:36 , Processed in 0.047182 second(s), 23 queries .

Powered by Mxzdjyxk! X3.5

© 2001-2026 Discuz! Team.

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