Linked list problem

The program crashes after displaying the results

Page 1 of 1

6 Replies - 955 Views - Last Post: 09 March 2008 - 12:32 PM Rate Topic: -----

#1 Hector6008  Icon User is offline

  • New D.I.C Head

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

Linked list problem

Posted 07 March 2008 - 08:34 AM

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

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  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Linked list problem

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

Was This Post Helpful? 0
  • +
  • -

#3 Hector6008  Icon User is offline

  • New D.I.C Head

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

Re: Linked list problem

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 :^:
Was This Post Helpful? 0
  • +
  • -

#4 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Linked list problem

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
Was This Post Helpful? 0
  • +
  • -

#5 Hector6008  Icon User is offline

  • New D.I.C Head

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

Re: Linked list problem

Posted 07 March 2008 - 11:21 AM

:^: Thanks a lot for ur help :^:
Was This Post Helpful? 0
  • +
  • -

#6 Hector6008  Icon User is offline

  • New D.I.C Head

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

Re: Linked list problem

Posted 07 March 2008 - 03:42 PM

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

#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();
}




Was This Post Helpful? 0
  • +
  • -

#7 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Linked list problem

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1