SplStack
SplStack extends SplDoublyLinkedList implements Iterator, Countable, ArrayAccess, Serializable
SPL's doubly-linked-list stack (LIFO) with O(1) push and pop on both ends.
C C23
#include <stdio.h>
#define CAP 8
// C has no built-in stack: array + top index, hand-written idiom
static char *stack[CAP];
static int top = -1;
static void push(char *s) { stack[++top] = s; }
static const char *pop(void) { return stack[top--]; }
static const char *peek(void) { return stack[top]; }
int main(void) {
push("a");
push("b");
push("c");
printf("pop: %s\n", pop());
printf("top: %s\n", peek());
printf("count: %d\n", top + 1);
printf("bottom to top: %s,%s\n", stack[0], stack[1]);
return 0;
}
pop: c top: b count: 2 bottom to top: a,b
C++ C++26
#include <iostream>
#include <stack>
#include <string>
#include <vector>
int main() {
// std::stack container adaptor (deque-backed by default): push/pop/top all O(1)
std::stack<std::string> stack;
stack.push("a");
stack.push("b");
stack.push("c");
std::cout << "pop: " << stack.top() << "\n";
stack.pop();
std::cout << "top: " << stack.top() << "\n";
std::cout << "count: " << stack.size() << "\n";
// std::stack is not iterable: pour into a vector to restore bottom→top
std::vector<std::string> rest;
while (!stack.empty()) {
rest.push_back(stack.top());
stack.pop();
}
std::cout << "bottom to top: ";
for (auto it = rest.rbegin(); it != rest.rend(); ++it) {
std::cout << (it != rest.rbegin() ? "," : "") << *it;
}
std::cout << "\n";
return 0;
}
pop: c top: b count: 2 bottom to top: a,b
C# v11.0.100
using System.Collections.Generic;
using System.Linq;
// Stack<T> generic stack: Push/Pop/Peek all O(1)
var stack = new Stack<string>();
stack.Push("a");
stack.Push("b");
stack.Push("c");
Console.WriteLine("pop: " + stack.Pop());
Console.WriteLine("top: " + stack.Peek());
Console.WriteLine("count: " + stack.Count);
// Enumeration order is top→down: Reverse yields bottom→top
Console.WriteLine("bottom to top: " + string.Join(",", stack.Reverse()));
pop: c top: b count: 2 bottom to top: a,b
Go v1.27.4
package main
import (
"fmt"
"strings"
)
func main() {
// Go has no built-in stack: slice append/tail-truncation idiom (hand-write a thin wrapper when you need semantic APIs)
var stack []string
stack = append(stack, "a", "b", "c")
top := stack[len(stack)-1]
stack = stack[:len(stack)-1]
fmt.Println("pop:", top)
fmt.Println("top:", stack[len(stack)-1])
fmt.Println("count:", len(stack))
fmt.Println("bottom to top:", strings.Join(stack, ","))
}
pop: c top: b count: 2 bottom to top: a,b
Java v27.0.1
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
// ArrayDeque is officially recommended over the legacy Stack class (a locked Vector subclass): push/pop both O(1)
Deque<String> stack = new ArrayDeque<>();
stack.push("a");
stack.push("b");
stack.push("c");
System.out.println("pop: " + stack.pop());
System.out.println("top: " + stack.peek());
System.out.println("count: " + stack.size());
// Iteration is top→down: descendingIterator reverses to bottom→top
Iterator<String> it = stack.descendingIterator();
StringBuilder rest = new StringBuilder();
while (it.hasNext()) {
if (rest.length() > 0) rest.append(",");
rest.append(it.next());
}
System.out.println("bottom to top: " + rest);
}
}
pop: c top: b count: 2 bottom to top: a,b
JavaScript v26.13.1
// JS idiomatically uses arrays as stacks: push/pop are both tail operations
const stack = [];
stack.push("a");
stack.push("b");
stack.push("c");
console.log("pop: " + stack.pop());
console.log("top: " + stack.at(-1));
console.log("count: " + stack.length);
console.log("bottom to top: " + stack.join(","));
pop: c top: b count: 2 bottom to top: a,b
PHP v8.6.0
// SplStack: a doubly-linked-list stack, push/pop O(1) at the same end
$stack = new SplStack();
$stack->push("a");
$stack->push("b");
$stack->push("c");
echo "pop: ", $stack->pop(), "\n";
echo "top: ", $stack->top(), "\n";
echo "count: ", $stack->count(), "\n";
// Iteration is fixed top→down (the LIFO mode is frozen): reverse to get bottom→top
echo "bottom to top: ", implode(",", array_reverse(iterator_to_array($stack)));
pop: c top: b count: 2 bottom to top: a,b
Python v3.15.2
# Python has no dedicated stack class: list append/pop(-1) idiom (tail O(1), built-in methods need no wrapper)
stack = []
stack.append("a")
stack.append("b")
stack.append("c")
print("pop: " + stack.pop())
print("top: " + stack[-1])
print("count: " + str(len(stack)))
print("bottom to top: " + ",".join(stack))
pop: c top: b count: 2 bottom to top: a,b
Rust v1.99.1
fn main() {
// Vec IS the stack idiom: push/pop O(1) at the tail (use VecDeque for double-ended needs)
let mut stack: Vec<&str> = Vec::new();
stack.push("a");
stack.push("b");
stack.push("c");
println!("pop: {}", stack.pop().unwrap());
println!("top: {}", stack.last().unwrap());
println!("count: {}", stack.len());
println!("bottom to top: {}", stack.join(","));
}
pop: c top: b count: 2 bottom to top: a,b
TypeScript v26.13.1
// stack idiom: array push/pop
const stack: string[] = [];
stack.push("a");
stack.push("b");
stack.push("c");
console.log("pop: " + stack.pop());
console.log("top: " + stack.at(-1));
console.log("count: " + stack.length);
console.log("bottom to top: " + stack.join(","));
pop: c top: b count: 2 bottom to top: a,b
Language differences
C: an array plus a top index is the standard hand-rolled stack; dynamic capacity needs realloc or a linked list — exactly the wrapping SplStack provides over raw capacity concerns.
C++: std::stack is a container adapter (over deque by default); with a language-level stack available, SPL's arrival in PHP filled the OO-semantics gap of the array culture.
C#: Stack<T> is a generic stack (circular-buffer implementation); like SplStack it is a collection-class stack, but .NET has had it since 1.0.
Go: no built-in stack — slice append/truncate at the tail is the idiom; write a thin wrapper when you want semantic APIs.
Java: the legacy Stack class extends Vector (synchronized, dated) and the official javadoc itself recommends ArrayDeque.
JavaScript: array push/pop are already stack operations; the language culture never felt a stack class was missing.
PHP: PHP arrays are hash-bucket + doubly-linked-list hybrids: array_push/array_pop at the tail are amortized O(1) and fine, but array_shift/array_unshift at the head are O(n); SplStack is a doubly linked list so both ends are O(1), with clear semantics (top/bottom/isEmpty/count) and serialization support. History: SPL shipped with PHP 5.0 and stabilized in 5.3 amid the everything-as-objects wave — in modern PHP arrays still make efficient stacks, and SplStack's value is mostly explicit intent and OO interfaces (Countable/Iterator/ArrayAccess).
Python: list carries stack semantics natively (append/pop); the docs literally present lists as the stack implementation.
Rust: Vec push/pop is a stack (VecDeque for both ends); the ownership model keeps borrowed stacks explicit.
TypeScript: same as JavaScript — arrays are the stack.