Remove duplicates in a linked list before adding - C
Posted
by DesperateCoders
on Stack Overflow
See other posts from Stack Overflow
or by DesperateCoders
Published on 2010-05-15T14:46:12Z
Indexed on
2010/05/15
14:54 UTC
Read the original article
Hit count: 120
Hi,
My question is about removing duplicates from a linked list. But i want to do it before adding to linked list.
struct myStr{int number; mystr *next;}
void append(mystr **q,int item)
{
myStr *temp;
temp = *q;
myStr *newone;
if(*q==NULL)// There should be control of previous elements. Call of keysearch function.
{ temp = (myStr *)malloc(sizeof(myStr));
temp->number =size;
temp->next=NULL;
*q=temp;
}
else //And also here
{ temp = *q;
while(temp->next !=NULL)
{ temp=temp->next;
}
newone = (myStr *)malloc(sizeof(myStr));
newone->count = size;
newone->next=NULL;
temp->next=newone;
}
}
int keysearch (myStr *p)
{
struct myStr *temp = p;
int found = 0;
int key= p->number;
while (temp->number != NULL)
{
if(temp->number == key)
{
found = 1;
}
temp = temp->next;
}
return found;
}
My problem is in keySearch. I don't know what is wrong? Or is there another way for doing this.
© Stack Overflow or respective owner