| | |
Search and delete duplicates in linked list
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2007
Posts: 7
Reputation:
Solved Threads: 0
Hello, guys!
I have the following code: 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: But something is wrong. It compiles without errors, but doesn't say anything about duplicates, even though I know there are. Thanks in advance!
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);
} C Syntax (Toggle Plain Text)
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; } }
Last edited by simps0n; Apr 23rd, 2008 at 8:21 am.
you need to keep two different pointers for curr because you have two different loops. Try this:
C Syntax (Toggle Plain Text)
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; }
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.
> 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!"
Like reading the rules, but what the heck heh?
http://www.daniweb.com/forums/thread78060.html
"Don't resurrect old threads!"
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Help with my row counter
- Next Thread: Vi and ctags
| Thread Tools | Search this Thread |
#include * ansi append array arrays asterisks bash binarysearch centimeter changingto char character convert copyimagefile cprogramme creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux hacking histogram ide inches include infiniteloop initialization input interest intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation overwrite owf pdf pointer pointers posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming standard strchr string suggestions systemcall test testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






