Arrange 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 sort(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::sort(int item)
{
node *pptr,*fptr;
pptr=list;
while(pptr !=NULL)
{
fptr=pptr->next;
while(fptr !=NULL)
{
if(pptr->data>fptr->data)
{
int temp;
temp=pptr->data;
pptr->data=fptr->data;
fptr->data=temp;
}
fptr=fptr->next;
}
pptr=pptr->next;

}
}

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();
//mylist.sort();
//char ans;
/* cout<<"\nDo you want to sort the list ?(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.sort(d);

cout<<"\n\nsort Data in the list: \t";
mylist.showdata();
cout<<"\n";




return 0;
}

No comments:

Post a Comment

Followers

About Me

Dhaka, Dhaka, Bangladesh
..