Skip to content

Commit f0ee9b9

Browse files
committed
Implement stack and queue in PHP
1 parent 98ca4ab commit f0ee9b9

File tree

4 files changed

+168
-0
lines changed

4 files changed

+168
-0
lines changed

CONTRIBUTORS.md

+1
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ This file lists everyone, who contributed to this repo and wanted to show up her
6464
- K. Shudipto Amin
6565
- Peanutbutter_Warrior
6666
- Thijs Raymakers
67+
- Abdullah Al Zawad
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/**
4+
* @template T
5+
*/
6+
interface IQueue
7+
{
8+
/**
9+
* Removes the first element from the queue and returns it.
10+
* @return T | null
11+
*/
12+
public function dequeue();
13+
14+
/**
15+
* Adds an element at the end of the queue and returns the new size.
16+
* @param T $element
17+
*/
18+
public function enqueue($element): int;
19+
20+
/**
21+
* Returns the length of the queue.
22+
*/
23+
public function size(): int;
24+
25+
/**
26+
* Returns the first element of the queue.
27+
* @return T
28+
*/
29+
public function front();
30+
}
31+
32+
/**
33+
* @template T
34+
* @implements IQueue<T>
35+
*/
36+
class Queue implements IQueue
37+
{
38+
/**
39+
* @var array<T> $elements
40+
*/
41+
private $elements = [];
42+
43+
public function dequeue()
44+
{
45+
return array_shift($this->elements);
46+
}
47+
48+
public function enqueue($element): int
49+
{
50+
array_push($this->elements, $element);
51+
return $this->size();
52+
}
53+
54+
public function size(): int
55+
{
56+
return count($this->elements);
57+
}
58+
59+
public function front()
60+
{
61+
return $this->elements[0];
62+
}
63+
}
64+
65+
function example_queue(): void
66+
{
67+
/**
68+
* @var Queue<int> $int_queue
69+
*/
70+
$int_queue = new Queue();
71+
72+
$int_queue->enqueue(4);
73+
$int_queue->enqueue(5);
74+
$int_queue->enqueue(7);
75+
76+
echo $int_queue->dequeue() . "\n"; // 4
77+
echo $int_queue->size() . "\n"; // 2
78+
echo $int_queue->front() . "\n"; // 5
79+
}
80+
81+
example_queue();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/**
4+
* @template T
5+
*/
6+
interface IStack
7+
{
8+
/**
9+
* Removes the last element from the stack and returns it.
10+
* @return T | null
11+
*/
12+
public function pop();
13+
14+
/**
15+
* Adds an element at the end of the stack and returns the new size.
16+
* @param T $element
17+
*/
18+
public function push($element): int;
19+
20+
/**
21+
* Returns the length of the stack.
22+
*/
23+
public function size(): int;
24+
25+
/**
26+
* Returns the first element of the stack.
27+
* @return T
28+
*/
29+
public function top();
30+
}
31+
32+
/**
33+
* @template T
34+
* @implements IStack<T>
35+
*/
36+
class Stack implements IStack
37+
{
38+
/**
39+
* @var array<T> $elements
40+
*/
41+
private $elements = [];
42+
43+
public function pop()
44+
{
45+
return array_pop($this->elements);
46+
}
47+
48+
public function push($element): int
49+
{
50+
array_push($this->elements, $element);
51+
return $this->size();
52+
}
53+
54+
public function size(): int
55+
{
56+
return count($this->elements);
57+
}
58+
59+
public function top()
60+
{
61+
return $this->elements[0];
62+
}
63+
}
64+
65+
function example_stack(): void
66+
{
67+
/**
68+
* @var Stack<int> $int_stack
69+
*/
70+
$int_stack = new Stack();
71+
72+
$int_stack->push(4);
73+
$int_stack->push(5);
74+
$int_stack->push(7);
75+
76+
echo $int_stack->pop() . "\n"; // 7
77+
echo $int_stack->size() . "\n"; // 2
78+
echo $int_stack->top() . "\n"; // 4
79+
}
80+
81+
example_stack();
82+

contents/stacks_and_queues/stacks_and_queues.md

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Here is a simple implementation of a stack:
2424
[import, lang:"cpp"](code/cpp/stack.cpp)
2525
{% sample lang="rust" %}
2626
[import, lang:"rust"](code/rust/Stack.rs)
27+
{% sample lang="php" %}
28+
[import, lang:"php"](code/php/stack.php)
2729
{% endmethod %}
2830

2931
Here is a simple implementation of a queue:
@@ -36,6 +38,8 @@ Here is a simple implementation of a queue:
3638
[import, lang:"cpp"](code/cpp/queue.cpp)
3739
{% sample lang="rust" %}
3840
[import, lang:"rust" ](code/rust/Queue.rs)
41+
{% sample lang="php" %}
42+
[import, lang:"php"](code/php/queue.php)
3943
{% endmethod %}
4044

4145

0 commit comments

Comments
 (0)