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";
}