File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
LinkedList-dataStructure/LL-implementation Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -191,6 +191,43 @@ class LinkedList
191
191
}
192
192
cout << endl;
193
193
}
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
+ }
194
231
};
195
232
196
233
int main ()
@@ -221,5 +258,11 @@ int main()
221
258
222
259
l.display ();
223
260
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
+
224
267
return 0 ;
225
268
}
You can’t perform that action at this time.
0 commit comments