Skip to content

Commit d5eb51d

Browse files
authored
Add comments to get and find methods
1 parent f7b5899 commit d5eb51d

File tree

1 file changed

+7
-1
lines changed
  • LinkedList-dataStructure/LL-implementation

1 file changed

+7
-1
lines changed

LinkedList-dataStructure/LL-implementation/main.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,14 @@ class LinkedList
194194

195195
int get(int index)
196196
{
197+
// Check whether node with this index exists
197198
if (index < 0 || index >= this->totalNodes)
198199
{
199200
cout << "[WARNING] Index out of bounds!" << endl;
200201
return -1;
201202
}
202203

204+
// Iterate over nodes until we reach the index-th node
203205
Node *curr = this->head;
204206
for (int i = 0; i < index; i++)
205207
{
@@ -211,9 +213,12 @@ class LinkedList
211213

212214
int find(int data)
213215
{
216+
// Iterate through all nodes
217+
// In the worst case we don't find the node and reach the end of the list
214218
Node *curr = this->head;
215219
for (int i = 0; i < this->totalNodes; i++)
216220
{
221+
// Check whether we found the data, if so return the index at which it lies
217222
if (curr->data == data)
218223
{
219224
return i;
@@ -224,7 +229,8 @@ class LinkedList
224229

225230
curr = curr->link;
226231
}
227-
232+
233+
// This means the loop terminated without finding the data
228234
cout << "Could not find the node" << endl;
229235
return -1;
230236
}

0 commit comments

Comments
 (0)