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
  [B]remove duplicates[/B]
  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:

curr = head;
  while(curr)
    {
      strcpy(check,curr->word);
      curr = curr->next;
      while(curr)
        {
          if(strcmp(check,curr->word)==0)
            printf("duplicate"); {here should come the remove node function}
          curr = curr->next;
        }
    }

But something is wrong. It compiles without errors, but doesn't say anything about duplicates, even though I know there are. Thanks in advance!

you need to keep two different pointers for curr because you have two different loops. Try this:

curr1 = head;
  while(curr1)
    {
      strcpy(check,curr1->word);
      curr2 = curr1->next;
      while(curr2)
        {
          if(strcmp(check,curr2->word)==0)
            printf("duplicate"); {here should come the remove node function}
          curr2 = curr2->next;
        }
        cur1 = cur1->next;
    }

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.

commented: I believe you are a little too late -3
itemWord* findDup(itemWord* head, itemWord* item)
{
    for (itemWord* curr = head; curr != 0; curr = curr->next)
    {
        if (curr != item && strcmp(item->word, curr->word) == 0)
        {
            return curr;
        }
    }
    return 0;
}
void removeDup(itemWord** head, itemWord* item)
{
    itemWord* prev = 0;
    for (itemWord* curr = *head; curr != 0; curr = curr->next)
    {
        if (item == curr)
        {
            if (prev == 0)
            {
                head = &(curr->next);
            }
            else
            {
                prev->next = curr->next;
            }
            free(curr);
            return;
        }
        prev = curr;
    }
}
int main(void)
{
    itemWord *curr = 0, *head = 0;
    int done = 0;
    .
    .
    .
    /* This will get rid of ALL duplicates, even if there are muliples.
     * An optimization may be to avoid rescanning the list. That said,
     * I haven't run this code, so it may need some tweaking! :-)
     */
    while (!done)
    {
        int gotone = 0;
        for (curr = head; curr != 0; curr = curr->next)
        {
            itemWord* found = findDup(head, curr);
            if (found)
            {
                removeDup(&head, curr);
                gotone = 1;
                break;
            }
        }
        done = !gotone
    }
    return 0;
}
commented: Doesn't anyone look at posting dates anymore???? It's a freaking 4-year old thread! -3

I see I didn't look at the date - I must have hit the "end" option instead of "next" on the main page... Oh well, stuff sometimes happens!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.