tags:

views:

88

answers:

3

explicit list( const A& Al = A( ) ); explicit list( size_type n, const T& v = T( ), const A& Al = A( ) ); list( const list& x ); list( const_iterator First, const_iterator Last, const A& Al = A( ) );

A: 

At least based on what you have here, the problem isn't with how you traverse the list -- it's with using a list at all. You're asking for random access to the data, which means you should probably be using something like a vector or a deque instead of a list.

Jerry Coffin
He implements random access in a sense, but only uses it to access elements in sequence.
Potatoswatter
A: 

I really can't tell what this is trying to do, but you have a possible segfault in your inner loop:

for ( cursor = head_ptr; cursor !=NULL ||count<i; cursor=cursor->link() )
{
count++;    
}

Your termination condition indicates that if count < i, you would keep looping even if cursor == NULL; when cursor=cursor->link() executes, you would try to dereference NULL.

Perhaps you meant cursor !=NULL && count<i ?

danben
+1  A: 
#include <list>
using namespace std;

list<Node> my_list;

int index = 0;
for ( list<Node>::iterator cursor = my_list.begin();
    it!= my_list.end(); ++ cursor, ++ index ) {
    cout << "index: " << index << “ value: “ << cursor->data() << endl;
}
Potatoswatter