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 #996

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ This file lists everyone, who contributed to this repo and wanted to show up her
- K. Shudipto Amin
- Peanutbutter_Warrior
- Thijs Raymakers
- Abdullah Al Zawad
81 changes: 81 additions & 0 deletions contents/stacks_and_queues/code/php/queue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/**
* @template T
*/
interface IQueue
{
/**
* Removes the first element from the queue and returns it.
* @return T | null
*/
public function dequeue();

/**
* Adds an element at the end of the queue and returns the new size.
* @param T $element
*/
public function enqueue($element): int;

/**
* Returns the length of the queue.
*/
public function size(): int;

/**
* Returns the first element of the queue.
* @return T
*/
public function front();
}

/**
* @template T
* @implements IQueue<T>
*/
class Queue implements IQueue
{
/**
* @var array<T> $elements
*/
private $elements = [];

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

public function enqueue($element): int
{
array_push($this->elements, $element);
return $this->size();
}

public function size(): int
{
return count($this->elements);
}

public function front()
{
return $this->elements[0];
}
}

function example_queue(): void
{
/**
* @var Queue<int> $int_queue
*/
$int_queue = new Queue();

$int_queue->enqueue(4);
$int_queue->enqueue(5);
$int_queue->enqueue(7);

echo $int_queue->dequeue() . "\n"; // 4
echo $int_queue->size() . "\n"; // 2
echo $int_queue->front() . "\n"; // 5
}

example_queue();
82 changes: 82 additions & 0 deletions contents/stacks_and_queues/code/php/stack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/**
* @template T
*/
interface IStack
{
/**
* Removes the last element from the stack and returns it.
* @return T | null
*/
public function pop();

/**
* Adds an element at the end of the stack and returns the new size.
* @param T $element
*/
public function push($element): int;

/**
* Returns the length of the stack.
*/
public function size(): int;

/**
* Returns the first element of the stack.
* @return T
*/
public function top();
}

/**
* @template T
* @implements IStack<T>
*/
class Stack implements IStack
{
/**
* @var array<T> $elements
*/
private $elements = [];

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

public function push($element): int
{
array_push($this->elements, $element);
return $this->size();
}

public function size(): int
{
return count($this->elements);
}

public function top()
{
return $this->elements[0];
}
}

function example_stack(): void
{
/**
* @var Stack<int> $int_stack
*/
$int_stack = new Stack();

$int_stack->push(4);
$int_stack->push(5);
$int_stack->push(7);

echo $int_stack->pop() . "\n"; // 7
echo $int_stack->size() . "\n"; // 2
echo $int_stack->top() . "\n"; // 4
}

example_stack();

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