Queue  

Representation of a FIFO (first in, first out) queue  
Time complexity in big O notation  

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 over of the sequence and the removal of entities from the other over of the sequence. By convention, the over of the sequence at which elements are added is called the back, tail, or rear of the queue, and the over at which elements are removed is called the head or front of the queue, analogously lớn the words used when people line up lớn wait for goods or services.
The operation of adding an element lớn 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 lớn be dequeued without dequeuing it.
The operations of a queue make it a firstinfirstout (FIFO) data structure. In a FIFO data structure, the first element added lớn the queue will be the first one lớn be removed. This is equivalent lớn the requirement that once a new element is added, all elements that were added before have lớn 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 objectoriented languages as classes. Common implementations are circular buffers and linked lists.
Queues provide services in computer science, transport, and operations research where various entities such as data, objects, persons, or events are stored and held lớn be processed later. In these contexts, the queue performs the function of a buffer. Another usage of queues is in the implementation of breadthfirst tìm kiếm.
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.
Fixedlength arrays are limited in capacity, but it is not true that items need lớn 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 lớn 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 lớn construct a queue in a highlevel language, but it does admittedly slow things down a little, because the array indices must be compared lớn zero and the array size, which is comparable lớn the time taken lớn kiểm tra whether an array index is out of bounds, which some languages bởi, but this will certainly be the method of choice for a quick and dirty implementation, or for any highlevel 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 lớn add an element onto a full queue and queue underflow happens when trying lớn remove an element from an empty queue.
A bounded queue is a queue limited lớn a fixed number of items.^{[1]}
There are several efficient implementations of FIFO queues. An efficient implementation is one that can perform the operations—enqueuing and dequeuing—in O(1) time.
 Linked list
 A doubly linked list has O(1) insertion and deletion at both ends, so sánh it is a natural choice for queues.
 A regular singly linked list only has efficient insertion and deletion at one over. However, a small modification—keeping a pointer lớn the last node in addition lớn the first one—will enable it lớn 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 doubleended queue (deque) and not implemented separately. For example, Perl and Ruby allow pushing and popping an array from both ends, so sánh one can use push and shift functions lớn 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 lớn 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 buổi tiệc ngọt libraries lượt thích beanstalk'd and Gearman.
Xem thêm: youth là gì
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 realtime queue^{[4]} and it allows the queue lớn be persistent with operations in O(1) worstcase time. It is a more complex implementation and requires lazy lists with memoization.
Amortized queue[edit]
This queue's data is stored in two singlylinked 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 lớn insert into the front of the queue by adding a node at the head of . And, if is not empty, it is easy lớn remove from the over of the queue by removing the node at the head of . When is empty, the list is reversed and assigned lớn 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 lớn be inserted and we can assign a constant cost for each element in the reverse lớn when it was inserted.
Realtime queue[edit]
The realtime queue achieves time for all operations, without amortization. This discussion will be technical, so sánh 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 lớn implement our queues consists of three singlylinked lists where f is the front of the queue, 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 lớn is almost . It is said almost, because in both of those results, . An auxiliary function must then be called for the invariant lớn 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 đường dây nóng 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 list 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 is 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 lớn ensure that the rear is never longer kêu ca 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.
Xem thêm: adoption là gì
See also[edit]
 Circular buffer
 Doubleended queue (deque)
 Priority queue
 Queuing theory
 Stack (abstract data type) – the "opposite" of a queue: LIFO (Last In First Out)
References[edit]
General references[edit]
 This article incorporates public tên miền material from Paul E. Black. "Bounded queue". Dictionary of Algorithms and Data Structures. NIST.
Further reading[edit]
 Donald Knuth. The Art of Computer Programming, Volume 1: Fundamental Algorithms, Third Edition. AddisonWesley, 1997. ISBN 0201896834. Section 2.2.1: Stacks, Queues, and Dequeues, pp. 238–243.
 Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction lớn Algorithms, Second Edition. MIT Press and McGrawHill, 2001. ISBN 0262032937. Section 10.1: Stacks and queues, pp. 200–204.
 William Ford, William Topp. Data Structures with C++ and STL, Second Edition. Prentice Hall, 2002. ISBN 0130858501. Chapter 8: Queues and Priority Queues, pp. 386–390.
 Adam Drozdek. Data Structures and Algorithms in C++, Third Edition. Thomson Course Technology, 2005. ISBN 0534491820. Chapter 4: Stacks and Queues, pp. 137–169.
External links[edit]
 STL Quick Reference
 VBScript implementation of stack, queue, deque, and RedBlack Tree
Bình luận