1 | <?php |
||
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) { |
||
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() { |
||
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() { |
||
87 | |||
88 | /** |
||
89 | * Returns the array size. |
||
90 | * |
||
91 | * @return int the length |
||
92 | */ |
||
93 | public function size() : int { |
||
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 { |
||
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) { |
||
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) { |
||
125 | |||
126 | /** |
||
127 | * Deletes the first node of the list and returns it. |
||
128 | * |
||
129 | * @return mixed the data. |
||
130 | */ |
||
131 | public function shift() { |
||
134 | |||
135 | /** |
||
136 | * Removes and returns the last node in the list. |
||
137 | * |
||
138 | * @return mixed data in node. |
||
139 | */ |
||
140 | public function pop() { |
||
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) { |
||
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 { |
||
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) { |
||
226 | |||
227 | /** |
||
228 | * {@inheritDoc} |
||
229 | */ |
||
230 | public function getLast() { |
||
234 | |||
235 | /** |
||
236 | * Gets the node stored in the position especified. |
||
237 | * If index is 0 or (size - 1) the method is O(1) else O(n). |
||
238 | * |
||
239 | * @param integer $index the position. |
||
240 | * @throws OutOfBoundsException if it is out of limits (< 0 or > size - 1) |
||
241 | * @return DataStructures\Lists\Nodes\SimpleLinkedListNode|null the node stored in $index. |
||
242 | */ |
||
243 | protected abstract function search($index); |
||
244 | } |