deleting in link list

#include 'stdio.h'
#include 'iostream.h'
#include 'conio.h"
struct node
{
int data;
node *next;
};
class linklist
{
node *list,*nptr,*tptr;
public:
linklist()
{
list=NULL;
}
void newnode(int item);
void link();
void showdata();
void deletion(int item);
};
void linklist::newnode(int item)
{
nptr =new(node);
nptr->data=item;
nptr->next=NULL;
//link();
}
void linklist::link()
{
if(list==NULL)
{
list=nptr;
tptr=nptr;
}
else
{
tptr->next=nptr;
tptr=nptr;
}
}

void linklist::showdata()
{
node *curptr;
curptr=list;
while(curptr !=NULL)
{
cout<<" "<data;
curptr=curptr->next;
}
//cout<<" "<data;
}
void linklist::deletion(int item)
{
node *pptr;
tptr=list;
if(list->data!=item)
{
while(tptr->data!=item)
{
if(tptr==NULL)
{
cout<<"\nItem is not found in the list\n";
break;
}
pptr=tptr;
tptr=tptr->next;
}
pptr->next=tptr->next;
delete(tptr);
}
else
{
list =list->next;
delete(tptr);
}

}

int main()
{
int n,d;
linklist mylist;
cout<<"\n How many nodes you have ?\t";
cin>>n;
cout<<"\n Enter data for nodes :";
for(int i=0;i {
cin>>d;
mylist.newnode(d);
mylist.link();
}
cout<<"data in the list :";
mylist.showdata();

char ans;
cout<<"\nDo you want to delete a node ?(y for yes )\t";
cin>>ans;
if(ans=='y'|| ans=='Y')
{
int x;
cout<<"\n\n Enter the node value to be deleted: ";
cin>>x;
mylist.deletion(x);
cout< cout<<"\n\nUpdated Data in the list: \t";
mylist.showdata();
cout<<"\n";
}



return 0;
}

No comments:

Post a Comment

There was an error in this gadget

Followers

About Me

Dhaka, Dhaka, Bangladesh
..