Page 1 of 1

## 6 Replies - 1050 Views - Last Post: 09 March 2008 - 12:32 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=45459&amp;s=726b6b8988335b7967b9fe38a448b7ad&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Hector6008

Reputation: 0
• Posts: 25
• Joined: 14-October 07

Posted 07 March 2008 - 08:34 AM

write a program to do the following:
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

Is This A Good Question/Topic? 0

## Replies To: Linked list problem

### #2 PennyBoki

• system("revolution");

Reputation: 53
• Posts: 2,335
• Joined: 11-December 06

Posted 07 March 2008 - 10:41 AM

```ptrav=pb2->pnext;
while(ptrav!=pb1)
{
cnode*pnn=new cnode();
pnn->info=ptrav->info;
pnn->pnext=NULL;
L2.attach(pnn);
ptrav=ptrav->pnext;
}
```

just few lines above that code there is this line

cnode *pb2=NULL;

*edit wait!!! be right back

This post has been edited by PennyBoki: 07 March 2008 - 10:44 AM

### #3 Hector6008

Reputation: 0
• Posts: 25
• Joined: 14-October 07

Posted 07 March 2008 - 11:06 AM

just few lines above that code there is this line

cnode *pb2=NULL;

*edit wait!!! be right back
[/quote]

Unfortunately, I didn't get the point, anyway thanks a lot

### #4 PennyBoki

• system("revolution");

Reputation: 53
• Posts: 2,335
• Joined: 11-December 06

Posted 07 March 2008 - 11:15 AM

here
```     pb2=ptrav2;
ptrav2=ptrav2->pnext;
}
cnode*ptrav;
ptrav=pb2->pnext;
```
pb2->next is the suspect you only assign to pb2 but nothing to pb2->next

### #5 Hector6008

Reputation: 0
• Posts: 25
• Joined: 14-October 07

Posted 07 March 2008 - 11:21 AM

Thanks a lot for ur help

### #6 Hector6008

Reputation: 0
• Posts: 25
• Joined: 14-October 07

Posted 07 March 2008 - 03:42 PM

The prog. crashes after displaying the results, don't know why??

```#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;
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;
}
clist L2;
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;
}
cnode*temp;
temp=L2.H;
pb2->pnext=pb1;
pb1->pnext=temp;
L2.H=pb2;
L2.Display();
}

```

### #7 PennyBoki

• system("revolution");

Reputation: 53
• Posts: 2,335
• Joined: 11-December 06

Posted 09 March 2008 - 12:32 PM

Maybe you have trouble with holding the execution window open, if so then see this thread

http://www.dreaminco...wtopic30581.htm

This post has been edited by PennyBoki: 09 March 2008 - 12:32 PM