File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change 46
46
echo SEPERATOR ;
47
47
48
48
// Delete the first entry in the list
49
- $ bookTitles ->deleteFirst ();
49
+ var_dump ($ bookTitles ->deleteFirst ());
50
+
51
+ $ bookTitles ->display ();
52
+ echo SEPERATOR ;
53
+
54
+ // Delete the last entry in the list
55
+ var_dump ($ bookTitles ->deleteLast ());
50
56
51
57
$ bookTitles ->display ();
52
58
echo SEPERATOR ;
Original file line number Diff line number Diff line change @@ -162,4 +162,30 @@ public function deleteFirst() {
162
162
return false ;
163
163
}
164
164
165
+ /**
166
+ * Deletes the last node of the nodelist, if applicable.
167
+ * We need to make sure the next property of the second last is set to NULL.
168
+ * @return bool
169
+ */
170
+ public function deleteLast ()
171
+ {
172
+ if ($ this ->_firstNode !== NULL ){
173
+ $ currentNode = $ this ->_firstNode ;
174
+ if ($ currentNode ->next === NULL ){
175
+ $ this ->_firstNode = NULL ;
176
+ } else {
177
+ $ previousNode = NULL ;
178
+
179
+ while ($ currentNode ->next !== NULL ) {
180
+ $ previousNode = $ currentNode ;
181
+ $ currentNode = $ currentNode ->next ;
182
+ }
183
+ $ previousNode ->next = NULL ;
184
+ $ this ->_totalNodes --;
185
+ return true ;
186
+ }
187
+ }
188
+ return false ;
189
+ }
190
+
165
191
}
You can’t perform that action at this time.
0 commit comments