Thread: remove duplicate words from linked list

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    84

    Unhappy remove duplicate words from linked list

    how do i locate and delete duplicate words ??
    my search function doesnt seem to work;

    Code:
    p = (Node *) malloc(sizeof(Node));
    	strcpy(p->word,word);
    	p->next = head;
    	head = p;
    search(word,p);
    Code:
    int search(char* check,Node *p) 
    {
    while( p!= NULL ) 
    {
        if(p->word == check)
        printf("Found!");
    	p = p->next;
    }
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So how often do you think that the address you are looking for is at the same address in memory as the one in the list? Because that is what you are comparing.

    Perhaps you meant to use strcmp() but forgot?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    oops!..yep thats what i meant
    if(strcmp(p->word, check)==0)
    printf(found);

    but now it returns found after i type in any word..where should i put the search function so that it only looks for duplicates and not when the word is entered the first time ???

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you need to post some more of your code?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    Code:
    p = (Node *) malloc(sizeof(Node));
    	strcpy(p->word,word);
    	p->next = head;
    	head = p;
           search(word,p);
    Last edited by rocketman03; 11-22-2008 at 08:25 PM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you insert a word in the list, do you expect to find it?

    Because that's exactly what you do:
    Code:
    	p = (Node *) malloc(sizeof(Node));
    	strcpy(p->word,word);
    	p->next = head;
    	head = p;
           search(word,p);
    The first four lines in that code inserts the string into the first position of the list, then searches the list for that word.

    Perhaps doing it the other way around would work better?

    You also shouldn't cast the return value from malloc() - if the compiler complains about that, it's probably because you are compiling your code as C++, not C (or you forgot to include stdlib.h or similar).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    well..if i put it before i ask for the word then it just crashes the program , and the same thing happens if i dont the cast the value from malloc ?..is there another way to remove duplicate words ?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, is that exactly just moving search() up to before the inserting of the word into the list? If so, I expect that it will do that - because it uses p that isn't initialized, rather than head which IS initialized to NULL, so we know that the list is empty, rather than p having some random value that is PROBABLY not a good pointer to a Node structure, and thus leads to undefined behaviour (which is usually a crash, but not guaranteed).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    oh yeaa! i know what you mean! thank you!!
    Last edited by rocketman03; 11-22-2008 at 07:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM