c - Remove duplicate elements from a sorted linked list -


i attempting c program remove duplicates sorted linked list , using simple concept of traversing list start node. while traversing, compare each node next node. if data of next node same current node delete next node.

my code is:

struct node *remove_dup(struct node *start) {     struct node *p,*tmp;     p=start;     while(p!=null)     {         if(p->info==p->link->info)         {           tmp=p->link;             p->link=p->link->link;           free(tmp);         }         p=p->link;     }     return start; } 

it not giving me correct answer! wrong execution? concept wrong?

since code examines next element, need stop when @ element 1 before last, this:

while (p != null && p->link != null) {     ... } 

the reason have first part of condition trap empty lists.

in addition, should not advance pointer when remove element. otherwise, not process runs of more 2 elements correctly.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -