干疮是什么样子的图片| 珠联璧合是什么意思| 稀饭配什么菜好吃| 悠闲惬意什么意思| 什么是沉香木| 头眩晕是什么原因引起的| 小儿厌食吃什么药最好| 扁桃体挂什么科| 吃了火龙果不能吃什么| 焦虑症吃什么好| o型血容易得什么病| 拉稀肚子疼吃什么药| 尿酸高能吃什么肉| 气是什么意思| 梦见打蛇是什么预兆| 办离婚证需要带什么证件| 坏是什么意思| 发飙是什么意思| 感冒拉肚子吃什么药| 癌胚抗原高是什么意思| 唇炎看什么科室| 啪啪啪什么意思| 想法是什么意思| 多囊是什么症状| slay是什么意思| 亚甲炎吃什么药效果好| 开黑什么意思| 月经期体重增加是什么原因| 最难写的字是什么| 寻常疣用什么药膏除根| 什么奶茶好喝| 米酒和黄酒有什么区别| 三个火读什么字| 大姨妈来了喝红糖水有什么功效| 北京豆汁儿什么味道| 精子对女性有什么好处| 为什么会得肾构错瘤| 萨德事件是什么意思| 美国为什么有哥伦比亚| 血小板低是什么问题| 93年的属什么| 经常头晕头疼是什么原因| 通风什么征兆| 东方蝾螈吃什么| 背包客是什么意思| 双肾泥沙样结石是什么意思| 12月15是什么星座| 三尖瓣反流什么意思| 什么叫戒断反应| 9月9日是什么星座| 腮腺炎看什么科室| 什么叫基因检测| 18岁属什么的生肖| 说话口臭是什么原因引起的| 梦见自己爷爷死了是什么预兆| 尽善尽美是什么生肖| 木瓜是什么季节的| 吃什么润肺| 艾滋病中期有什么症状| 夫妻肺片有什么材料| 浸猪笼是什么意思| 每个月月经都推迟是什么原因| hpv病毒是什么意思| 胰岛素是干什么的| 跳大神是什么意思| 心悸心慌吃什么药最好| 孩子打呼噜是什么原因| 小宝贝是什么意思| 什么烟好抽又便宜| 第二磨牙什么时候长| darling什么意思| 转氨酶高吃什么药效果好| 静推是什么意思| 脑鸣吃什么药最有效| 什么是毛囊炎| 中午喜鹊叫有什么预兆| 分别心是什么意思| 什么动物是爸爸生的| 甘油三脂高是什么意思| 劲酒加红牛有什么功能| 百香果有什么营养| 茄子吃多了有什么坏处| 感冒头疼吃什么药| 胎盘吃了对身体有什么好处| 支气管扩张什么症状| 免疫力低挂什么科| 咳嗽吃什么食物好得最快最有效| 大便是红色的是什么原因| 宝宝吃什么鱼比较好| 甘薯和红薯有什么区别| 怀孕10天左右有什么症状| 什么大专好就业| 126是什么邮箱| 同仁什么意思| 学制是什么| 压箱底是什么意思| 属马的生什么属相的宝宝好| 嗯是什么意思| 维生素e吃多了有什么副作用| 生吃大葱有什么好处和坏处| 骨结核是什么病| 接吻是什么感觉| 233什么意思| 直接胆红素高是什么病| a型rhd阳性是什么意思| 加湿器有什么作用| 牙体牙髓科看什么| 脂膜炎是什么原因引起的| 农历六月初十是什么日子| 健康管理是什么专业| 什么是犹太人| 为什么头痛| 什么叫cd| 月经不正常去医院检查什么项目| 7.2什么星座| 胃溃疡什么症状| 未加一笔是什么字| 儿童拖鞋什么材质好| 归宁是什么意思| 梦见前男友死了是什么意思| 距离产生美是什么意思| 肝的反射区在什么部位| 副县级是什么级别| 学生近视配什么镜片好| 承上启下是什么意思| 清蒸什么鱼好吃| 投其所好是什么意思| 令是什么生肖| 咳嗽一直不好什么原因| 宝宝什么时候可以吃盐| 低钠有什么症状和危害| 趋是什么意思| 橄榄油有什么作用| 西班牙用的是什么货币| 琼花是什么意思| 神经内科主要看什么病| 腰间盘突出压迫神经什么症状| 浊气是什么| 折寿是什么意思| 喝大麦茶有什么好处| 空性是什么意思| 体重除以身高的平方是什么指数| 宫腔粘连有什么症状| 大型血小板比率偏低是什么意思| 女生无缘无故头疼是什么原因| 望而生畏什么意思| 前胸后背出汗多是什么原因| 退烧吃什么药好| 口干口苦吃什么中成药| 左手麻木什么原因| 排气是什么意思| 1989年五行属什么| 北京有什么好吃的美食| 咖啡有什么功效| 伯母是什么意思| 今年三十岁属什么生肖| 甲状腺分泌什么激素| 血脂稠吃什么药最好| 子宫脱垂有什么症状| 赛诺菲是什么药| 弟弟是什么意思| 海胆是什么东西| 十二指肠球炎是什么意思| 彩金是什么金| 麦冬和什么相克| 神甫是什么意思| 鬼是什么意思| 拉肚子适合吃什么食物| 洗衣机脱水是什么意思| 孕妇梦见捡鸡蛋是什么意思| 露酒是什么酒| 夏季养什么脏腑| 冰冻三尺非一日之寒是什么意思| 对联又称什么| apf值是什么意思| 膝盖痒是什么原因| 什么水果对心脏有好处| 甲状腺有什么症状| 脑部ct挂什么科| 右耳痒是什么预兆| 举案齐眉是什么意思| 苹果越狱是什么意思啊| 什么石头最值钱| 女主是什么意思| 医院规培生是什么意思| 悦人不如悦己什么意思| 尖锐湿疣的症状是什么| 肌肉萎缩挂什么科| 冷暴力是什么| pe什么材质| 男性生殖器叫什么| 阑尾粪石是什么| 什么叫精索静脉曲张啊| 脚气用什么| 周正是什么意思| 子宫内膜息肉样增生是什么意思| 乙肝恢复期是什么意思| 洋字五行属什么| 口腔溃疡是什么症状| 二氧化硅是什么晶体| 神经性皮炎用什么药膏效果最好| 淋巴细胞高是什么原因| 手麻是什么原因引起| 鸡冠油是什么| 上山下乡是什么意思| 青椒炒什么好吃| 水色是什么颜色| 氮泵有什么作用| 杜甫自号什么| ins是什么社交软件| 手指脱皮是什么原因引起的| 执念是什么意思| 鸳鸯戏水是什么意思| 梅菜在北方叫什么菜| 心脏大是什么病严重吗| 为什么排卵期会出血| 滴水观音叶子发黄是什么原因| 层林尽染是什么季节| 孝顺的真正含义是什么| 金针菇为什么叫明天见| 中分化是什么意思| 穿山甲是什么动物| dpo是什么意思| 围魏救赵是什么意思| 说什么| 葡萄什么季节成熟| 泛化是什么意思| 老年人尿血是什么原因| 大黄米和小黄米有什么区别| 浙江大学什么专业最好| 什么是植物人| 杨梅泡酒有什么功效和作用| 手指缝里长水泡还痒是什么原因| 宝宝佛适合什么人戴| 仙代表什么生肖| 高血压属于什么科| 秋葵炒什么好吃| 肌酸激酶偏高吃什么药| 二甲双胍为什么晚上吃| 头爱出汗是什么原因| std什么意思| 老鸨是什么意思| 单核细胞偏低是什么意思| 信指什么生肖| 手掌心经常出汗是什么原因| 嫩黄的什么| 什么是甲醛| vte是什么| 意味深长是什么意思| 肠系膜淋巴结是什么病| 佛法是什么意思| 烧仙草是什么植物| 科长是什么级别| 梦见家里着火了是什么征兆| 琅琊榜是什么意思| 灰色五行属什么| 白砂糖和冰糖有什么区别| 主食是什么意思| 氧氟沙星和诺氟沙星有什么区别| 西瓜什么时候传入中国| 牛肉和什么相克| 开导是什么意思| 腰痛吃什么好| 嘴巴苦什么原因| 百度

