diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 8bf39132c..c099d20f5 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -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 diff --git a/contents/stacks_and_queues/code/php/queue.php b/contents/stacks_and_queues/code/php/queue.php new file mode 100644 index 000000000..6415f8ea0 --- /dev/null +++ b/contents/stacks_and_queues/code/php/queue.php @@ -0,0 +1,81 @@ + + */ +class Queue implements IQueue +{ + /** + * @var array $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_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(); diff --git a/contents/stacks_and_queues/code/php/stack.php b/contents/stacks_and_queues/code/php/stack.php new file mode 100644 index 000000000..2d6b1f01d --- /dev/null +++ b/contents/stacks_and_queues/code/php/stack.php @@ -0,0 +1,82 @@ + + */ +class Stack implements IStack +{ + /** + * @var array $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_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(); + diff --git a/contents/stacks_and_queues/stacks_and_queues.md b/contents/stacks_and_queues/stacks_and_queues.md index b695f53bd..1a0f9d32c 100644 --- a/contents/stacks_and_queues/stacks_and_queues.md +++ b/contents/stacks_and_queues/stacks_and_queues.md @@ -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: @@ -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 %}