Instead of having a function that removes duplicates, why not design the linked list functionality so that it will not accept duplicates..e.g. inserting a new node that results in a duplicate will do nothing.
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
Can we see what you have so far?
gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
I don't see how searching for a duplicate before insertion is problematic. Just traverse the list and if you get to the end, append a new node:
struct node *append_no_dup(struct node *head, struct node *new)
{
if (head== NULL)
head = new;
else if (compare(head, new) != 0) {
struct node *curr = head;
while (curr->next != NULL && compare(curr->next, new) != 0)
curr = curr->next;
if (curr->next == NULL)
curr->next = new;
}
return head;
}
It's basically the same idea as inserting into a sorted list.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401