The content of this post was inspired by the post To Queue Or Not To Queue by Vaidehi Joshi and from the Base CS Series Video Stacks and Queues
To Queue Or Not To Queue
Vaidehi Joshi ・ Feb 14 '17 ・ 9 min read
On the post made by Vaidehi she explain how queues work and show some "functions/methods" to enqueue and dequeue something, this is a simple and direct examples to show you how it works in PHP (i'm assuming that you already have seeing the CS video).
PHP comes with a standard library called SplQueue
, (Spl stands for Standard PHP Library)
SplQueue inherits from
SplDoublyLinkedList
. So, objects ofSplQueue
support methodspush()
andpop()
. But please be advised that if you usepush()
andpop()
methods on aSplQueue
object, it behaves like a stack rather than a queue
- Manu Manjunath (original explanation from PHP official documentation)
long short story, SplDoublyLinkedList
means that the same library works both for queue
and stacks
.
Stacks
As mentioned above, if you use push()
and pop()
on a SplQueue
object the behave will be of a stack and not a queue, see:
$stack = new SplQueue();
$stack->push(1);
$stack->push(2);
$stack->push(3);
$stack->pop(); // remove the last one
print_r($stack);
/*
OUTPUT
SplQueue Object
(
[flags:SplDoublyLinkedList:private] => 4
[dllist:SplDoublyLinkedList:private] => Array
(
[0] => 1
[1] => 2
)
)
*/
Note that 3 got popped and not 1.
Queues
Methods to use SplQueue
as a queue are: enqueue()
and dequeue()
, see:
$queue = new SplQueue();
$queue->enqueue('A');
$queue->enqueue('B');
$queue->enqueue('C');
$queue->dequeue(); // remove the first one
print_r($q);
/*
OUTPUT
SplQueue Object
(
[flags:SplDoublyLinkedList:private] => 4
[dllist:SplDoublyLinkedList:private] => Array
(
[0] => B
[1] => C
)
)
*/
Queue
and Stack
helper methods:
$queue->enqueue('dev'); // or $stack->push('dev');
$queue->enqueue('.to'); // or $stack->push('.to');
$queue->count(); // (int) 2
$queue->isEmpty(); // false
$queue->valid(); // false because right now the pointer is in the last position
// move the cursor to the first position, if you run valid() again after this it will be true
$queue->rewind();
$queue->current(); // "dev", current() just work if you rewind() before call it
$queue->unshift(3); // add the value to the beginning
$queue->current(); // "dev", because we need to move the cursor back to the first position
$queue->rewind();
$queue->current(); // 3
$queue->add(2, 'random-text'); // Add/insert a new value at the specified index
print_r($queue); // Show only 3, "dev" and "to" because we don't rewind() yet.
$queue->rewind(); // move the cursor back to the first position
print_r($queue);
/*
OUTPUT
SplQueue Object
(
[flags:SplDoublyLinkedList:private] => 4
[dllist:SplDoublyLinkedList:private] => Array
(
[0] => 3
[1] => dev
[2] => random-text
[3] => .to
)
)
*/
Iterating over the queue and stack:
$queue = new SplQueue();
$queue->enqueue('dev');
$queue->enqueue('.to');
$queue->unshift('https://');
$queue->rewind(); // always rewind the queue/stack, so PHP can start from the beginning.
while($queue->valid()){
echo $queue->current()."\n"; // Show the first one
$queue->next(); // move the cursor to the next element
}
/*
OUTPUT
https://
dev
.to
*/
There is a bunch of methods that you can use and i cannot show all of it here, you can see every method in the official documentation about SplQueue
So, what programming language are you working right now? do this have queue and stack built-in? if so, how can i use it? if not how do you do it? write a post and show to us how to work with queues and stacks in your favorite programming language! 😄
--
Thanks for reading out, I'm working on my English and if there is any misspelling please let me know.
Top comments (1)
This is wonderful! I always love to see implementations of these structures in different languages. It's especially interesting to me because I don't know any PHP! But I can still follow along :)
Thank you for taking the time to write this up, and for the shout out to the BaseCS Video Series! I hope you'll write more implementation posts in the future.