Queue (data structure)

From Wikipedia, the free encyclopedia

Jump to: navigation, search
Linear data structures

Array
Deque
Linked list
Queue
Stack

A queue (pronounced /kjuː/) is a particular kind of collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position and removal of entities from the front terminal position. This makes the queue 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 whenever an element is added, all elements that were added before have to be removed before the new element can be invoked. A queue is an example of a linear data structure.

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.

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. Common implementations are Circular buffers and Linked lists

Contents

Common operations from the C++ Standard Template Library include:

bool empty() Returns True if the queue is empty, and False otherwise.

T& front() Returns a reference to the value at the front of a non-empty queue. There is also a constant version of this function, const T& front().

void pop() Removes the item at the front of a non-empty queue.

void push(const T& foo) Inserts the argument foo at the back of the queue.

int size() Returns the total number of elements in the queue.

Based on example in Ford and Topp “Data Structures with C++” page 387.

       queue< char > theQueue; // creates a queue of chars named "theQueue"
       theQueue.push('a');     // theQueue now contains "a"
       theQueue.push('b');     // theQueue now contains "a b"
       theQueue.push('c');     // theQueue now contains "a b c"

cout << "theQueue contains: a b c" << endl << endl;

while( !theQueue.empty() ) // while the queue is not empty... { cout << "Size = " << theQueue.size() << endl; // ...output queue size cout << "Value at front = " << theQueue.front() << endl << endl; // ...and output the first element value theQueue.pop(); // ...and remove it }


The defining attribute of a queue data structure is the fact that allows access to only the front and back of the structure. Furthermore, elements can only be removed from the front and can only be added to the back. In this way, an appropriate metaphor often used to represent queues is the idea of a checkout line (Ford/Topp p. 385). Other examples of queues are people traveling up an escalator, machine parts on an assembly line, or cars in line at a gas station. The recurring theme is clear: queues are essentially waiting lines.

In each of the cases, the customer or object at the front of the line was the first one to enter, while at the end of the is the last to have entered. Every time a customer finishes paying for their items (or a person steps off the escalator, or the machine part is removed from the assembly line, etc.) that object leaves the queue from the front. This represents the queue “pop” function. Every time another object or customer enters the line to wait, they join the end of the line and represent the “push” function. The queue “size” function would return the length of the line, and the “empty” function would return true only if there was nothing in the line.

Queues can be very useful in many different situations. As seen in the grocery store metaphor, they are a data structure ordered as first-in-first-out (FIFO) or first-come-first-served (FCFS). This specific type of structure is used for applications that require items to be retrieved in their order of addition to the list (their order of occurrence). Examples of such applications could be an event scheduler (perhaps for a hair salon, or massage parlor), a simple to-do list, or specific operations of the radix sort algorithm (see below under the “See also” header for further information on Radix Sort).

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.

A practical implementation of a queue e.g. with pointers of course does have some capacity limit, that depends on the concrete situation it is used in. For a data structure the executing computer will eventually run out of memory, thus limiting the queue size. 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.

Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.