πŸ“š PHP Stack & Queue β€” Topic Index

Stacks (LIFO) and Queues (FIFO) are foundational data structures that power parsing, scheduling, monotonic algorithms, and BFS/DFS traversal.

Topics Covered

#TopicKey Concepts
1BasicsPHP SplStack, SplQueue, array-as-stack
2PatternsMonotonic stack, deque, two-stack tricks
3Interview Questions25 classic problems

When to Use Stack

When to Use Queue

PHP Data Structures

// Stack (LIFO) β€” SplStack
$stack = new SplStack();
$stack->push(1);
$top = $stack->top();  // peek
$val = $stack->pop();

// Queue (FIFO) β€” SplQueue
$queue = new SplQueue();
$queue->enqueue(1);
$front = $queue->bottom(); // peek
$val   = $queue->dequeue();

// Array as stack (idiomatic PHP)
$stack = [];
array_push($stack, 1);   // or $stack[] = 1;
$top = end($stack);      // peek
$val = array_pop($stack);