File tree Expand file tree Collapse file tree 1 file changed +7
-1
lines changed
LinkedList-dataStructure/LL-implementation Expand file tree Collapse file tree 1 file changed +7
-1
lines changed Original file line number Diff line number Diff line change @@ -194,12 +194,14 @@ class LinkedList
194
194
195
195
int get (int index)
196
196
{
197
+ // Check whether node with this index exists
197
198
if (index < 0 || index >= this ->totalNodes )
198
199
{
199
200
cout << " [WARNING] Index out of bounds!" << endl;
200
201
return -1 ;
201
202
}
202
203
204
+ // Iterate over nodes until we reach the index-th node
203
205
Node *curr = this ->head ;
204
206
for (int i = 0 ; i < index; i++)
205
207
{
@@ -211,9 +213,12 @@ class LinkedList
211
213
212
214
int find (int data)
213
215
{
216
+ // Iterate through all nodes
217
+ // In the worst case we don't find the node and reach the end of the list
214
218
Node *curr = this ->head ;
215
219
for (int i = 0 ; i < this ->totalNodes ; i++)
216
220
{
221
+ // Check whether we found the data, if so return the index at which it lies
217
222
if (curr->data == data)
218
223
{
219
224
return i;
@@ -224,7 +229,8 @@ class LinkedList
224
229
225
230
curr = curr->link ;
226
231
}
227
-
232
+
233
+ // This means the loop terminated without finding the data
228
234
cout << " Could not find the node" << endl;
229
235
return -1 ;
230
236
}
You can’t perform that action at this time.
0 commit comments