Working with Queues and Stacks in PHP
Tags: php, queues, stacks
Published: 2018-03-13 14:26:33


---
title: Working with Queues and Stacks in PHP
published: true
description: Inspired by the CS lessons this is how you work with queues and stacks in PHP.
tags: php,queues,stacks
---

The content of this post was inspired by the post To Queue Or Not To Queue by [Vaidehi Joshi](https://dev.to/vaidehijoshi) and from the Base CS Series Video [Stacks and Queues](https://dev.to/vaidehijoshi/stacks-and-queues--basecs-video-series--20oj)

{% link https://dev.to/vaidehijoshi/to-queue-or-not-to-queue %}


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 of `SplQueue` support methods `push()` and `pop()`. But please be advised that if you use `push()` and `pop()` methods on a `SplQueue` object, **it behaves like a stack rather than a queue**
- Manu Manjunath [(original explanation from PHP official documentation)](http://php.net/manual/pt_BR/class.splqueue.php#114336)

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:

```php
$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:

```php
$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:

```php
$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:

```php
$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`](http://php.net/manual/pt_BR/class.splqueue.php)


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! :smile:

--
Thanks for reading out, I'm working on my English and if there is any misspelling please let me know.