“地球一小时”活动在各地举行(组图)

百度 对于剧中密集上演的重口味“推倒”桥段,片方曾坦言就是以此为噱头和看点。

In computer science, a queue is a collection of entities that are maintained in a sequence and can be modified by the addition of entities at one end of the sequence and the removal of entities from the other end of the sequence. By convention, the end of the sequence at which elements are added is called the back, tail, or rear of the queue, and the end at which elements are removed is called the head or front of the queue, analogously to the words used when people line up to wait for goods or services.

Queue
Representation of a FIFO (first in, first out) queue
Time complexity in big O notation
Operation Average Worst case
Search O(n) O(n)
Insert O(1) O(1)
Delete O(1) O(1)
Space complexity
Space O(n) O(n)

The operation of adding an element to the rear of the queue is known as enqueue, and the operation of removing an element from the front is known as dequeue. Other operations may also be allowed, often including a peek or front operation that returns the value of the next element to be dequeued without dequeuing it.

The operations of a queue make it a first-in-first-out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed. A queue is an example of a linear data structure, or more abstractly a sequential collection. Queues are common in computer programs, where they are implemented as data structures coupled with access routines, as an abstract data structure or in object-oriented languages as classes.

A queue has two ends, the top, which is the only position at which the push operation may occur, and the bottom, which is the only position at which the pop operation may occur. A queue may be implemented as circular buffers and linked lists, or by using both the stack pointer and the base pointer.

