How to work with queues and stacks
How to work with a stack
$names = array('Mike', 'Joel', 'Anne');
array_push($names, 'Ray');
$next = array_pop($names);
echo $next;
Ray
How to work with a queue
$names = array('Mike', 'Joel', 'Anne');
array_push($names, 'Ray'); // $names is Mike, Anne, Joel, Ray
$next = array_shift($names); // $names is Anne, Joel, Ray
echo $next; // displays Mike
Mike
- A stack is a special type of array that implements a last-in, first-out (LIFO) collec-tion of values. You can use the array_push() and array_pop() functions to add and remove elements in a stack.
-
- A queue is a special type of array that implements a first-in, first-out (FIFO) collection of values. You can use the array_push() and array_shift() functions to add and remove elements in a queue.
- The functions in this figure modify the array that’s passed to the function. In other words, they don’t return a new array.
Back