1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DataStructures\Lists; |
4
|
|
|
|
5
|
|
|
use DataStructures\Lists\Interfaces\ListInterface; |
6
|
|
|
use OutOfBoundsException; |
7
|
|
|
|
8
|
|
|
abstract class ListAbstract implements ListInterface { |
9
|
|
|
protected $size; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Insert a node in the specified list position. |
13
|
|
|
* |
14
|
|
|
* @param integer $index position |
15
|
|
|
* @param mixed $data data to be saved |
16
|
|
|
*/ |
17
|
|
|
public function insert($index, $data) { |
18
|
|
|
if($index < 0) { |
19
|
|
|
throw new OutOfBoundsException(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
if($index === 0) { |
23
|
|
|
$this->insertBeginning($data); |
24
|
|
|
} else if($index >= $this->size) { |
25
|
|
|
$this->insertEnd($data); |
26
|
|
|
} else if($index > 0 && $index < $this->size) { |
27
|
|
|
$this->insertAt($index, $data); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$this->size++; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Add a new node in the specified index. |
35
|
|
|
* |
36
|
|
|
* @param integer $index the position. |
37
|
|
|
* @param mixed $data the data to be stored. |
38
|
|
|
*/ |
39
|
|
|
protected abstract function insertAt($index, $data); |
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Add a new node in the specified index. |
43
|
|
|
* |
44
|
|
|
* @param mixed $data the data to be stored. |
45
|
|
|
*/ |
46
|
|
|
protected abstract function insertEnd($data); |
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Inserts at the beginning of the list. |
50
|
|
|
* |
51
|
|
|
* @param mixed $data |
52
|
|
|
*/ |
53
|
|
|
protected abstract function insertBeginning($data); |
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Removes all nodes of the list. It removes from the beginning. |
57
|
|
|
*/ |
58
|
|
|
public function clear() { |
59
|
|
|
while($this->head !== null) { |
|
|
|
|
60
|
|
|
$this->shift(); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Binds to count() method. This is equal to make $this->tree->size(). |
66
|
|
|
* |
67
|
|
|
* @return integer the tree size. 0 if it is empty. |
68
|
|
|
*/ |
69
|
|
|
public function count() { |
70
|
|
|
return $this->size; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns the array size. |
75
|
|
|
* |
76
|
|
|
* @return int the length |
77
|
|
|
*/ |
78
|
|
|
public function size() : int { |
79
|
|
|
return $this->size; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Checks if the list is empty. |
84
|
|
|
* |
85
|
|
|
* @return boolean true if is empty, else false. |
86
|
|
|
*/ |
87
|
|
|
public function empty() : bool { |
88
|
|
|
return $this->size === 0; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Adds at the end of the list new node containing |
93
|
|
|
* the data to be stored. |
94
|
|
|
* |
95
|
|
|
* @param mixed $data The data |
96
|
|
|
*/ |
97
|
|
|
public function push($data) { |
98
|
|
|
$this->insert($this->size, $data); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Adds at the beginning a node in the list. |
103
|
|
|
* |
104
|
|
|
* @param mixed $data |
105
|
|
|
* @return mixed the data stored. |
106
|
|
|
*/ |
107
|
|
|
public function unshift($data) { |
108
|
|
|
$this->insert(0, $data); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Deletes the first node of the list and returns it. |
113
|
|
|
* |
114
|
|
|
* @return mixed the data. |
115
|
|
|
*/ |
116
|
|
|
public function shift() { |
117
|
|
|
return $this->delete(0); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Removes and returns the last node in the list. |
122
|
|
|
* |
123
|
|
|
* @return mixed data in node. |
124
|
|
|
*/ |
125
|
|
|
public function pop() { |
126
|
|
|
return $this->delete($this->size - 1); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Delete a node in the given position and returns it back. |
131
|
|
|
* |
132
|
|
|
* @param integer $index the position. |
133
|
|
|
* @throws OutOfBoundsException if index is negative |
134
|
|
|
* or is greater than the size of the list. |
135
|
|
|
*/ |
136
|
|
|
public function delete($index) { |
137
|
|
|
if($index < 0 || ($index > 0 && $index > $this->size - 1)) { |
138
|
|
|
throw new OutOfBoundsException(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// if the list is empty |
142
|
|
|
if($this->empty()) { |
143
|
|
|
return null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$nodeData = null; |
|
|
|
|
147
|
|
|
if($index === 0) { |
148
|
|
|
$nodeData = $this->deleteBeginning(); |
149
|
|
|
} else if($index === $this->size - 1) { |
150
|
|
|
$nodeData = $this->deleteEnd(); |
151
|
|
|
} else { |
152
|
|
|
$nodeData = $this->deleteAt($index); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$this->size--; |
156
|
|
|
return $nodeData; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Deletes at the beginnig of the list and returns the data stored. |
161
|
|
|
* |
162
|
|
|
* @return mixed the data stored in the node. |
163
|
|
|
*/ |
164
|
|
|
protected abstract function deleteBeginning(); |
|
|
|
|
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Deletes at the specified position and returns the data stored. |
168
|
|
|
* |
169
|
|
|
* @param integer $index the position. |
170
|
|
|
* @return mixed the data stored in the node. |
171
|
|
|
*/ |
172
|
|
|
protected abstract function deleteAt($index); |
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Deletes at the end of the list and returns the data stored. |
176
|
|
|
* |
177
|
|
|
* @return mixed the data stored in the node. |
178
|
|
|
*/ |
179
|
|
|
protected abstract function deleteEnd(); |
|
|
|
|
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Converts/exports the list content into array type. |
183
|
|
|
* |
184
|
|
|
* @return array data stored in all nodes. |
185
|
|
|
*/ |
186
|
|
|
public function toArray() : array { |
187
|
|
|
$arr = []; |
188
|
|
|
foreach($this->getAll() as $node) { |
189
|
|
|
$arr[] = $node; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $arr; |
193
|
|
|
} |
194
|
|
|
} |