|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
//TODO: Documentation, Unit Test |
|
4
|
|
|
|
|
5
|
|
|
namespace Ptypes; |
|
6
|
|
|
|
|
7
|
|
|
use Countable; |
|
8
|
|
|
use Ptypes\Exceptions\InvalidArgument; |
|
9
|
|
|
|
|
10
|
|
|
class DLNode //Doubly Linked Node |
|
11
|
|
|
{ |
|
12
|
|
|
public $next = null, $prev = null; //A reference to the next and previous node |
|
13
|
|
|
public $data = null; //Data or a reference to data |
|
14
|
|
|
|
|
15
|
|
|
public function __construct(){} |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
class DoublyLinkedList implements Countable |
|
19
|
|
|
{ |
|
20
|
|
|
public $firstNode = null, $lastNode = null; //points to the first and last node of the list |
|
21
|
|
|
|
|
22
|
|
|
private $size = 0; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(){} |
|
25
|
|
|
|
|
26
|
|
|
public function insert_after($node, $newNode) |
|
27
|
|
|
{ |
|
28
|
|
|
//type checks |
|
29
|
|
|
if(get_class($node) != "DLNode"){throw new InvalidArgument("Expected a DLNode, got: ".get_class($node));} |
|
30
|
|
|
if(get_class($newNode) != "DLNode"){throw new InvalidArgument("Expected a DLNode, got: ".get_class($newNode));} |
|
31
|
|
|
|
|
32
|
|
|
$newNode->prev = $node; |
|
33
|
|
|
if($node->next == null) |
|
34
|
|
|
{ |
|
35
|
|
|
$newNode->next = null; |
|
36
|
|
|
$this->lastNode = $newNode; |
|
37
|
|
|
} |
|
38
|
|
|
else |
|
39
|
|
|
{ |
|
40
|
|
|
$newNode->next = $node->next; |
|
41
|
|
|
$node->next->prev = $newNode; |
|
42
|
|
|
$node->next = $newNode; |
|
43
|
|
|
} |
|
44
|
|
|
$this->size += 1; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function insert_before($node, $newNode) |
|
48
|
|
|
{ |
|
49
|
|
|
//type checks |
|
50
|
|
|
if(get_class($node) != "DLNode"){throw new InvalidArgument("Expected a DLNode, got: ".get_class($node));} |
|
51
|
|
|
if(get_class($newNode) != "DLNode"){throw new InvalidArgument("Expected a DLNode, got: ".get_class($newNode));} |
|
52
|
|
|
|
|
53
|
|
|
$newNode->next = $node; |
|
54
|
|
|
if($node->prev == null) |
|
55
|
|
|
{ |
|
56
|
|
|
$newNode->prev = null; |
|
57
|
|
|
$this->firstNode = $newNode; |
|
58
|
|
|
} |
|
59
|
|
|
else |
|
60
|
|
|
{ |
|
61
|
|
|
$newNode->prev = $node->prev; |
|
62
|
|
|
$node->prev->next = $newNode; |
|
63
|
|
|
$node->prev = $newNode; |
|
64
|
|
|
} |
|
65
|
|
|
$this->size += 1; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function insert_beginning($newNode) |
|
69
|
|
|
{ |
|
70
|
|
|
//type checks |
|
71
|
|
|
if(get_class($newNode) != "DLNode"){throw new InvalidArgument("Expected a DLNode, got: ".get_class($newNode));} |
|
72
|
|
|
|
|
73
|
|
|
if($this->firstNode == null) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->firstNode = $newNode; |
|
76
|
|
|
$this->lastNode = $newNode; |
|
77
|
|
|
$newNode->prev = null; |
|
78
|
|
|
$newNode->next = null; |
|
79
|
|
|
} |
|
80
|
|
|
else |
|
81
|
|
|
{ |
|
82
|
|
|
$this->insert_before($this->firstNode, $newNode); |
|
83
|
|
|
} |
|
84
|
|
|
$this->size += 1; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function insert_end($newNode) |
|
88
|
|
|
{ |
|
89
|
|
|
if($this->lastNode == null) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->insert_beginning($newNode); |
|
92
|
|
|
} |
|
93
|
|
|
else |
|
94
|
|
|
{ |
|
95
|
|
|
$this->insert_after($this->lastNode, $newNode); |
|
96
|
|
|
} |
|
97
|
|
|
$this->size += 1; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function contains($node) |
|
101
|
|
|
{ |
|
102
|
|
|
//check this so we could possibly avoid list traversal |
|
103
|
|
|
if($node == $this->firstNode || $node == $this->lastNode) {return true;} |
|
104
|
|
|
|
|
105
|
|
|
//traverse the list and try to find the node |
|
106
|
|
|
$n = $this->firstNode->next; //we can start one node ahead as we already checked the firstNode |
|
107
|
|
|
while($n != null) |
|
108
|
|
|
{ |
|
109
|
|
|
if($n == $node) {return true;} |
|
110
|
|
|
$n = $n->next; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function remove($node) |
|
117
|
|
|
{ |
|
118
|
|
|
//check if the node exists |
|
119
|
|
|
if($this->contains($node) == false){throw new InvalidArgument("The node given does not exist in this list!");} |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
if($node->prev == null) |
|
122
|
|
|
{ |
|
123
|
|
|
$this->firstNode = $node->next; |
|
124
|
|
|
} |
|
125
|
|
|
else |
|
126
|
|
|
{ |
|
127
|
|
|
$node->prev->next = $node->next; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if($node->next == null) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->lastNode = $node->prev; |
|
133
|
|
|
} |
|
134
|
|
|
else |
|
135
|
|
|
{ |
|
136
|
|
|
$node->next->prev = $node->prev; |
|
137
|
|
|
} |
|
138
|
|
|
$this->size -= 1; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function size() |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->size; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
//Overriding functions & magic methods below |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Count, same functionality as Size. |
|
150
|
|
|
* Overrides the default count function call on this class. |
|
151
|
|
|
* |
|
152
|
|
|
* @return int |
|
153
|
|
|
*/ |
|
154
|
|
|
public function count() |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->size(); |
|
157
|
|
|
} |
|
158
|
|
|
} |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.