#include
#include
struct node
{
int data;
node *next;
};
class linklist
{
node *list,*nptr,*tptr;
public:linklist()
{
list=NULL;
}
void newnode(int x);
void link();
void showdata();
void delet();
};
void linklist::newnode(int x)
{
nptr=new node;
nptr->data=x;
nptr->next=NULL;
}
void linklist::link()
{
if(list==NULL)
{
list=nptr;
tptr=nptr;
}
else
{
tptr->next=nptr;
tptr=nptr;
}
}
void linklist::delet()
{
int a;
node *lptr;
printf("\nDelete Number:");
scanf("%d",&a);
tptr=list;
while(tptr->data!=a)
{
lptr=tptr;
tptr=tptr->next;
}
if(tptr->next==NULL)
{
delete(tptr);
lptr->next=NULL;
}
else
{
lptr->next=tptr->next;
delete(tptr);
}
}
void linklist::showdata()
{
node *curptr;
curptr=list;
while(curptr!=NULL)
{
printf("%3d",curptr->data);
curptr=curptr->next;
}
}
int main()
{
clrscr();
int n,d,a;
linklist mylist;
printf("\nhow many node you have?");
scanf("%d",&n);
printf("\nEnter deta:");
for(int i=0;i
{
printf("\nElement[%d]=",i+1);
scanf("%d",&d);
mylist.newnode(d);
mylist.link();
}
printf("\npress 1 for showdata:");
printf("\nPress 2 for delete:");
printf("\nEntar option:");
scanf("%d",&a);
switch(a)
{
case 1:printf("Data in list:");
mylist.showdata();
break;
case 2:mylist.delet();
mylist.showdata();
break;
}
getch();
return(0);
}
0 comments:
Post a Comment