Lably
Skip to content

Queue (abstract data type)

Abstract data type

  • 7 min read
  • 2 chapters

Related on Lably

Through Computer science, Circular buffer and Linked list

  • Enqueue, which adds one element to the rear of the queue
  • Dequeue, which removes one element from the front of the queue.

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 as the first element added to the queue is the first one 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 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

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.

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

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()), although in some cases these operations are not efficient.

C++'s Standard Template Library provides a std::queue<T> 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

A simple queue implemented in TypeScript:

<span id="mwXw"></span><span class="kd" id="mwYA">class</span><span class="w" id="mwYQ"> </span><span class="nx" id="mwYg">Queue</span><span class="o" id="mwYw"><</span><span class="nx" id="mwZA">T</span><span class="o" id="mwZQ">></span><span class="w" id="mwZg"> </span><span class="p" id="mwZw">{</span> <span class="w" id="mwaA"> </span><span class="k" id="mwaQ">private</span><span class="w" id="mwag"> </span><span class="nx" id="mwaw">items</span><span class="o" id="mwbA">:</span><span class="w" id="mwbQ"> </span><span class="kt" id="mwbg">T</span><span class="p" id="mwbw">[]</span><span class="w" id="mwcA"> </span><span class="o" id="mwcQ">=</span><span class="w" id="mwcg"> </span><span class="p" id="mwcw">[];</span> <span class="w" id="mwdA"> </span><span class="nx" id="mwdQ">enqueue</span><span class="p" id="mwdg">(</span><span class="nx" id="mwdw">element</span><span class="o" id="mweA">:</span><span class="w" id="mweQ"> </span><span class="kt" id="mweg">T</span><span class="p" id="mwew">)</span><span class="o" id="mwfA">:</span><span class="w" id="mwfQ"> </span><span class="ow" id="mwfg">void</span><span class="w" id="mwfw"> </span><span class="p" id="mwgA">{</span> <span class="w" id="mwgQ"> </span><span class="k" id="mwgg">this</span><span class="p" id="mwgw">.</span><span class="nx" id="mwhA">items</span><span class="p" id="mwhQ">.</span><span class="nx" id="mwhg">push</span><span class="p" id="mwhw">(</span><span class="nx" id="mwiA">element</span><span class="p" id="mwiQ">);</span> <span class="w" id="mwig"> </span><span class="p" id="mwiw">}</span> <span class="w" id="mwjA"> </span><span class="nx" id="mwjQ">dequeue</span><span class="p" id="mwjg">()</span><span class="o" id="mwjw">:</span><span class="w" id="mwkA"> </span><span class="nx" id="mwkQ">T</span><span class="w" id="mwkg"> </span><span class="o" id="mwkw">|</span><span class="w" id="mwlA"> </span><span class="kc" id="mwlQ">undefined</span><span class="w" id="mwlg"> </span><span class="p" id="mwlw">{</span> <span class="w" id="mwmA"> </span><span class="k" id="mwmQ">return</span><span class="w" id="mwmg"> </span><span class="k" id="mwmw">this</span><span class="p" id="mwnA">.</span><span class="nx" id="mwnQ">items</span><span class="p" id="mwng">.</span><span class="nx" id="mwnw">shift</span><span class="p" id="mwoA">();</span> <span class="w" id="mwoQ"> </span><span class="p" id="mwog">}</span> <span class="w" id="mwow"> </span><span class="nx" id="mwpA">isEmpty</span><span class="p" id="mwpQ">()</span><span class="o" id="mwpg">:</span><span class="w" id="mwpw"> </span><span class="kt" id="mwqA">boolean</span><span class="w" id="mwqQ"> </span><span class="p" id="mwqg">{</span> <span class="w" id="mwqw"> </span><span class="k" id="mwrA">return</span><span class="w" id="mwrQ"> </span><span class="k" id="mwrg">this</span><span class="p" id="mwrw">.</span><span class="nx" id="mwsA">items</span><span class="p" id="mwsQ">.</span><span class="nx" id="mwsg">length</span><span class="w" id="mwsw"> </span><span class="o" id="mwtA">===</span><span class="w" id="mwtQ"> </span><span class="mf" id="mwtg">0</span><span class="p" id="mwtw">;</span> <span class="w" id="mwuA"> </span><span class="p" id="mwuQ">}</span> <span class="p" id="mwug">}</span>

Sign in to keep reading

One more chapter is free with a Lably account.

More in Theoretical computer science