deleting nodes in a linear list
well, i was able to write the code for deleting a node in a linear list when the data contained in it was specified....BUT how should i proceed if there are multiple occurences of the same data element,and i wish to delete all those occurences in one go....AND please guide me for deleting the even/odd occurences of that data element
well that's what i also thought but could'nt write it correctly
this is what i was able to write ,the problem is that it deletes only the first occurence of the desired number.so please help me out..... and kindly rectify the errors.
Code:
void remove(struct node **q)
{
struct node *temp,*old;
int n,num;
cout<<"\nenter the no. u want to remove\n";
cin>>num;
temp=*q;
if(*q==NULL)
{
cout<<"empty list,can't remove";
}
while(temp->next!=NULL)
{
if(temp->data==num)
{
if(temp==*q) //first node
{
(*q)=temp->next;
}
else
{
old->next=temp->next;
}
free(temp);
return;
}
else
{
old=temp;
temp=temp->next;
}
}
cout<<"\n"<<num<<" not found\n";
}