SplQueue

SplQueue extends SplDoublyLinkedList implements Iterator, Countable, ArrayAccess, Serializable

SPL's doubly-linked-list queue (FIFO) with O(1) enqueue and dequeue on both ends.

C C23

C23
#include <stdio.h>

#define CAP 8

// C has no built-in queue: array + head/tail indices hand-written as a ring buffer idiom
static char *queue[CAP];
static int head = 0, tail = 0;

static void enqueue(char *s) { queue[tail++ % CAP] = s; }
static const char *dequeue(void) { return queue[head++ % CAP]; }

int main(void) {
    enqueue("a");
    enqueue("b");
    enqueue("c");
    printf("dequeue: %s\n", dequeue());
    printf("front: %s\n", queue[head]);
    printf("count: %d\n", tail - head);
    printf("remaining: %s,%s\n", queue[head], queue[head + 1]);
    return 0;
}
Output
dequeue: a
front: b
count: 2
remaining: b,c

C++ C++26

C++26
#include <iostream>
#include <queue>
#include <string>
#include <vector>

int main() {
    // std::queue container adaptor (deque-backed by default): push/pop/front all O(1)
    std::queue<std::string> queue;
    queue.push("a");
    queue.push("b");
    queue.push("c");
    std::cout << "dequeue: " << queue.front() << "\n";
    queue.pop();
    std::cout << "front: " << queue.front() << "\n";
    std::cout << "count: " << queue.size() << "\n";
    // std::queue is not iterable: drain and restore to show front→back
    std::vector<std::string> rest;
    while (!queue.empty()) {
        rest.push_back(queue.front());
        queue.pop();
    }
    std::cout << "remaining: ";
    for (size_t i = 0; i < rest.size(); ++i) {
        std::cout << (i ? "," : "") << rest[i];
    }
    std::cout << "\n";
    return 0;
}
Output
dequeue: a
front: b
count: 2
remaining: b,c

C# v11.0.100

v11.0.100
using System.Collections.Generic;

// Queue<T> generic queue (ring buffer): Enqueue/Dequeue both O(1)
var queue = new Queue<string>();
queue.Enqueue("a");
queue.Enqueue("b");
queue.Enqueue("c");
Console.WriteLine("dequeue: " + queue.Dequeue());
Console.WriteLine("front: " + queue.Peek());
Console.WriteLine("count: " + queue.Count);
// Enumeration runs front→back
Console.WriteLine("remaining: " + string.Join(",", queue));
Output
dequeue: a
front: b
count: 2
remaining: b,c

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"strings"
)

func main() {
	// Go has no built-in queue: slice append + head re-slicing idiom (re-slicing is O(1), the backing array is shared)
	var queue []string
	queue = append(queue, "a", "b", "c")
	front := queue[0]
	queue = queue[1:]
	fmt.Println("dequeue:", front)
	fmt.Println("front:", queue[0])
	fmt.Println("count:", len(queue))
	fmt.Println("remaining:", strings.Join(queue, ","))
}
Output
dequeue: a
front: b
count: 2
remaining: b,c

Java v27.0.1

v27.0.1
import java.util.ArrayDeque;
import java.util.Deque;

public class Main {
    public static void main(String[] args) {
        // ArrayDeque is the officially recommended queue: offer/poll/peek all O(1)
        Deque<String> queue = new ArrayDeque<>();
        queue.offer("a");
        queue.offer("b");
        queue.offer("c");
        System.out.println("dequeue: " + queue.poll());
        System.out.println("front: " + queue.peek());
        System.out.println("count: " + queue.size());
        // Iteration runs front→back
        System.out.println("remaining: " + String.join(",", queue));
    }
}
Output
dequeue: a
front: b
count: 2
remaining: b,c

JavaScript v26.13.1

