ImplementStackUsingQueues   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 17
c 2
b 0
f 0
dl 0
loc 37
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A push() 0 9 2
A pop() 0 7 2
A empty() 0 3 2
A top() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace leetcode;
6
7
class ImplementStackUsingQueues
8
{
9
    private array $cache = [];
10
    private array $queue = [];
11
12
    public function push(int $value): void
13
    {
14
        $this->cache[] = $value;
15
        while (count($this->queue)) {
16
            $this->cache[] = array_shift($this->queue);
17
        }
18
        $tmp = $this->cache;
19
        $this->cache = $this->queue;
20
        $this->queue = $tmp;
21
    }
22
23
    public function pop(): ?int
24
    {
25
        if (empty($this->queue)) {
26
            return null;
27
        }
28
29
        return array_shift($this->queue);
30
    }
31
32
    public function top(): ?int
33
    {
34
        if (empty($this->queue)) {
35
            return null;
36
        }
37
38
        return current($this->queue);
39
    }
40
41
    public function empty(): bool
42
    {
43
        return empty($this->queue) && empty($this->cache);
44
    }
45
}
46