Skip to content

Commit a8ea318

Browse files
committed
Added Priority Queue as a Linked List example + changed property access modifiers
1 parent 3bf47c7 commit a8ea318

File tree

6 files changed

+156
-6
lines changed

6 files changed

+156
-6
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: bert
5+
* Date: 3/10/18
6+
* Time: 23:33
7+
*/
8+
9+
use App\Chapter04\AgentPriorityQueueList;
10+
11+
require_once __DIR__ . "/../vendor/autoload.php";
12+
13+
try {
14+
$agents = new AgentPriorityQueueList(10);
15+
16+
$agents->enqueueWithPriority("Fred", 1);
17+
$agents->enqueueWithPriority("John", 2);
18+
$agents->enqueueWithPriority("Keith", 3);
19+
$agents->enqueueWithPriority("Adiyan", 4);
20+
$agents->enqueueWithPriority("Michael", 2);
21+
22+
$agents->display();
23+
24+
echo $agents->dequeue() . PHP_EOL;
25+
echo $agents->dequeue() . PHP_EOL;
26+
27+
28+
} catch (Exception $e){
29+
echo $e->getMessage();
30+
}

src/Chapter03/LinkedList.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
class LinkedList implements \Iterator
1313
{
14-
private $_firstNode = NULL;
15-
private $_totalNodes = 0;
16-
private $_currentNode = NULL;
17-
private $_currentPosition = 0;
14+
protected $_firstNode = NULL;
15+
protected $_totalNodes = 0;
16+
protected $_currentNode = NULL;
17+
protected $_currentPosition = 0;
1818

1919
public function insert(string $data = NULL)
2020
{
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: bert
5+
* Date: 3/10/18
6+
* Time: 23:26
7+
*/
8+
9+
namespace App\Chapter04;
10+
11+
12+
use OverflowException;
13+
14+
class AgentPriorityQueueList extends AgentQueueList
15+
{
16+
public function __construct(int $limit = 20)
17+
{
18+
$this->limit = $limit;
19+
$this->queue = new PriorityQueueLinkedList();
20+
}
21+
22+
public function enqueueWithPriority(string $item, int $priority)
23+
{
24+
if ($this->queue->getSize() < $this->limit){
25+
$this->queue->insert($item, $priority);
26+
} else {
27+
throw new OverflowException("Queue is full");
28+
}
29+
}
30+
31+
public function display()
32+
{
33+
// TODO : this will show the 'Total books line', because of the implementation of display in LinkedList!
34+
// This is in fact a weak way of using display method, but so be it!
35+
$this->queue->display();
36+
}
37+
38+
39+
}

src/Chapter04/AgentQueueList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
class AgentQueueList implements Queue
1717
{
18-
private $limit;
19-
private $queue;
18+
protected $limit;
19+
protected $queue;
2020

2121
/**
2222
* AgentQueueList constructor.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: bert
5+
* Date: 3/10/18
6+
* Time: 23:13
7+
*/
8+
9+
namespace App\Chapter04;
10+
11+
12+
use App\Chapter03\LinkedList;
13+
14+
class PriorityQueueLinkedList extends LinkedList
15+
{
16+
public function insert(string $data = null, int $priority = NULL)
17+
{
18+
$newNode = new PriorityQueueListNode($data, $priority);
19+
$this->_totalNodes++;
20+
21+
// List is empty - newly added node is the first.
22+
if ($this->_firstNode === NULL){
23+
$this->_firstNode = $newNode;
24+
} else {
25+
$previous = $this->_firstNode;
26+
$currentNode = $this->_firstNode;
27+
while ($currentNode !== NULL){
28+
// List is not emptu, but new item has highest priority
29+
if ($currentNode->priority < $priority){
30+
if ($currentNode == $this->_firstNode){
31+
$previous = $this->_firstNode;
32+
$this->_firstNode = $newNode;
33+
$newNode->next = $previous;
34+
return;
35+
}
36+
37+
$newNode->next = $currentNode;
38+
$previous->next = $newNode;
39+
return;
40+
}
41+
$previous = $currentNode;
42+
$currentNode = $currentNode->next;
43+
}
44+
}
45+
return true;
46+
}
47+
48+
49+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: bert
5+
* Date: 3/10/18
6+
* Time: 23:04
7+
*/
8+
9+
namespace App\Chapter04;
10+
11+
12+
use App\Chapter03\ListNode;
13+
14+
class PriorityQueueListNode extends ListNode
15+
{
16+
17+
public $data = NULL;
18+
public $next = NULL;
19+
public $priority = NULL;
20+
21+
/**
22+
* PriorityQueueListNode constructor.
23+
* @param null $data
24+
* @param null $priority
25+
*/
26+
public function __construct(string $data = NULL, int $priority = NULL)
27+
{
28+
$this->data = $data;
29+
$this->priority = $priority;
30+
}
31+
32+
}

0 commit comments

Comments
 (0)