forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequential.php
More file actions
60 lines (52 loc) · 1.3 KB
/
Sequential.php
File metadata and controls
60 lines (52 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
class LoopQueue
{
private $MaxSzie;
private $data = [];
private $head = 0;
private $tail = 0;
/**
* 初始化队列大小 最后的位置不存放数据,实际大小 = size++
*/
public function __construct($size = 10)
{
$this->MaxSzie = ++$size;
}
/**
* 队列满条件 ($this->tail+1) % $this->MaxSzie == $this->head
*/
public function enQueue($data)
{
if (($this->tail+1) % $this->MaxSzie == $this->head)
return -1;
$this->data[$this->tail] = $data;
$this->tail = (++$this->tail) % $this->MaxSzie;
}
public function deQueue()
{
if ($this->head == $this->tail)
return NULL;
$data = $this->data[$this->head];
unset($this->data[$this->head]);
$this->head = (++$this->head) % $this->MaxSzie;
return $data;
}
public function getLength()
{
return ($this->tail - $this->head + $this->MaxSzie) % $this->MaxSzie;
}
}
$queue = new LoopQueue(4);
// var_dump($queue);
$queue->enQueue(1);
$queue->enQueue(2);
$queue->enQueue(3);
$queue->enQueue(4);
// $queue->enQueue(5);
var_dump($queue->getLength());
$queue->deQueue();
$queue->deQueue();
$queue->deQueue();
$queue->deQueue();
$queue->deQueue();
var_dump($queue);