Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Stack and Queue in PHP #1002

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions contents/stacks_and_queues/code/php/queue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* The SplQueue class provides the main functionalities of a queue.
* It is integrated into the PHP SPL library which is usable in any PHP application.
*
* Note: top() reads from end of the doubly linked list
* and will not return the first element. Use bottom() for that.
*
* @see https://www.php.net/manual/en/class.splqueue.php
*/
$queue = new SplQueue();

$queue->enqueue(4);
$queue->enqueue(5);
$queue->enqueue(9);

echo $queue->dequeue(), PHP_EOL; // 4 - First in first out
echo $queue->count(), PHP_EOL; // 2 - Elements in the queue
echo $queue->top(), PHP_EOL; // 9 - End of the doubly linked list
echo $queue->bottom(), PHP_EOL; // 5 - Begin of the doubly linked list

// Implementation of own Queue

interface QueueInterface
{
public function enqueue($value);
public function dequeue();
public function front();
public function count();
}

class Queue implements QueueInterface
{
private $queue = [];

public function enqueue($value)
{
$this->queue[] = $value;
}

public function dequeue()
{
return array_shift($this->queue);
}

public function front()
{
return reset($this->queue);
}

public function count()
{
return count($this->queue);
}
}

$queue = new Queue();

$queue->enqueue(4);
$queue->enqueue(5);
$queue->enqueue(9);

echo $queue->dequeue(), PHP_EOL; // 4 - First in first out
echo $queue->count(), PHP_EOL; // 2 - Elements in the queue
echo $queue->front(), PHP_EOL; // 5 - Begin of the array
69 changes: 69 additions & 0 deletions contents/stacks_and_queues/code/php/stack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* The SplStack class provides the main functionalities of a stack.
* It is integrated into the PHP SPL library which is usable in any PHP application.
*
* @see https://www.php.net/manual/en/class.splstack.php
*/
$stack = new SplStack();

$stack->push(4);
$stack->push(5);
$stack->push(9);

echo $stack->pop(), PHP_EOL; // 9 - Last in first out
echo $stack->count(), PHP_EOL; // 2 - Elements in the stack
echo $stack->top(), PHP_EOL; // 5 - End of the doubly linked list
echo $stack->bottom(), PHP_EOL; // 4 - Begin of the doubly linked list

// Implementation of own Stack

interface StackInterface
{
public function push($value);
public function pop();
public function top();
public function bottom();
public function count();
}

class Stack implements StackInterface
{
private $stack = [];

public function push($value)
{
$this->stack[] = $value;
}

public function pop()
{
return array_pop($this->stack);
}

public function top()
{
return end($this->stack);
}

public function bottom()
{
return reset($this->stack);
}

public function count()
{
return count($this->stack);
}
}

$stack = new Stack();

$stack->push(4);
$stack->push(5);
$stack->push(9);

echo $stack->pop(), PHP_EOL; // 9 - Last in first out
echo $stack->count(), PHP_EOL; // 2 - Elements in the stack
echo $stack->top(), PHP_EOL; // 5 - End of the array
echo $stack->bottom(), PHP_EOL; // 4 - Begin of the array
4 changes: 4 additions & 0 deletions contents/stacks_and_queues/stacks_and_queues.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Here is a simple implementation of a stack:
[import, lang:"cpp"](code/cpp/stack.cpp)
{% sample lang="rust" %}
[import, lang:"rust"](code/rust/Stack.rs)
{% sample lang="php" %}
[import, lang:"php"](code/php/stack.php)
{% endmethod %}

Here is a simple implementation of a queue:
Expand All @@ -36,6 +38,8 @@ Here is a simple implementation of a queue:
[import, lang:"cpp"](code/cpp/queue.cpp)
{% sample lang="rust" %}
[import, lang:"rust" ](code/rust/Queue.rs)
{% sample lang="php" %}
[import, lang:"php"](code/php/queue.php)
{% endmethod %}


Expand Down