C Programming
#include
#include
struct node
{
int data;
node *next;
};
class linklist
{
node *list,*nptr,*tptr,*bptr;
public:
linklist()
{
list=NULL;
}
void newnode(int item);
void link();
void display();
void add(int r);
void delet(int x);
};
void linklist::newnode(int item)
{
nptr=new node;
nptr->data=item;
nptr->next=NULL;
}
void linklist::link()
{
if(list==NULL)
{
list=nptr;
tptr=nptr;
}
else
{
tptr->next=nptr;
tptr=nptr;
}
}
void linklist::display()
{
node *curptr;
curptr=list;
while(curptr!=NULL)
{
printf("%d->",curptr->data);
curptr=curptr->next;
}
}
void linklist::add(int r)
{
nptr=new node;
nptr->data=r;
nptr->next=NULL;
tptr=list;
while(tptr->next!=NULL)
{
tptr=tptr->next;
}
tptr->next=nptr;
}
void linklist::delet(int x)
{
tptr=list;
while(tptr->data!=x)
{
bptr=tptr;
tptr=tptr->next;
}
bptr->next=tptr->next;
delete(tptr);
}
int main()
{
clrscr();
int n,d,e,g;
linklist mylist;
printf("How many node you want:");
scanf("%d",&n);
printf("Enter data for node:");
for(int i=0;i
{
printf("Element[%d]=",i+1);
scanf("%d",&d);
mylist.newnode(d);
mylist.link();
}
mylist.display();
printf("Enter the number :");
scanf("%d",&e);
mylist.add(e);
printf("The linklist is=");
mylist.display();
printf("Enter the number:");
scanf("%d",&g);
mylist.delet(g);
mylist.display();
getch();
return 0;
}
0 comments:
Post a Comment