# Thread: deleting nodes in a linear list

1. ## 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

2. > BUT how should i proceed if there are multiple occurences of the same data element
Keep going until you reach the end of the list, rather than bailing out when you find (and delete) the first node which matches.

3. ## 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";
}```

4. So remove the return statement from within if(temp->data==num)

5. ## doesn't help

on removing return ,it prints "not found " everytime , and still deletes only the first occurence.Please check the looping statements....I will be GRATEFUL.( I AM ENCLOSING FULL CODE IF U HAVE TIME PLEASE TRY THIS AND KINDLY GIVE UR SUGGESTIONS)
Code:
```#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<alloc.h>

struct node
{
int data;
struct node *next;
};
void append(struct node **);
void display(struct node *);
int count(struct node*);
void addatbeg(struct node**);
void remove(struct node**);
void main()
{       clrscr();
char ch,c;
struct node *p;
p=NULL;
c='1';
while(c=='1'||c=='1')  \
{
cout<<"enter ur choice\n1: add\n2: display\n3: count\n"
"4: add at beginning\n5: remove\n any other no. to exit\n";
cin>>ch;

switch(ch)
{
case '1':   append(&p);
break;

case '2':   display(p);
break;
case '3':   count(p);
break;
case '4':   addatbeg(&p);
break;
case '5':   remove(&p);
break;
default:    exit(0);
}
cout<<"\n\n\nwould u like to contnue(1/2)";
cin>>c;

}
getche();
}
void append(struct node **q)
{       int num,n,i;
struct node *temp,*r;
cout<<"how many no do u want to add at a stretch";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"enter the no.";
cin>>num;

if(*q==NULL)
{
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
temp->next=NULL;
*q=temp;
} //if
else
{      temp=*q;
while(temp->next!=NULL)
temp=temp->next;
r=(struct node *)malloc(sizeof(struct node));
r->data=num;
r->next=NULL;
temp->next=r;

}//else

}

}//append

void display(struct node *q)
{
while(q!=NULL)
{
cout<<"  "<<q->data;
q=q->next;
}
}
int count(struct node *q)
{
int c=0;
while(q!=NULL)
{
c++;
q=q->next;
}
cout<<"the no of numbers entered are  "<<c;
return 0;
}
void addatbeg(struct node **q)
{       int num,i,n;
cout<<"\nenter the no. u want to add at a stretch\n";
cin>>n;
for(i=1;i<=n;i++)
{

cout<<"enter the no.";
cin>>num;
struct node *temp;
temp=*q;
if(*q==NULL)
{        temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
temp->next=NULL;
*q=temp;
}
else
{	 temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
temp->next=*q;
*q=temp;
}
}

}

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);

}
else
{
old=temp;
temp=temp->next;
}

}
cout<<"\n"<<num<<" not found\n";
}```

6. main() is int main().

You should use new instead of malloc() in C++.

Maybe this loop
Code:
`while(temp->next!=NULL)`
should be
Code:
`while(temp!=NULL)`
Code:
`#include<alloc.h>`
What compiler are you using?

7. > on removing return ,it prints "not found " everytime
Because your "logic" is now broken.
Of course it prints this, because you always now get to the end of the list.
What you also need is a flag to tell you whether you deleted any items at all.
If this flag remains unset, then (and only then) should you print "not found".

> I AM ENCLOSING FULL CODE
We don't appreciate YELLING on message boards.

8. ## thanks,......

and sorry for yelling.

Popular pages Recent additions