Read the info of the nodes in the linked list
Find the node which carry the max. value
Find the node which carry the min. value
copy all nodes between the min and max in another list
Make the min and the max nodes the head of the list
#include<iostream>
using namespace std;
class cnode
{
public:
int info;
cnode *pnext;
};
class clist
{
public:
cnode *H,*T;
clist()
{
H=T=NULL;
}
~clist()
{
cnode *ptrav=H;
while(H !=NULL)
{
H=H->pnext;
delete ptrav;
ptrav=H;
}
}
void attach(cnode *pnn)
{
if(H==NULL)
H=T=pnn;
else
{
T->pnext=pnn;
T=pnn;
}
}
void Display()
{
cnode *ptrav;
ptrav=H;
while(ptrav !=NULL)
{
cout<<ptrav->info<<" ";
ptrav=ptrav->pnext;
}
}
};
void main()
{
clist L1,L2;
int max=-999;
int min=999;
char ch='y';
cout<<"Fill the list\n";
while(ch !='n')
{
cnode*pnn=new cnode();
cin>>pnn->info;
pnn->pnext=NULL;
L1.attach(pnn);
cout<<"Do you want to add another item (y/n)\n";
cin>>ch;
}
cnode *pb1=NULL;
cnode *ptrav1=L1.H;
while(ptrav1 !=NULL)
{
if(ptrav1->info>max)
max=ptrav1->info;
pb1=ptrav1;
ptrav1=ptrav1->pnext;
}
cnode *pb2=NULL;
cnode *ptrav2=L1.H;
while(ptrav2 !=NULL)
{
if(ptrav2->info<min)
min=ptrav2->info;
pb2=ptrav2;
ptrav2=ptrav2->pnext;
}
cnode*ptrav;
ptrav=pb2->pnext;
while(ptrav!=pb1)
{
cnode*pnn=new cnode();
pnn->info=ptrav->info;
pnn->pnext=NULL;
L2.attach(pnn);
ptrav=ptrav->pnext;
}
pb2->pnext=pb1;
pb1->pnext=L2.H;
L2.H=pb2;
L2.Display();
}
The prog. crashes here (pnn->info=ptrav->info;)
while(ptrav!=pb1)
{
cnode*pnn=new cnode();
pnn->info=ptrav->info;
pnn->pnext=NULL;
L2.attach(pnn);
ptrav=ptrav->pnext;
}
would you please tell me what should i do to fix this problem??
Thanks a lot
This post has been edited by Hector6008: 07 March 2008 - 03:49 PM

New Topic/Question
Reply




MultiQuote




|