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

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -