Skip to content

Commit f7b5899

Browse files
committed
Added LList access methods (#29)
1 parent 3b351fd commit f7b5899

File tree

1 file changed

+43
-0
lines changed
  • LinkedList-dataStructure/LL-implementation

1 file changed

+43
-0
lines changed

LinkedList-dataStructure/LL-implementation/main.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,43 @@ class LinkedList
191191
}
192192
cout << endl;
193193
}
194+
195+
int get(int index)
196+
{
197+
if (index < 0 || index >= this->totalNodes)
198+
{
199+
cout << "[WARNING] Index out of bounds!" << endl;
200+
return -1;
201+
}
202+
203+
Node *curr = this->head;
204+
for (int i = 0; i < index; i++)
205+
{
206+
curr = curr->link;
207+
}
208+
209+
return curr->data;
210+
}
211+
212+
int find(int data)
213+
{
214+
Node *curr = this->head;
215+
for (int i = 0; i < this->totalNodes; i++)
216+
{
217+
if (curr->data == data)
218+
{
219+
return i;
220+
} else if(curr->link == NULL)
221+
{
222+
break;
223+
}
224+
225+
curr = curr->link;
226+
}
227+
228+
cout << "Could not find the node" << endl;
229+
return -1;
230+
}
194231
};
195232

196233
int main()
@@ -221,5 +258,11 @@ int main()
221258

222259
l.display();
223260

261+
cout << "Get Node: 2" << endl;
262+
cout << l.get(2) << endl;
263+
264+
cout << "Find Node 8:" << endl;
265+
cout << l.find(8) << endl;
266+
224267
return 0;
225268
}

0 commit comments

Comments
 (0)