Does this work?
Code:
#include <stdio.h>
#include <stdbool.h>
struct Node
{
int data;
struct Node *next;
};
typedef struct Node Node_t;
int count_distinct(Node_t * start)
{
int counter = 0;
for ( Node_t *ptr1 = start ; ptr1 != NULL ; ptr1 = ptr1->next ) {
printf("Outer loop ptr=%p, looking for %d\n", ptr1, ptr1->data );
Node_t *seen_before = start;
for ( seen_before = start ; seen_before != ptr1 ; seen_before = seen_before->next ) {
if ( seen_before->data == ptr1->data ) {
printf(" Found previous at %p\n", seen_before );
break;
}
}
if ( seen_before == ptr1 ) {
printf(" First time seeing %d\n", ptr1->data );
counter++;
}
}
return counter;
}
int main ( ) {
Node_t list[] = {
{1,&list[1]},
{2,&list[2]},
{1,&list[3]},
{1,&list[4]},
{3,&list[5]},
{4,NULL},
};
printf("%d\n",count_distinct(list));
return 0;
}