Count distinct elements in an list in C
Quote:
I want to count all the distinct numbers of a list.For example if my list consist of {1,2,1,1,3,4} i need to return 4.Here is the code i have written.Something is missing but i don't know what.
Code:
struct Node
{
int data;
struct Node *next;
};
typedef struct Node Node_t;
int count_distinct(Node_t * start)
{
Node_t *ptr1, *ptr2;
ptr1 = start;
int counter=0;
/* pick one element*/
while (ptr1 != NULL && ptr1->next != NULL)
{
ptr2 = ptr1;
/* compare picked element with the rest */
while (ptr2->next != NULL)
{
/* If duplicate */
if (ptr1->data == ptr2->next->data)
{
break;
}
else /*if unique element must counter++*/
//do what???
ptr2 = ptr2->next;
counter++;
}
ptr1 = ptr1->next;
}
return counter;
}