Queue (abstract data type)

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.

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.

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.

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.

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.

Theoretically, one characteristic of a queue is that it does not have a specific capacity.

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.

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.

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.

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.

A simple queue implemented in JavaScript: Queues can also be implemented as a purely functional data structure.

It is a more complex implementation and requires lazy lists with memoization.

This queue's data is stored in two singly-linked lists named

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

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

This counter allows us to ensure that the rear is never longer than the front list.