1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DataStructures for PHP |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/SiroDiaz/DataStructures |
6
|
|
|
* @copyright Copyright (c) 2017 Siro Díaz Palazón |
7
|
|
|
* @license https://github.com/SiroDiaz/DataStructures/blob/master/README.md (MIT License) |
8
|
|
|
*/ |
9
|
|
|
namespace DataStructures\Lists; |
10
|
|
|
|
11
|
|
|
use DataStructures\Lists\Interfaces\ListInterface; |
12
|
|
|
use OutOfBoundsException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* ListAbstract |
16
|
|
|
* |
17
|
|
|
* ListAbstract is a superclass that implements common operations in lists. |
18
|
|
|
* Also define abstract methods that are designed for implement a template method |
19
|
|
|
* design pattern. |
20
|
|
|
* |
21
|
|
|
* @author Siro Diaz Palazon <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
abstract class ListAbstract implements ListInterface { |
24
|
|
|
protected $size; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Insert a node in the specified list position. |
28
|
|
|
* |
29
|
|
|
* @param integer $index position |
30
|
|
|
* @param mixed $data data to be saved |
31
|
|
|
*/ |
32
|
|
|
public function insert($index, $data) { |
33
|
|
|
if($index < 0) { |
34
|
|
|
throw new OutOfBoundsException(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if($index === 0) { |
38
|
|
|
$this->insertBeginning($data); |
39
|
|
|
} else if($index >= $this->size) { |
40
|
|
|
$this->insertEnd($data); |
41
|
|
|
} else if($index > 0 && $index < $this->size) { |
42
|
|
|
$this->insertAt($index, $data); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->size++; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Add a new node in the specified index. |
50
|
|
|
* |
51
|
|
|
* @param integer $index the position. |
52
|
|
|
* @param mixed $data the data to be stored. |
53
|
|
|
*/ |
54
|
|
|
protected abstract function insertAt($index, $data); |
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Add a new node in the specified index. |
58
|
|
|
* |
59
|
|
|
* @param mixed $data the data to be stored. |
60
|
|
|
*/ |
61
|
|
|
protected abstract function insertEnd($data); |
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Inserts at the beginning of the list. |
65
|
|
|
* |
66
|
|
|
* @param mixed $data |
67
|
|
|
*/ |
68
|
|
|
protected abstract function insertBeginning($data); |
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Removes all nodes of the list. It removes from the beginning. |
72
|
|
|
*/ |
73
|
|
|
public function clear() { |
74
|
|
|
while($this->head !== null) { |
|
|
|
|
75
|
|
|
$this->shift(); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Binds to count() method. This is equal to make $this->tree->size(). |
81
|
|
|
* |
82
|
|
|
* @return integer the tree size. 0 if it is empty. |
83
|
|
|
*/ |
84
|
|
|
public function count() { |
85
|
|
|
return $this->size; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the array size. |
90
|
|
|
* |
91
|
|
|
* @return int the length |
92
|
|
|
*/ |
93
|
|
|
public function size() : int { |
94
|
|
|
return $this->size; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Checks if the list is empty. |
99
|
|
|
* |
100
|
|
|
* @return boolean true if is empty, else false. |
101
|
|
|
*/ |
102
|
|
|
public function empty() : bool { |
103
|
|
|
return $this->size === 0; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Adds at the end of the list new node containing |
108
|
|
|
* the data to be stored. |
109
|
|
|
* |
110
|
|
|
* @param mixed $data The data |
111
|
|
|
*/ |
112
|
|
|
public function push($data) { |
113
|
|
|
$this->insert($this->size, $data); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Adds at the beginning a node in the list. |
118
|
|
|
* |
119
|
|
|
* @param mixed $data |
120
|
|
|
* @return mixed the data stored. |
121
|
|
|
*/ |
122
|
|
|
public function unshift($data) { |
123
|
|
|
$this->insert(0, $data); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Deletes the first node of the list and returns it. |
128
|
|
|
* |
129
|
|
|
* @return mixed the data. |
130
|
|
|
*/ |
131
|
|
|
public function shift() { |
132
|
|
|
return $this->delete(0); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Removes and returns the last node in the list. |
137
|
|
|
* |
138
|
|
|
* @return mixed data in node. |
139
|
|
|
*/ |
140
|
|
|
public function pop() { |
141
|
|
|
return $this->delete($this->size - 1); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Delete a node in the given position and returns it back. |
146
|
|
|
* |
147
|
|
|
* @param integer $index the position. |
148
|
|
|
* @throws OutOfBoundsException if index is negative |
149
|
|
|
* or is greater than the size of the list. |
150
|
|
|
*/ |
151
|
|
|
public function delete($index) { |
152
|
|
|
if($index < 0 || ($index > 0 && $index > $this->size - 1)) { |
153
|
|
|
throw new OutOfBoundsException(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// if the list is empty |
157
|
|
|
if($this->empty()) { |
158
|
|
|
return null; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$nodeData = null; |
|
|
|
|
162
|
|
|
if($index === 0) { |
163
|
|
|
$nodeData = $this->deleteBeginning(); |
164
|
|
|
} else if($index === $this->size - 1) { |
165
|
|
|
$nodeData = $this->deleteEnd(); |
166
|
|
|
} else { |
167
|
|
|
$nodeData = $this->deleteAt($index); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$this->size--; |
171
|
|
|
return $nodeData; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Deletes at the beginnig of the list and returns the data stored. |
176
|
|
|
* |
177
|
|
|
* @return mixed the data stored in the node. |
178
|
|
|
*/ |
179
|
|
|
protected abstract function deleteBeginning(); |
|
|
|
|
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Deletes at the specified position and returns the data stored. |
183
|
|
|
* |
184
|
|
|
* @param integer $index the position. |
185
|
|
|
* @return mixed the data stored in the node. |
186
|
|
|
*/ |
187
|
|
|
protected abstract function deleteAt($index); |
|
|
|
|
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Deletes at the end of the list and returns the data stored. |
191
|
|
|
* |
192
|
|
|
* @return mixed the data stored in the node. |
193
|
|
|
*/ |
194
|
|
|
protected abstract function deleteEnd(); |
|
|
|
|
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Converts/exports the list content into array type. |
198
|
|
|
* |
199
|
|
|
* @return array data stored in all nodes. |
200
|
|
|
*/ |
201
|
|
|
public function toArray() : array { |
202
|
|
|
$arr = []; |
203
|
|
|
foreach($this->getAll() as $node) { |
204
|
|
|
$arr[] = $node; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $arr; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Gets the data stored in the position especified. |
212
|
|
|
* |
213
|
|
|
* @param integer $index Index that must be greater than 0 |
214
|
|
|
* and lower than the list size. |
215
|
|
|
* @return mixed The data stored in the given index |
216
|
|
|
* @throws OutOfBoundsException if index is out bounds. |
217
|
|
|
*/ |
218
|
|
|
public function get($index) { |
219
|
|
|
$node = $this->search($index); |
220
|
|
|
|
221
|
|
|
return $node->data; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* {@inheritDoc} |
226
|
|
|
*/ |
227
|
|
|
public function getLast() { |
228
|
|
|
$lastNode = $this->searchLast(); |
|
|
|
|
229
|
|
|
return $lastNode === null ? null : $lastNode->data; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Gets the node stored in the position especified. |
234
|
|
|
* If index is 0 or (size - 1) the method is O(1) else O(n). |
235
|
|
|
* |
236
|
|
|
* @param integer $index the position. |
237
|
|
|
* @throws OutOfBoundsException if it is out of limits (< 0 or > size - 1) |
238
|
|
|
* @return DataStructures\Lists\Nodes\SimpleLinkedListNode|null the node stored in $index. |
239
|
|
|
*/ |
240
|
|
|
protected abstract function search($index); |
|
|
|
|
241
|
|
|
} |