ImplementStackUsingQueues::empty()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 2
nc 2
nop 0
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