Hello !
I am trying to remove the duplicates strings from a file...
i have read the strings from the file , it reads successfully and prints it but when I try to delete the duplicates, it doesnt work n give no errors,
I have no clue what is going wrong....
Code:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define LINE_LENGTH 10
struct lnode {
char *str;
struct lnode *next;
};
void removeDuplicates(struct lnode* list)
{
struct lnode* p = list;
struct lnode* q;
if(p == NULL)
return;
while(p->next != NULL)
{
if(p->str == p->next->str)
{
q = p->next->next;
free(p->next);
p->next = q;
}
else
{
p = p->next;
}
}
}
void *insert(char *data, struct lnode *list) {
struct lnode *p;
p = (struct lnode *)malloc(sizeof(struct lnode));
p->str = strdup(data);
p->next = list;
list = p;
return list;
}
void print_list(struct lnode *list)
{
while(list!=NULL)
{
printf("%s ", list->str);
list = list->next;
}
}
int main ()
{
struct lnode *list = NULL;
FILE *fp;
int count = 0;
char names[10];
fp = fopen ("Workbook2.txt","r");
if(fp ==NULL)
{
printf("Error");
}
else{
while(fgets(names,10,fp) != NULL)
{
count++;
list = insert(names,list);
}
removeDuplicates(list);
print_list(list);
printf("%d\n", count);
}
fclose(fp);
return 0;
}
thanks.