3 Replies - 571 Views - Last Post: 08 October 2008 - 02:15 PM Rate Topic: -----

#1 payamrastogi  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 31
  • Joined: 30-July 08

Segmentation Fault

Posted 08 October 2008 - 06:14 AM

#include<iostream>

using namespace std;



struct node

{

	int name;

	struct node *connect;

	struct node *next;

};



class graph

{

	public:

		node* p;



		graph()

		{

			p = NULL;

		}

	int displaymenu();

	void adb(struct node *q,int x);

	void display(struct node *q);

	void ade(struct node *q,int x);

	void search(struct node *q,int x, int y);

};



int main()

{

	graph g;

	int x,choice,l, y;

	static int count;

	do

	{

		choice=g.displaymenu();

		switch(choice)

		{

		case 1:x=++count;

			   g.adb(g.p,x);

			   break;

		case 2:cout<<"Connect: ";

			   cin>>x;

			   cout<<"to: ";

			   cin>>y;

			   g.search(g.p,x,y);

			   g.search(g.p,y,x);

			   break;

		case 5:g.display(g.p);

			break;

		case 6:break;

		default:cout<<"Invalid choice";

		}

	}while(choice!=6);

	return 0;

}



int graph::displaymenu()

{

	int c;

	cout<<"\n---Network Configuration---";

	cout<<"\n1.Add a Node in the Network";

	cout<<"\n2.Connect the Node";

	/*cout<<"\n3.Delete the Node";

	cout<<"\n4.Delete the Connection";*/

	cout<<"\n5.Display the Network Map";

	cout<<"\n6.Exit";

	cout<<"\n\tchoice=";

	cin>>c;

	return c;

}



void graph::adb(struct node *q,int x)

{

	struct node *temp;

	temp=new node;

	temp->name=x;

	temp->next=q;

	temp->connect=NULL;

	q=temp;

	p=q;

}



void graph::display(struct node *q)

{

	struct node *temp1;

	struct node *temp2;

	temp1=q;

	temp2=q;

	 cout<<"\nNodes";

	 while(temp1!=NULL)

	 {

	cout<<"\n"<<temp1->name;

	while(temp2!=NULL)

	{

		temp2=temp2->connect;

		cout<<"\t->"<<temp2->name;

	}

	temp1=temp1->next;

	temp2=temp1;

	 }

}



void graph::ade(struct node *q,int x)

{

	struct node *temp,*r;

	if(q==NULL)

	{

		temp=new node;

		temp->name=x;						   //cout<<temp->name;

		temp->connect=NULL;

		q=temp;

		p=q;

	}

	else

	{

		temp=q;

		while(temp->connect!=NULL)

		{

			temp=temp->connect;

			cout<<temp->name;

		}

		r=new node;

		r->name=x;						   //	cout<<r->name;

		r->connect=NULL;					   //	cout<<temp->name;

		temp->connect=r;

	}

}



void graph::search(struct node *q,int x, int y)

{

 struct node *temp;

 int loc=1;

 temp=q;

 while(temp->name!=x)

 {

  temp=temp->next;

  if(temp==NULL)

  {

   cout<<"number not found";

   return;

  }

  loc=loc+1;

 }

 graph::ade(temp,y);

 return;

}





Is This A Good Question/Topic? 0
  • +

Replies To: Segmentation Fault

#2 RedSonja  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 172
  • Joined: 04-September 08

Re: Segmentation Fault

Posted 08 October 2008 - 06:48 AM

If you look in online help it will tell you all about segmentation error. Did you overrun a buffer? Maybe it was too small, maybe you input more data than you planned. If you can't see it straightaway try stepping through with the debugger.
Was This Post Helpful? 0
  • +
  • -

#3 payamrastogi  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 31
  • Joined: 30-July 08

Re: Segmentation Fault

Posted 08 October 2008 - 02:04 PM

thanks for your reply, but the program is running properly in turbo C++ but when we run it on gcc it halt with segmentation fault error
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,210
  • Joined: 23-August 08

Re: Segmentation Fault

Posted 08 October 2008 - 02:15 PM

Try replacing your loop in display() with this:
while(temp1 != NULL)
{
	cout << "\n" << temp1->name;
	temp2 = temp2->connect;
	while(temp2 != NULL)
	{
		cout << "\t->" <<temp2->name;
		temp2 = temp2->connect;
	}

	temp1 = temp1->next;
	temp2 = temp1;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1