{
  "id": "queue-data-type",
  "title": "Queue (Data Type)",
  "lang": "en",
  "source_lang": "en",
  "summary": "A queue is a first-in, first-out abstract data type used to organize ordered processing, buffering, communication, and algorithmic traversal.",
  "category": "computer-science",
  "subcategory": "algorithms-theory",
  "type": "concept",
  "aliases": [
    "FIFO queue",
    "first-in, first-out queue"
  ],
  "generated_at": "2026-11-03",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/queue-data-type/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/queue-data-type/",
      "json": "https://aiwiki.page/zh/queue-data-type.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "A queue is an abstract data type that stores elements in an order and normally removes them in the order in which they were inserted. This discipline is called first in, first out, or FIFO. Insertion takes place at the rear, also called the tail, and removal takes place at the front, also called the head. If the elements \\(a\\), \\(b\\), and \\(c\\) are inserted in that order, three successful removals return \\(a\\), then \\(b\\), then \\(c\\). This distinguishes a queue from a stack, which removes the most recently inserted element first.\n\nQueues separate the production of work from its consumption. An algorithm can record items that must be examined later; an operating system can hold pending work; and a communication system can buffer data until a receiver is ready. The queue abstraction specifies observable behavior, not a particular memory layout. Arrays, linked nodes, circular buffers, and combinations of other data structures can all implement the same sequential FIFO contract.\n\nThe apparent simplicity of a queue conceals several distinct design questions. An implementation must determine how storage grows, what happens when the queue is empty or full, and how operations behave when multiple participants access it simultaneously. A queue used inside a single-threaded algorithm has different requirements from a bounded hardware buffer or a durable messaging service. In particular, FIFO removal order alone does not guarantee fairness among waiting workers, completion in submission order, or reliable delivery across failures."
    },
    {
      "heading": "Abstract model and operation semantics",
      "text": "A queue can be modeled as a finite sequence \\(Q=\\langle x_0,x_1,\\ldots,x_{n-1}\\rangle\\), where \\(x_0\\) is the front element and \\(n\\) is the current size. The operation enqueue(x) appends \\(x\\) to the sequence. When \\(n>0\\), dequeue() returns \\(x_0\\) and changes the sequence to \\(\\langle x_1,\\ldots,x_{n-1}\\rangle\\). These definitions establish the ordering rule independently of implementation. Elements need not be distinct: inserting the same value twice creates two logical queue entries.\n\nA typical application programming interface also provides front() or peek(), which examines the front without removing it; is_empty(); and sometimes size(). A bounded queue additionally exposes a capacity, either directly or through operations that report whether an insertion succeeded. These supplementary operations are not all equally easy to implement. For example, a sequential queue can maintain an exact count cheaply, whereas an exact size query on some concurrent queues requires additional coordination or traversal.\n\nRemoval from an empty queue has no successful result under the basic sequence model. Libraries therefore extend the contract in different ways: an operation may raise an exception, return an optional value, return a success flag alongside an output parameter, or wait until an element becomes available. Returning a special value is safe only if that value cannot be confused with a legitimate element. If null references are permitted as stored elements, for example, a bare null result cannot unambiguously distinguish an empty queue from a queued null.\n\nA bounded queue requires a corresponding policy for insertion when full. It may reject the new element, block the producer, evict an existing element, or overwrite the oldest entry. The last two policies make the structure a lossy buffer rather than an ordinary queue that preserves every successfully accepted entry until removal. A precise contract must distinguish an attempted insertion from an accepted insertion and state whether acceptance can later be revoked by eviction.\n\nFIFO is a property of the ordering of accepted entries and removals, not a universal ordering of all activity around the queue. In a sequential execution, insertion order is unambiguous. With concurrent producers, overlapping insertions may be ordered in either direction unless the interface specifies otherwise. Moreover, a worker that removes the first item may finish after another worker that removes the second. Queue order therefore governs access to work, while processing order and completion order require separate definitions."
    },
    {
      "heading": "Array-based queues and circular buffers",
      "text": "An array provides compact storage and direct indexing, but using it as a queue requires more than repeatedly deleting the first array element. In a straightforward packed array, removing the front shifts all remaining elements one position toward the beginning. A removal from a queue of \\(n\\) elements then costs \\(O(n)\\) time. Repeating this operation until the array is empty can take quadratic total time, even though the queue abstraction itself does not require any shifting.\n\nA circular buffer, or ring buffer, avoids this movement by interpreting an array cyclically. In one common design, an array has capacity \\(C>0\\), an index \\(h\\) identifies the front, and a count \\(n\\) records occupancy. The next insertion position is\n\n\\[t=(h+n)\\bmod C.\\]\n\nInsertion writes at \\(t\\) and increments \\(n\\). Removal reads at \\(h\\), advances \\(h\\) to \\((h+1)\\bmod C\\), and decrements \\(n\\). Existing elements remain in place. For example, with capacity eight, a queue can occupy positions \\(6,7,0,1\\) while still presenting those four entries as a single logical sequence.\n\nImplementations must distinguish full from empty. If only front and rear indices are stored and both are interpreted modulo \\(C\\), equality may indicate either condition. One solution maintains an explicit count. Another leaves one slot unused, so an array of length \\(C\\) stores at most \\(C-1\\) elements. A third uses logical counters that advance beyond the array bounds and derives physical indices from them. In fixed-width machine arithmetic, such counters require an overflow-aware design rather than an assumption that they increase forever.\n\nA fixed-capacity ring buffer supports constant-time insertion and removal without allocating storage during normal operation. A growable array queue instead allocates a larger array when full and copies the logical sequence into it, commonly resetting the front index to zero. If capacity increases geometrically, such as by doubling, insertion has constant amortized cost despite occasional linear-time copies. Shrinking requires care: shrinking immediately after a small occupancy change can cause repeated growth and contraction, so implementations commonly use different expansion and contraction thresholds.\n\nContiguous storage often improves interaction with the CPU cache, reduces allocation overhead, and avoids a pointer per element. These advantages do not make every ring operation equally cheap. Resizing pauses, element construction or destruction, and copying large values can dominate the bookkeeping. When entries hold references to objects, a removed slot may also need to be cleared so that garbage collection can reclaim an object no longer logically present in the queue. Thus, constant-time index manipulation is only one part of the actual cost."
    },
    {
      "heading": "Linked representations and storage management",
      "text": "A queue can also be built from a linked list. In the simplest singly linked representation, each node contains an element and a reference to the next node. The queue maintains references to both the front node and the last node. Enqueue creates a node, attaches it after the last node, and updates the rear reference. Dequeue advances the front reference to the next node. With both endpoints available, neither operation requires traversal of the existing sequence.\n\nThe empty and singleton states need explicit treatment. When the first element is inserted, both endpoints must identify the new node. When the last element is removed, both endpoints must return to the empty representation. Failing to update the rear reference after the last removal can leave a stale pointer that later corrupts an insertion. A common alternative uses a sentinel, or dummy, node. The sentinel separates structural bookkeeping from the first stored element and can reduce special cases, although the exact invariant depends on the implementation.\n\nA linked queue grows one node at a time rather than reserving a contiguous block for a future maximum size. This can be useful when the number of pending entries is unpredictable or contiguous memory is scarce. It also avoids moving existing elements during growth. However, every node carries link information, allocation may require synchronization inside a memory allocator, and nodes can be scattered across memory. A theoretically constant-time dequeue may therefore incur a cache miss for each node it visits.\n\nThe statement that linked enqueue and dequeue take \\(O(1)\\) time usually describes pointer updates under an assumed memory-allocation model. It does not guarantee a bounded elapsed time for allocating a new node or returning memory to an allocator. This distinction matters in a real-time operating system, where the worst-case duration of an operation can be more important than its average cost. Preallocated node pools can remove general-purpose allocation from the critical path, but they also impose a finite resource limit and require a policy for exhaustion.\n\nBetween linked nodes and a single resizable array lies a family of segmented or block-based designs. Each block stores several elements, while links or an auxiliary index connect blocks. Such structures amortize link and allocation overhead across many entries and avoid copying the entire queue when additional storage is needed. They also introduce boundary cases when an endpoint crosses a block boundary. Some general-purpose double-ended containers use related designs, although the abstract queue contract does not require access to both ends for both insertion and removal."
    },
    {
      "heading": "Complexity, amortization, and functional queues",
      "text": "The usual performance target for a queue is \\(O(1)\\) insertion and removal, with \\(O(n)\\) storage for \\(n\\) elements. These bounds belong to different analytical categories. A fixed-capacity circular buffer can provide worst-case constant-time structural updates. A geometrically growing array queue generally provides amortized constant-time insertion. A linked queue provides constant-time endpoint manipulation but may depend on allocation costs. Time complexity therefore needs to be stated together with the computational model and the treatment of memory management.\n\nA queue can be implemented using two stacks, often called in and out. Enqueue pushes an element onto in. Dequeue pops from out; if out is empty, it first transfers every element from in to out. This transfer reverses their order, placing the oldest outstanding element at the top of out. New arrivals remain in in until the next transfer. The representation illustrates that a FIFO abstraction need not be built from a data structure that is intrinsically FIFO.\n\nA single dequeue in this design can take \\(O(n)\\) time, but a sequence of operations takes only linear total time in the number of operations. Each entry is pushed onto in once, transferred at most once, and removed from out once. The same result follows from amortized analysis using a potential proportional to the number of entries in in. If a transfer of \\(k\\) entries is charged roughly \\(2k\\) primitive stack operations, the decrease in stored potential pays for that transfer. The bound describes the whole operation sequence, not a promise that every individual call is fast.\n\nThe two-part representation is especially important in functional programming. An immutable queue can use a front list in removal order and a rear list in reverse insertion order. Adding a rear element or removing a front element can create a new queue version while sharing most of the existing structure. When the front becomes empty, the rear is reversed to form a new front. This supports persistence: earlier queue versions remain usable rather than being overwritten.\n\nPersistence complicates amortized reasoning because a program may branch repeatedly from an earlier queue version. If every branch independently performs the same expensive reversal, an argument based on one linear history may no longer apply. Functional queue designs can address this with carefully shared deferred computation, memoization, or incremental scheduling. Chris Okasaki’s work in the 1990s established influential techniques for purely functional queues, including real-time designs that distribute rebuilding work across operations. Their constant-time claims depend on the evaluation and cost model, particularly how deferred computations are shared and evaluated."
    },
    {
      "heading": "Queues in graph algorithms and structured processing",
      "text": "One of the clearest uses of a queue is breadth-first search in a graph. The algorithm places a starting vertex in a queue, repeatedly removes a vertex, and inserts its previously undiscovered neighbors. When each vertex is marked as discovered before being enqueued, no vertex needs to enter the queue more than once. For an adjacency-list graph with \\(V\\) vertices and \\(E\\) edges, the traversal takes \\(O(V+E)\\) time and uses \\(O(V)\\) auxiliary storage in the worst case.\n\nFIFO order is essential to the distance property of breadth-first search. Every vertex at distance \\(d\\) from the source is processed before vertices at distance \\(d+1\\). Newly discovered neighbors of a distance-\\(d\\) vertex are appended behind the vertices already awaiting processing. Consequently, when all edges have equal cost, the first discovery of a vertex establishes a path with the minimum number of edges. A queue is therefore part of the proof of correctness, not merely a convenient implementation choice.\n\nThis property does not extend unchanged to arbitrary weighted graphs. Dijkstra’s algorithm selects an unsettled vertex with minimum tentative distance, usually through a priority-based structure rather than an ordinary FIFO queue. Substituting a FIFO queue removes the minimum-distance selection rule and invalidates the usual Dijkstra correctness argument. Conversely, depth-first search uses a stack-like discipline, whether explicitly or through recursive calls, and explores a different traversal order. The choice of worklist discipline can change both an algorithm’s behavior and its proof.\n\nQueues also appear in topological sorting. In the algorithm associated with Arthur B. Kahn’s 1962 work, vertices with zero remaining incoming edges are eligible for removal. A FIFO queue can hold these eligible vertices. Removing one vertex decreases the incoming-edge counts of its successors, and any successor whose count becomes zero joins the queue. With adjacency lists, this takes \\(O(V+E)\\) time. The queue does not create a unique ordering: multiple valid topological orders may exist, and the order in which eligible vertices are inserted influences the result.\n\nOther applications use queues without requiring a strict layered traversal. A compiler can keep a worklist of program points whose data-flow information may need updating. A web crawler can store discovered locations awaiting retrieval, although practical scheduling commonly adds priorities and per-host constraints. An event loop often has queues for pending callbacks or tasks, but its complete scheduling rules may involve several queues and distinct processing phases. In each case, the queue records deferred work; the surrounding algorithm determines when work is added and what its removal signifies."
    },
    {
      "heading": "Concurrent queues and synchronization",
      "text": "A concurrent queue allows multiple execution contexts to insert or remove elements while operations overlap. Designs are often classified by their producer and consumer counts: single-producer/single-consumer, multiple-producer/single-consumer, or multiple-producer/multiple-consumer. This distinction is substantive. In a single-producer/single-consumer ring buffer, each endpoint can have one writer, greatly simplifying coordination. With multiple producers, several participants may compete to claim the same insertion position, requiring an additional protocol.\n\nA straightforward implementation protects a sequential queue with a mutex and uses condition variables to wait for “not empty” and, for a bounded queue, “not full.” A waiting thread releases the mutex as part of the wait and reacquires it before testing the condition again. The condition must normally be checked in a loop because wakeups do not reserve an item for a particular waiter and may occur without the desired condition being true. Synchronization is thus responsible both for protecting structural invariants and for coordinating waiting.\n\nMore specialized implementations use atomic operations and a memory model that defines visibility and ordering between threads. A producer must not merely reserve a slot; it must publish a fully initialized entry before a consumer treats that entry as available. Release and acquire operations can establish this relationship when used in an appropriate protocol. Plain shared variables, or language-specific modifiers that do not provide the required atomicity and synchronization, are not substitutes. Correctness depends on both the algorithm and the rules of the implementation language.\n\nAn important correctness condition is linearizability. Each completed operation must appear to take effect at one instant between its invocation and its response, and the resulting sequential history must satisfy the FIFO specification. If one insertion completes before another begins, that order must be respected. Overlapping insertions may be placed in either order. Linearizability does not require simultaneous consumers to return from their calls in item order: a consumer that has logically removed an earlier element can be delayed before returning.\n\nNonblocking progress guarantees are separate from ordering guarantees. A lock-free queue guarantees system-wide progress under its stated execution assumptions, not that every thread completes promptly. A wait-free queue provides an individual-operation progress bound in the relevant model. Maged Michael and Michael Scott’s 1996 queue algorithm is an influential lock-free linked design using atomic compare-and-swap operations. Such designs also face safe memory-reclamation problems: a removed node cannot be reused while another thread might still access it. Garbage collection, hazard pointers, epoch-based schemes, and other methods address this issue with different costs and assumptions. Lock-free structure alone therefore does not eliminate memory-management coordination."
    },
    {
      "heading": "Buffering, capacity, and overload",
      "text": "In systems software, a queue often absorbs a temporary mismatch between arrival rate and service rate. A producer may create work in bursts while a consumer processes it at a steadier pace. Queue capacity buys time for the consumer to catch up; it does not increase the consumer’s sustained processing capacity. If work arrives persistently faster than it can be completed, an unbounded queue grows until some other resource limit is reached.\n\nQueueing theory supplies a mathematical framework for these relationships. Little’s law, published by John D. C. Little in 1961, states that under its applicability conditions,\n\n\\[L=\\lambda W,\\]\n\nwhere \\(L\\) is the long-run average number of items in a defined system, \\(\\lambda\\) is its long-run arrival rate, and \\(W\\) is the average time an item spends there. The boundary of the system matters: counting only waiting entries requires waiting time, while counting entries both waiting and being processed requires total time in the system. An arrival rate of \\(2{,}000\\) items per second and an average residence time of \\(0.05\\) seconds imply an average population of \\(100\\) items within that boundary.\n\nBounded queues make overload explicit. When capacity is exhausted, a producer can wait, fail, shed work, or invoke another policy. Backpressure propagates this inability to accept more work toward the source, for example by blocking a submission or withholding permission to send additional data. It is not equivalent to merely allocating a larger buffer. A finite buffer without a feedback or rejection mechanism can postpone overload while increasing the delay experienced by accepted work.\n\nThe unit of capacity also matters. A limit of 1,000 entries bounds memory poorly if entries range from a few bytes to several megabytes. Systems may instead limit total bytes, estimated processing cost, or multiple resource measures. A queue containing references may retain large object graphs even when its own array or node storage is small. Monitoring only the structural size can consequently underestimate the resources tied up in pending work.\n\nStrict FIFO can create head-of-line blocking when the front item prevents later eligible work from progressing. The effect depends on the service model: a shared arrival queue feeding available workers differs from a single server that must finish each item before starting another. Separate queues, selective scheduling, or partitioning can reduce interference, but they alter ordering guarantees or introduce coordination costs. Queue length likewise does not fully describe latency: service-time variation, worker availability, batching, and stalls can produce very different delays at the same occupancy."
    },
    {
      "heading": "Related queue types and distributed messaging",
      "text": "Several structures carry the name “queue” while exposing different selection rules. A double-ended queue, usually shortened to deque, permits insertion and removal at both ends. Restricting its operations yields either a FIFO queue or a stack. A priority queue removes an element according to a priority relation rather than insertion time; equal-priority entries are FIFO only if the implementation explicitly preserves that order. A heap is a common priority-queue implementation, but a priority queue is not an ordinary FIFO queue with a faster representation.\n\nA blocking queue extends queue operations with waiting semantics. A producer-consumer channel may go further by defining closure, cancellation, and ownership transfer. A zero-capacity channel can require a sender and receiver to rendezvous, transferring an item without retaining a backlog. Such an interface can support ordered communication while differing substantially from a finite sequence stored between independent operations. A delay queue likewise selects only entries whose eligibility time has arrived, usually combining time-based selection with another ordering rule.\n\nDistributed message queues, commonly implemented by a message broker, add network communication, persistence, acknowledgments, and recovery. Removing an entry from an in-memory FIFO queue is normally a local state change. Receiving a distributed message may instead create a delivery attempt that remains pending until acknowledged. If a consumer fails after processing but before acknowledging, the broker may deliver the message again. If it acknowledges before completing the work, a subsequent failure may leave the work unfinished.\n\nDelivery guarantees therefore concern more than ordering. At-most-once delivery permits loss while avoiding repeated delivery attempts under the service’s defined model. At-least-once delivery allows retries and duplicates. Claims of exactly-once behavior must identify the scope of the guarantee: broker state, a transactional stream-processing operation, or an external business effect are different boundaries. Coordinating message acknowledgment with a database transaction can address some failure cases, but writing to an unrelated external system introduces another consistency problem unless an appropriate protocol connects the effects.\n\nDistributed ordering is similarly scoped. A service may preserve order within one partition, one producer stream, or one consumer session rather than across all messages. Redelivery and parallel consumers can further separate initial delivery order from processing and completion order. Some broker interfaces also support priorities, delayed availability, or expiration, each of which changes the relationship to a simple FIFO sequence. The abstract queue remains a useful foundation, but reliability and global order cannot be inferred from the word “queue” alone."
    },
    {
      "heading": "Library interfaces, invariants, and verification",
      "text": "Programming libraries expose queues through different combinations of abstract interfaces and concrete containers. In Java, Queue defines paired methods such as add and offer, and remove and poll, with different failure-reporting conventions; the interface also permits implementations whose selection rule is not FIFO, such as PriorityQueue. ArrayDeque can be used for ordinary FIFO behavior. In C++, std::queue is a container adapter that presents a restricted interface over an underlying container, with std::deque as the default. These designs separate an operation vocabulary from a particular storage representation.\n\nPython makes another useful distinction. collections.deque supports efficient operations at both ends and can implement a local FIFO queue, whereas queue.Queue provides coordinated producer-consumer operations, optional bounded capacity, and task accounting. A Python list’s pop(0) instead shifts subsequent entries and has linear cost. In JavaScript, push and shift can express FIFO behavior, but an algorithm should not assume that front removal has constant cost across implementations. A ring representation or specialized queue avoids relying on such an unstated performance guarantee.\n\nVerification begins with representation invariants. For a count-based circular buffer, typical conditions include \\(0\\leq n\\leq C\\), a front index within array bounds, and a mapping from logical position \\(i\\) to physical position \\((h+i)\\bmod C\\). For a linked queue, the endpoint references must agree with emptiness, the rear must identify the final reachable node, and its next link must be empty. A loop invariant can establish that a resizing copy preserves the sequence even when the old array wraps around.\n\nA useful testing method compares an implementation against a simple sequence model over generated operation histories. Tests cover empty removals, repeated values, transitions between empty and singleton states, wraparound, capacity exhaustion, and resizing while wrapped. Identity-sensitive entries can reveal accidental duplication or loss that value-only tests might miss. For bounded overwrite buffers, the model must include eviction semantics rather than incorrectly treating every insertion as lossless.\n\nConcurrent verification requires histories rather than only final contents. A test can record operation invocations, responses, and returned values, then check whether some legal sequential FIFO history respects the real-time ordering constraints. Formal verification can instead relate implementation states to an abstract queue and prove that state transitions preserve the required behavior. These proofs may include atomic operations, memory visibility, reclamation, and progress assumptions. A queue that passes sequential ordering tests can still be incorrect under overlapping operations, and one that is linearizable can still violate a separately required capacity, cancellation, or progress contract."
    }
  ],
  "links": [
    "abstract-data-type",
    "stack-data-type",
    "algorithm",
    "operating-system",
    "application-programming-interface",
    "array-data-structure",
    "cpu-cache",
    "garbage-collection",
    "linked-list",
    "real-time-operating-system",
    "time-complexity",
    "amortized-analysis",
    "functional-programming",
    "breadth-first-search",
    "dijkstra-algorithm",
    "depth-first-search",
    "compiler",
    "event-loop",
    "synchronization-computing",
    "memory-model",
    "priority-queue",
    "message-broker",
    "database-transaction",
    "javascript",
    "loop-invariant",
    "formal-verification"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}