v26.13.1
// JS array push + shift makes a queue; shift is O(n) — high-throughput cases need a cursor or linked structure
const queue = [];
queue.push("a");
queue.push("b");
queue.push("c");
console.log("dequeue: " + queue.shift());
console.log("front: " + queue[0]);
console.log("count: " + queue.length);
console.log("remaining: " + queue.join(","));
Output
dequeue: a
front: b
count: 2
remaining: b,c

PHP v8.6.0

v8.6.0
// SplQueue: a doubly-linked-list queue, enqueue/dequeue both O(1)
$queue = new SplQueue();
$queue->enqueue("a");
$queue->enqueue("b");
$queue->enqueue("c");
echo "dequeue: ", $queue->dequeue(), "\n";
echo "front: ", $queue->bottom(), "\n";
echo "count: ", $queue->count(), "\n";
// Iteration defaults to front→back (FIFO)
echo "remaining: ", implode(",", iterator_to_array($queue));
Output
dequeue: a
front: b
count: 2
remaining: b,c

Python v3.15.2

v3.15.2
from collections import deque

# collections.deque: the double-ended queue with O(1) popleft — list.pop(0) is the O(n) counterexample
queue = deque(["a", "b", "c"])
print("dequeue: " + queue.popleft())
print("front: " + queue[0])
print("count: " + str(len(queue)))
print("remaining: " + ",".join(queue))
Output
dequeue: a
front: b
count: 2
remaining: b,c

Rust v1.99.1

v1.99.1
use std::collections::VecDeque;

fn main() {
    // VecDeque: push_back/pop_front both O(1), the official first choice for queues (Vec head ops are O(n))
    let mut queue: VecDeque<&str> = VecDeque::new();
    queue.push_back("a");
    queue.push_back("b");
    queue.push_back("c");
    println!("dequeue: {}", queue.pop_front().unwrap());
    println!("front: {}", queue.front().unwrap());
    println!("count: {}", queue.len());
    // Iteration runs front→back
    let rest: Vec<&str> = queue.iter().copied().collect();
    println!("remaining: {}", rest.join(","));
}
Output
dequeue: a
front: b
count: 2
remaining: b,c

TypeScript v26.13.1

v26.13.1
// queue idiom: array push/shift
const queue: string[] = [];
queue.push("a");
queue.push("b");
queue.push("c");
console.log("dequeue: " + queue.shift());
console.log("front: " + queue[0]);
console.log("count: " + queue.length);
console.log("remaining: " + queue.join(","));
Output
dequeue: a
front: b
count: 2
remaining: b,c

Language differences

C: a hand-rolled ring buffer (modulo head/tail) is the standard; capacity must be sized ahead, or use a linked list.

C++: std::queue adapter (over deque); std::priority_queue for priority semantics.

C#: Queue<T> is a circular array, sibling of Stack<T>; ConcurrentQueue<T> for concurrency.

Go: re-slicing (queue = queue[1:]) is amortized O(1) but keeps the backing array alive — watch memory for long-lived queues; the standard library has no general queue container.

Java: ArrayDeque is the official pick (LinkedList works but pays per-node cost); the Queue interface splits throwing methods (add/remove) from value-returning ones (offer/poll).

JavaScript: array push + shift make a queue, but shift is O(n) (whole-array shift); high-throughput code uses a head-index cursor or linked lists.

PHP: array_shift on the head is O(n) (indexes renumbered) — slow at scale; SplQueue is a doubly linked list with O(1) on both ends, semantic APIs (enqueue/dequeue/bottom) and FIFO iteration; together with SplStack it is the two directions of SPL's linked list.

Python: list.pop(0) is the O(n) anti-pattern — the docs explicitly recommend collections.deque (O(1) both ends); queue.Queue adds thread safety.

Rust: VecDeque is a ring buffer with O(1) ends; Vec::remove(0) is the O(n) anti-pattern; concurrency via std::sync::mpsc/crossbeam.

TypeScript: same as JavaScript; ecosystem queue libraries own the semantic wrapping.