-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.php
More file actions
78 lines (72 loc) · 1.62 KB
/
Queue.php
File metadata and controls
78 lines (72 loc) · 1.62 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/**
* A simple queue implementation in PHP
*/
class Queue
{
private $items = array();
private $front = 0;
private $rear = -1;
private $size = 0;
/**
* Add an item to the end of the queue
* @param mixed $item The item to add to the queue
*/
public function enqueue($item)
{
$this->items[++$this->rear] = $item;
$this->size++;
}
/**
* Remove and return the item at the front of the queue
* @return mixed The item at the front of the queue
*/
public function dequeue()
{
if ($this->isEmpty()) {
return null;
}
$item = $this->items[$this->front];
unset($this->items[$this->front]);
$this->front++;
$this->size--;
return $item;
}
/**
* Get the item at the front of the queue without removing it
* @return mixed The item at the front of the queue
*/
public function peek()
{
if ($this->isEmpty()) {
return null;
}
return $this->items[$this->front];
}
/**
* Check if the queue is empty
* @return bool True if the queue is empty, false otherwise
*/
public function isEmpty()
{
return $this->size === 0;
}
/**
* Get the number of items in the queue
* @return int The number of items in the queue
*/
public function size()
{
return $this->size;
}
/**
* Clear all items from the queue
*/
public function clear()
{
$this->items = array();
$this->front = 0;
$this->rear = -1;
$this->size = 0;
}
}