File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change 23
23
// Insert a new book as the first element of the list.
24
24
$ bookTitles ->insertAtFirst ("Introducing PHP to noobs " );
25
25
26
- $ bookTitles ->display ();
26
+ $ bookTitles ->display ();
27
+
28
+ // Search for a existing title, and a non-existing title.
29
+ var_dump ($ bookTitles ->search ("Introducing PHP to noobs " )->data );
30
+ var_dump ($ bookTitles ->search ("Clever stuff " ));
Original file line number Diff line number Diff line change @@ -67,4 +67,24 @@ public function insertAtFirst(string $data = NULL)
67
67
return true ;
68
68
}
69
69
70
+ /**
71
+ * Search for a node in the linked list.
72
+ * We need to check if there are any nodes to begin with, and iterate through the list until found.
73
+ * @param string|null $data
74
+ * @return bool|ListNode
75
+ */
76
+ public function search (string $ data = NULL )
77
+ {
78
+ if ($ this ->_totalNodes ) {
79
+ $ currentNode = $ this ->_firstNode ;
80
+ while ($ currentNode !== NULL ) {
81
+ if ($ currentNode ->data === $ data ){
82
+ return $ currentNode ;
83
+ }
84
+ $ currentNode = $ currentNode ->next ;
85
+ }
86
+ }
87
+ return false ;
88
+ }
89
+
70
90
}
You can’t perform that action at this time.
0 commit comments