When using a linked list that stores numerous objects how would you go about access the data inside said object?
If you have a nodePtr
you'll have to do ptr->data
to access the int
of the struct.
My current way of doing it doesn't work and I'm not sure how to solve it.
You are trying to access member functions on an object of type int
, that does not have any. You probably meant to define another type for the data
member object of node
.
On a side note, would it be possible to search for a specific object(an object with specific data member value) in a linked list?
Of course, the implementation of such an algorithm is pretty trivial. You can implement it along the lines of:
nodePtr find(nodePtr head, int value) {
for (; head != NULL; head = head->next)
if (*head == value)
return first;
return NULL;
}
What I would suggest is to use the standard std::forward_list
or std::list
. If you do, you'll be able to use std::find
for the "find"-algorithm.