Queues provide services in computer science, transport, and operations research where various entities such as data, objects, persons, or events are stored and held to be processed later. In these contexts, the queue performs the function of a buffer. Another usage of queues is in the implementation of breadth-first search.

Queue implementation

edit

Theoretically, one characteristic of a queue is that it does not have a specific capacity. Regardless of how many elements are already contained, a new element can always be added. It can also be empty, at which point removing an element will be impossible until a new element has been added again.

Fixed-length arrays are limited in capacity, but it is not true that items need to be copied towards the head of the queue. The simple trick of turning the array into a closed circle and letting the head and tail drift around endlessly in that circle makes it unnecessary to ever move items stored in the array. If n is the size of the array, then computing indices modulo n will turn the array into a circle. This is still the conceptually simplest way to construct a queue in a high-level language, but it does admittedly slow things down a little, because the array indices must be compared to zero and the array size, which is comparable to the time taken to check whether an array index is out of bounds, which some languages do, but this will certainly be the method of choice for a quick and dirty implementation, or for any high-level language that does not have pointer syntax. The array size must be declared ahead of time, but some implementations simply double the declared array size when overflow occurs. Most modern languages with objects or pointers can implement or come with libraries for dynamic lists. Such data structures may have not specified a fixed capacity limit besides memory constraints. Queue overflow results from trying to add an element onto a full queue and queue underflow happens when trying to remove an element from an empty queue.

A bounded queue is a queue limited to a fixed number of items.[1]

There are several efficient implementations of FIFO queues. An efficient implementation is one that can perform the operations—en-queuing and de-queuing—in O(1) time.

  • Linked list
    • A doubly linked list has O(1) insertion and deletion at both ends, so it is a natural choice for queues.
    • A regular singly linked list only has efficient insertion and deletion at one end. However, a small modification—keeping a pointer to the last node in addition to the first one—will enable it to implement an efficient queue.
  • A deque implemented using a modified dynamic array

Queues and programming languages

edit

Queues may be implemented as a separate data type, or maybe considered a special case of a double-ended queue (deque) and not implemented separately. For example, Perl and Ruby allow pushing and popping an array from both ends, so one can use push and shift functions to enqueue and dequeue a list (or, in reverse, one can use unshift and pop),[2] although in some cases these operations are not efficient.

C++'s Standard Template Library provides a "queue" templated class which is restricted to only push/pop operations. Since J2SE5.0, Java's library contains a Queue interface that specifies queue operations; implementing classes include LinkedList and (since J2SE 1.6) ArrayDeque. PHP has an SplQueue class and third-party libraries like beanstalk'd and Gearman.

 

Example

edit

A simple queue implemented in JavaScript:

class Queue {
    constructor() {
        this.items = [];
    }

    enqueue(element) {
        this.items.push(element);
    }

    dequeue() {
        return this.items.shift();
    }
}

Purely functional implementation

edit

Queues can also be implemented as a purely functional data structure.[3] There are two implementations. The first one only achieves   per operation on average. That is, the amortized time is  , but individual operations can take   where n is the number of elements in the queue. The second implementation is called a real-time queue[4] and it allows the queue to be persistent with operations in O(1) worst-case time. It is a more complex implementation and requires lazy lists with memoization.

Amortized queue

edit

