File tree Expand file tree Collapse file tree 6 files changed +156
-6
lines changed Expand file tree Collapse file tree 6 files changed +156
-6
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 11
11
12
12
class LinkedList implements \Iterator
13
13
{
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 ;
18
18
19
19
public function insert (string $ data = NULL )
20
20
{
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 15
15
16
16
class AgentQueueList implements Queue
17
17
{
18
- private $ limit ;
19
- private $ queue ;
18
+ protected $ limit ;
19
+ protected $ queue ;
20
20
21
21
/**
22
22
* AgentQueueList constructor.
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments