943,954 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6978
  • C RSS
Apr 23rd, 2008
0

Search and delete duplicates in linked list

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
simps0n is offline Offline
7 posts
since Dec 2007
Apr 23rd, 2008
0

Re: Search and delete duplicates in linked list

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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jul 10th, 2009
-1

Re: Search and delete duplicates in linked list

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.
Reputation Points: 7
Solved Threads: 0
Newbie Poster
masalacurry is offline Offline
1 posts
since Jul 2009
Jul 11th, 2009
0

Re: Search and delete duplicates in linked list

> 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!"
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help with my row counter
Next Thread in C Forum Timeline: Vi and ctags





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC