Skip to content

Commit de33c3f

Browse files
committed
Restructured the code base
1 parent 4158bac commit de33c3f

File tree

28 files changed

+1219
-23
lines changed

28 files changed

+1219
-23
lines changed

DS/LinkedList/Classes/LinkedList.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ public function insert(string $data = NULL) {
2121
$newNode = new ListNode($data);
2222
if ($this->_firstNode === NULL) {
2323
$this->_firstNode = &$newNode;
24-
$this->_lastNode = $newNode;
24+
$this->_lastNode = &$newNode;
2525
} else {
2626
$currentNode = $this->_lastNode;
2727
$currentNode->next = $newNode;
2828
$newNode->prev = $currentNode;
29-
$this->_lastNode = $newNode;
29+
$this->_lastNode = &$newNode;
3030
}
3131
$this->_totalNode++;
3232
return TRUE;
@@ -36,10 +36,12 @@ public function insertAtFirst(string $data = NULL) {
3636
$newNode = new ListNode($data);
3737
if ($this->_firstNode === NULL) {
3838
$this->_firstNode = &$newNode;
39+
$this->_lastNode = &$newNode;
3940
} else {
4041
$currentFirstNode = $this->_firstNode;
4142
$this->_firstNode = &$newNode;
4243
$newNode->next = $currentFirstNode;
44+
$this->_lastNode = &$currentFirstNode;
4345
}
4446
$this->_totalNode++;
4547
return TRUE;
@@ -73,6 +75,7 @@ public function insertBefore(string $data = NULL, string $query = NULL) {
7375
}
7476
$previous = $currentNode;
7577
$currentNode = $currentNode->next;
78+
7679
}
7780
}
7881
}
@@ -90,6 +93,11 @@ public function insertAfter(string $data = NULL, string $query = NULL) {
9093
}
9194
$currentNode->next = $newNode;
9295
$this->_totalNode++;
96+
97+
if($currentNode === $this->_lastNode) {
98+
$this->_lastNode = &$newNode;
99+
}
100+
93101
break;
94102
}
95103
$currentNode = $currentNode->next;
@@ -104,6 +112,7 @@ public function deleteFirst() {
104112
$this->_firstNode = $this->_firstNode->next;
105113
} else {
106114
$this->_firstNode = NULL;
115+
$this->_lastNode = NULL;
107116
}
108117
$this->_totalNode--;
109118
return TRUE;
@@ -125,6 +134,7 @@ public function deleteLast() {
125134
}
126135

127136
$previousNode->next = NULL;
137+
$this->_lastNode = &$previousNode;
128138
$this->_totalNode--;
129139
return TRUE;
130140
}
@@ -143,6 +153,10 @@ public function delete(string $query = NULL) {
143153
} else {
144154
$previous->next = $currentNode->next;
145155
}
156+
157+
if($currentNode === $this->_lastNode) {
158+
$this->_lastNode = &$previous;
159+
}
146160

147161
$this->_totalNode--;
148162
break;

DS/Queue/Classes/CircularQueue.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* Author: Mizanur Rahman <[email protected]>
5+
*
6+
*/
7+
8+
namespace DS\Queue\Classes;
9+
use \DS\Queue\Interfaces\QueueInterface;
10+
11+
12+
class CircularQueue implements QueueInterface {
13+
14+
private $queue;
15+
private $limit;
16+
private $front = 0;
17+
private $rear = 0;
18+
19+
public function __construct(int $limit = 5) {
20+
$this->limit = $limit;
21+
$this->queue = [];
22+
}
23+
24+
public function size() {
25+
if ($this->rear > $this->front)
26+
return $this->rear - $this->front;
27+
return $this->limit - $this->front + $this->rear;
28+
}
29+
30+
public function isEmpty() {
31+
return $this->rear == $this->front;
32+
}
33+
34+
public function isFull() {
35+
$diff = $this->rear - $this->front;
36+
if ($diff == -1 || $diff == ($this->limit - 1))
37+
return true;
38+
return false;
39+
}
40+
41+
public function enqueue(string $item) {
42+
if ($this->isFull()) {
43+
throw new OverflowException("Queue is Full.");
44+
} else {
45+
$this->queue[$this->rear] = $item;
46+
$this->rear = ($this->rear + 1) % $this->limit;
47+
}
48+
}
49+
50+
public function dequeue() {
51+
$item = "";
52+
if ($this->isEmpty()) {
53+
throw new UnderflowException("Queue is empty");
54+
} else {
55+
$item = $this->queue[$this->front];
56+
$this->queue[$this->front] = NULL;
57+
$this->front = ($this->front + 1) % $this->limit;
58+
}
59+
return $item;
60+
}
61+
62+
public function peek() {
63+
return $this->queue[$this->front];
64+
}
65+
66+
}

DS/Queue/Classes/DeQueue.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/*
4+
* Author: Mizanur Rahman <[email protected]>
5+
*
6+
*/
7+
8+
namespace DS\Queue\Classes;
9+
10+
use \DS\LinkedList\Classes\LinkedList;
11+
12+
class DeQueue {
13+
14+
private $limit;
15+
private $queue;
16+
17+
public function __construct(int $limit = 20) {
18+
$this->limit = $limit;
19+
$this->queue = new LinkedList();
20+
}
21+
22+
public function dequeueFromFront() {
23+
24+
if ($this->isEmpty()) {
25+
throw new UnderflowException('Queue is empty');
26+
} else {
27+
$lastItem = $this->peekFront();
28+
$this->queue->deleteFirst();
29+
return $lastItem;
30+
}
31+
}
32+
33+
public function dequeueFromBack() {
34+
35+
if ($this->isEmpty()) {
36+
throw new UnderflowException('Queue is empty');
37+
} else {
38+
$lastItem = $this->peekBack();
39+
$this->queue->deleteLast();
40+
return $lastItem;
41+
}
42+
}
43+
44+
public function enqueueAtBack(string $newItem) {
45+
46+
if ($this->queue->getSize() < $this->limit) {
47+
$this->queue->insert($newItem);
48+
} else {
49+
throw new OverflowException('Queue is full');
50+
}
51+
}
52+
53+
public function enqueueAtFront(string $newItem) {
54+
55+
if ($this->queue->getSize() < $this->limit) {
56+
$this->queue->insertAtFirst($newItem);
57+
} else {
58+
throw new OverflowException('Queue is full');
59+
}
60+
}
61+
62+
public function peekFront() {
63+
return $this->queue->getNthNode(1)->data;
64+
}
65+
66+
public function peekBack() {
67+
return $this->queue->getNthNode($this->queue->getSize())->data;
68+
}
69+
70+
public function isEmpty(): bool {
71+
return $this->queue->getSize() == 0;
72+
}
73+
74+
}

DS/Queue/Classes/PriorityQueue.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* Author: Mizanur Rahman <[email protected]>
5+
*
6+
*/
7+
8+
namespace DS\Queue\Classes;
9+
10+
class PriorityQueue extends \SplPriorityQueue {
11+
12+
public function compare($priority1, $priority2) {
13+
return $priority1 <=> $priority2;
14+
}
15+
16+
}

DS/Queue/Classes/Queue.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* Author: Mizanur Rahman <[email protected]>
5+
*
6+
*/
7+
8+
namespace DS\Queue\Classes;
9+
use \DS\Queue\Interfaces\QueueInterface;
10+
use \DS\LinkedList\Classes\LinkedList;
11+
12+
class Queue implements QueueInterface {
13+
14+
private $limit;
15+
private $queue;
16+
17+
public function __construct(int $limit = 20) {
18+
$this->limit = $limit;
19+
$this->queue = new LinkedList();
20+
}
21+
22+
public function dequeue(): string {
23+
24+
if ($this->isEmpty()) {
25+
throw new UnderflowException('Queue is empty');
26+
} else {
27+
$lastItem = $this->peek();
28+
$this->queue->deleteFirst();
29+
return $lastItem;
30+
}
31+
}
32+
33+
public function enqueue(string $newItem) {
34+
35+
if ($this->queue->getSize() < $this->limit) {
36+
$this->queue->insert($newItem);
37+
} else {
38+
throw new OverflowException('Queue is full');
39+
}
40+
}
41+
42+
public function peek(): string {
43+
return $this->queue->getNthNode(1)->data;
44+
}
45+
46+
public function isEmpty(): bool {
47+
return $this->queue->getSize() == 0;
48+
}
49+
50+
}

DS/Queue/Classes/QueueArray.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* Author: Mizanur Rahman <[email protected]>
5+
*
6+
*/
7+
8+
namespace DS\Queue\Classes;
9+
use \DS\Queue\Interfaces\QueueInterface;
10+
11+
12+
class QueueArray implements QueueInterface {
13+
14+
private $limit;
15+
private $queue;
16+
17+
public function __construct(int $limit = 20) {
18+
$this->limit = $limit;
19+
$this->queue = [];
20+
}
21+
22+
public function dequeue(): string {
23+
24+
if ($this->isEmpty()) {
25+
throw new UnderflowException('Queue is empty');
26+
} else {
27+
return array_shift($this->queue);
28+
}
29+
}
30+
31+
public function enqueue(string $newItem) {
32+
33+
if (count($this->queue) < $this->limit) {
34+
array_push($this->queue, $newItem);
35+
} else {
36+
throw new OverflowException('Queue is full');
37+
}
38+
}
39+
40+
public function peek(): string {
41+
return current($this->queue);
42+
}
43+
44+
public function isEmpty(): bool {
45+
return empty($this->queue);
46+
}
47+
48+
}

DS/Queue/Examples/1.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
4+
/*
5+
* Author: Mizanur Rahman <[email protected]>
6+
*
7+
*/
8+
9+
include __DIR__ . '/../../../Vendor/Autoload.php';
10+
11+
use \DS\Queue\Classes\QueueArray;
12+
13+
try {
14+
$agents = new QueueArray(10);
15+
$agents->enqueue("Fred");
16+
$agents->enqueue("John");
17+
$agents->enqueue("Keith");
18+
$agents->enqueue("Adiyan");
19+
$agents->enqueue("Mikhael");
20+
echo $agents->dequeue()."\n";
21+
echo $agents->dequeue()."\n";
22+
echo $agents->peek()."\n";
23+
} catch (Exception $e) {
24+
echo $e->getMessage();
25+
}

DS/Queue/Examples/2.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
4+
/*
5+
* Author: Mizanur Rahman <[email protected]>
6+
*
7+
*/
8+
9+
include __DIR__ . '/../../../Vendor/Autoload.php';
10+
11+
use \DS\Queue\Classes\Queue;
12+
13+
try {
14+
$agents = new Queue(10);
15+
$agents->enqueue("Fred");
16+
$agents->enqueue("John");
17+
$agents->enqueue("Keith");
18+
$agents->enqueue("Adiyan");
19+
$agents->enqueue("Mikhael");
20+
echo $agents->dequeue()."\n";
21+
echo $agents->dequeue()."\n";
22+
echo $agents->peek()."\n";
23+
} catch (Exception $e) {
24+
echo $e->getMessage();
25+
}

0 commit comments

Comments
 (0)