ok..
I dont get segmentation fault now...
but nothing is printed either
any suggestion why???
ok..
I dont get segmentation fault now...
but nothing is printed either
any suggestion why???
Last edited by satty; 07-28-2010 at 07:33 AM.
Somethings you could do, first specify a return type to the function, void if you don't want to return anything.Code:removeDuplicates(struct node* head) { struct node* current = head; struct node* next_next; while(current->next != NULL) if (strcmp (current->data, current->next->data) ==0) { next_next = current->next->next; free(current->next); current->next = next_next; } { current = current->next; } } }
Also, your while isn't working that well, you're not advancing the pointers. The while only works on the if statement.
You could try using two pointers, one that points to the current, and the other to the previous, its easier to work with, and you can't forget to move the pointers forward in the list.
Thanks, I had made the changes u suggested but I dont know if M passing the strings correctly.
1. I read the text file.
2. Found the number of strings present
3. printed the file
4. tried [assing it to the function to remove duplicates
Code:fp = fopen ("Names.txt","r"); while(feof(fp)==0) { fgets(names,10,fp) ; count++; // printf("%s\n",names); } printf("%d\n", count-1); struct node* head ; while(!EOF) { add(&head,*names); }
can u please go through this code!
First test the variable 'fp' for NULL, just in case it didn't open.
Then, you shouldn't use the feof() function to test if you reach the end of file.Code:if (fp == NULL) { //do something }
Try something like,
Code:while(fgets(names , sizeof(names) , fp) != NULL) { count++; //add names to linked list }
Thanks a lot.
I did the way u mentioned.
It complies without error but doesnt give the result.I mean i get something like this
List after removal of the duplicates = NULL.
any suggestion here!??Code:struct node* head ; head = calloc(count,sizeof (char)); if(fp ==NULL) { printf("Error"); } else{ while(fgets(names,10,fp) != NULL) { count++; printf("%s",names); add(head,*names); } } printf("\n%d\n", count);
when you send the string 'names', you want to send the pointer to the first element, like: add(head , names);
You could also have some problem with the head pointer. I don't know how you wrote the add() function, can you show it?