#include<conio.h>
#include<iostream.h>
struct node
{ int data;
node *next;
}*p,*start,*save,*loc,*locp;
int findloc(int);
int deleteitem();
void main()
int
j=0;
clrscr();
start='\0';
do
{
if(start=='\0')
{
start=new node;
p=start;
}
else
{
p->next=new node;
p=p->next;
}
cout<<"\nEnter data ";
cin>>p->data;
j++;
cout<<"\nEnter choice (y/n)?";
cin>>ch;
}while(ch=='Y'||ch=='y');
p->next='\0';
cout<<"\nData for nodes ";
p=start;
while(p!='\0')
{
cout<<endl<<p->data;
p=p->next;
}
cout<<endl<<"No. of nodes"<<j;
cout<<"\nEnter the item to be deleted ";
int i;
cin>>i;
findloc(i);
deleteitem();
cout<<"\nData for nodes after deletion ";
p=start;
while(p!='\0')
{
cout<<endl<<p->data;
p=p->next;
}
getch();
}
int findloc(int d)
{ if (start==0)
{
loc=0;
locp=0;
return(0);
}
if (start->data==d)
{
loc=start;
locp=0;
return(0);
}
save=start;
p=start->next;
while (p!=0)
{ if
(p->data==d)
{loc=p;
locp=save;
return(0);
}
save=p;
p=p->next; }
loc=0;
return(0);
}
int deleteitem()
{ if(loc==0)
{cout<<"\nNode not found ";
return(0); }
else if(locp==0)
{ start=start->next; }
else
{ locp->next=loc->next; }
delete loc;
}
No comments:
Post a Comment