-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_implementation.php
More file actions
52 lines (44 loc) · 992 Bytes
/
queue_implementation.php
File metadata and controls
52 lines (44 loc) · 992 Bytes
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
<?php
# Queue Implementation using PHP array functions - array_unshift, array_pop
class Queue
{
protected $queue;
protected $limit;
public function __construct($limit = 10)
{
$this->queue = array();
$this->limit = $limit;
}
public function push($x)
{
if(count($this->queue) < $this->limit)
array_unshift($this->queue, $x); // Insert an element into queue
else
echo "Queue is Full";
}
public function pop()
{
if(count($this->queue) == 0)
echo "Queue is Empty";
else
array_pop($this->queue); // Remove the top element from stack
}
public function dump()
{
echo "<pre>";print_r($this->queue);
}
}
$obj = new Queue();
$obj->push(2);
$obj->push(1);
$obj->push(5);
$obj->push(3);
$obj->push(9);
$obj->push(13);
$obj->push(33);
$obj->push(2);
$obj->push(11);
$obj->push(8);
$obj->dump();
$obj->pop();
$obj->dump();