Simple list traversal in C++
- by anni15
I have a code for simple list traversal. Could you please help me out to do this traversal more efficiently!
mylist::traverse()
{
// this code traverses a list by index order of the elements
(for int i=1; i<= thelist.size(); i++ ) /* find index numbers */
{
int count=0; /* counter to find index */
Node * cursor;
for ( cursor = head_ptr; cursor !=NULL ||count<i; cursor=cursor->link() )
{
count++;
}
if ( cursor != NULL )
cout << “index: “ << i << “ value: “ << cursor->data() << endl;
}
}