-
List traversal employs a linear search algorithm:
// Node.h
class Node {
Node* pnext;
public:
int data;
// Measure distance to other node:
int distance( Node const* other ) const
{
int hops = 1;
Node const* iter = pnext;
while ( iter != other ) {
if ( iter == NULL )
return hops - 1;
iter = iter->pnext;
++hops;
}
return hops;
}
// Calculate number of nodes in the list:
size_t size() const
{
return distance( NULL );
}
};
|
|