Search and delete duplicates in linked list

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2007
Posts: 7
Reputation: simps0n is an unknown quantity at this point 
Solved Threads: 0
simps0n simps0n is offline Offline
Newbie Poster

Search and delete duplicates in linked list

 
0
  #1
Apr 23rd, 2008
Hello, guys!

I have the following code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct list_word
{
  char word[64];
  struct list_word * next;
};

typedef struct list_word itemWord;

int main()
{
  itemWord * curr, * head;

  fill list with words
  remove duplicates
  show final list of words

  return(0);
}
So I experience difficulties when implementing the removing duplicates part. What I tried to do is: get (each) word from list, strcmpit to next words, remove if duplicate found. Here is my solution:
  1. curr = head;
  2. while(curr)
  3. {
  4. strcpy(check,curr->word);
  5. curr = curr->next;
  6. while(curr)
  7. {
  8. if(strcmp(check,curr->word)==0)
  9. printf("duplicate"); {here should come the remove node function}
  10. curr = curr->next;
  11. }
  12. }
But something is wrong. It compiles without errors, but doesn't say anything about duplicates, even though I know there are. Thanks in advance!
Last edited by simps0n; Apr 23rd, 2008 at 8:21 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,442
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1474
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Search and delete duplicates in linked list

 
0
  #2
Apr 23rd, 2008
you need to keep two different pointers for curr because you have two different loops. Try this:
  1. curr1 = head;
  2. while(curr1)
  3. {
  4. strcpy(check,curr1->word);
  5. curr2 = curr1->next;
  6. while(curr2)
  7. {
  8. if(strcmp(check,curr2->word)==0)
  9. printf("duplicate"); {here should come the remove node function}
  10. curr2 = curr2->next;
  11. }
  12. cur1 = cur1->next;
  13. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 1
Reputation: masalacurry is an unknown quantity at this point 
Solved Threads: 0
masalacurry masalacurry is offline Offline
Newbie Poster

Re: Search and delete duplicates in linked list

 
-1
  #3
Jul 10th, 2009
Old Thread, but what the heck..

I believe the Remove() function will need a pointer to the node preceding your target.
Kindly design the loops keeping that in mind.

Regards.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Search and delete duplicates in linked list

 
0
  #4
Jul 11th, 2009
> Old Thread, but what the heck..
Like reading the rules, but what the heck heh?
http://www.daniweb.com/forums/thread78060.html
"Don't resurrect old threads!"
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC