-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.php
58 lines (41 loc) · 1.02 KB
/
queue.php
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
<?php
define(MAX_QUEUE_SIZE,100);
class Queue {
private $queue_size = MAX_QUEUE_SIZE;
private $front = 0;
private $size = 0;
private $queue_array = array();
public function __construct($queue_size) {
if($queue_size <= MAX_QUEUE_SIZE) {
$this->queue_size = $queue_size;
}
}
public function Enqueue($data) {
if($this->size == $this->queue_size) {
throw new Exception("Queue is full");
}
$this->queue_array[($this->front+$this->size)%$this->queue_size] = $data;
$this->size++;
}
public function Dequeue() {
if($this->size) {
$data = $this->queue_array[$this->front];
$this->queue_array[$this->front] = NULL;
$this->size--;
$this->front = ($this->front+1) % $this->queue_size;
return $data;
} else {
throw new Exception("Queue is empty");
}
}
public function GetSize() {
return $this->size;
}
public function IsFull() {
return $this->size == $this->queue_size;
}
public function IsEmpty() {
return !$this->size;
}
}
?>