This queue's data is stored in two singly-linked lists named   and  . The list   holds the front part of the queue. The list   holds the remaining elements (a.k.a., the rear of the queue) in reverse order. It is easy to insert into the front of the queue by adding a node at the head of  . And, if   is not empty, it is easy to remove from the end of the queue by removing the node at the head of  . When   is empty, the list   is reversed and assigned to   and then the head of   is removed.

The insert ("enqueue") always takes   time. The removal ("dequeue") takes   when the list   is not empty. When   is empty, the reverse takes   where   is the number of elements in  . But, we can say it is   amortized time, because every element in   had to be inserted and we can assign a constant cost for each element in the reverse to when it was inserted.

Real-time queue

edit

The real-time queue achieves   time for all operations, without amortization. This discussion will be technical, so recall that, for   a list,   denotes its length, that NIL represents an empty list and   represents the list whose head is h and whose tail is t.

The data structure used to implement our queues consists of three singly-linked lists   where f is the front of the queue and r is the rear of the queue in reverse order. The invariant of the structure is that s is the rear of f without its   first elements, that is  . The tail of the queue   is then almost   and inserting an element x to   is almost  . It is said almost, because in both of those results,  . An auxiliary function   must then be called for the invariant to be satisfied. Two cases must be considered, depending on whether   is the empty list, in which case  , or not. The formal definition is   and   where   is f followed by r reversed.

Let us call   the function which returns f followed by r reversed. Let us furthermore assume that  , since it is the case when this function is called. More precisely, we define a lazy function   which takes as input three lists such that  , and return the concatenation of f, of r reversed and of a. Then  . The inductive definition of rotate is   and  . Its running time is  , but, since lazy evaluation is used, the computation is delayed until the results are forced by the computation.

The list s in the data structure has two purposes. This list serves as a counter for  , indeed,   if and only if s is the empty list. This counter allows us to ensure that the rear is never longer than the front list. Furthermore, using s, which is a tail of f, forces the computation of a part of the (lazy) list f during each tail and insert operation. Therefore, when  , the list f is totally forced. If it was not the case, the internal representation of f could be some append of append of... of append, and forcing would not be a constant time operation anymore.

See also

edit

References

edit
  1. ^ "Queue (Java Platform SE 7)". Docs.oracle.com. 2025-08-07. Retrieved 2025-08-07.
  2. ^ "Class Array".
  3. ^ Okasaki, Chris. "Purely Functional Data Structures" (PDF).
  4. ^ Hood, Robert; Melville, Robert (November 1981). "Real-time queue operations in pure Lisp". Information Processing Letters. 13 (2): 50–54. doi:10.1016/0020-0190(81)90030-2. hdl:1813/6273.

General references

edit

Further reading

edit
edit
孕妇缺铁吃什么食物好 什么药治牙疼最快 麻药过敏什么症状 四次元是什么意思啊 右手指发麻是什么原因
老人不睡觉是什么预兆 鼻子上长脓包型痘痘是什么原因 robinhood是什么牌子 指甲变紫色是什么原因 濒死感是什么感觉
眼睛酸疼是什么原因 一吃就吐是什么病症 母仪天下是什么意思 辅酶q10是什么 离家出走需要准备什么
fred是什么牌子 黄芪什么人不能喝 今年50岁属什么 佝偻病是什么 风热感冒吃什么
验血能查出什么hcv8jop8ns0r.cn 生源地是什么意思xinmaowt.com 贵州菜属于什么菜系hcv9jop1ns0r.cn 14楼五行属什么hcv8jop7ns2r.cn 台湾什么时候收复hcv7jop4ns6r.cn
小肠与什么相表里youbangsi.com 人参有什么功效hcv9jop5ns4r.cn 狂风暴雨是什么生肖hcv7jop5ns1r.cn 县团委书记是什么级别hcv8jop8ns1r.cn 什么满园hcv9jop4ns2r.cn
麻是什么shenchushe.com 长明灯是什么意思hcv8jop6ns3r.cn 肚脐眼的作用是什么hcv8jop3ns6r.cn 什么是烤瓷牙hcv9jop2ns5r.cn 胡子长的快是什么原因hcv8jop7ns1r.cn
气泡水是什么hcv8jop2ns4r.cn 薄谷开来为什么杀人luyiluode.com 二尾子什么意思hcv9jop1ns5r.cn h什么意思hcv9jop0ns5r.cn 乏力是什么感觉hcv9jop6ns9r.cn
百度