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..d61d92062 --- /dev/null +++ b/contents/stacks_and_queues/code/php/queue.php @@ -0,0 +1,65 @@ +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 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..6d63dafac --- /dev/null +++ b/contents/stacks_and_queues/code/php/stack.php @@ -0,0 +1,69 @@ +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 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 %}