adjacency list graph representation

"My Programm is crashing and i dont know why can anyone help..?"





#include<iostream>


using namespace std;
int arr[3];
struct edges
{


int edge_data;
edges *next;
int cost;



}*edge_head,*ptr1;

struct graph

{

int node;
int status;
graph *next;
edges *edge_next;



} *head,*ptr,*tail;




void append_node(int x)

{

//ptr=head;

graph *newptr;

for(ptr=head; ptr->next!=NULL; ptr=ptr->next){}



newptr=new graph;

newptr->node=x;

ptr->next=newptr;
newptr->next=NULL;
ptr->edge_next=NULL;

}

void append_edges(int node,int data)
{



for(ptr=head;ptr!=NULL;ptr=ptr->next)
{


if(ptr->node==node)



{//cout<<"HI\n";


if(ptr->edge_next==NULL)
{

ptr->edge_next=new edges;
//ptr1=ptr->edge_next;
ptr1=ptr->edge_next;
ptr1->edge_data=data;

ptr1->next=NULL;
//ptr->edge_next=NULL;
//ptr->edge_next->cost=cost;



}

else if (ptr->edge_next!=NULL)
{
//cout<<ptr->edge_next;

// cout<<"HI\n";
for(ptr1=ptr->edge_next; ptr1->next!=NULL; ptr1=ptr1->next){}


ptr1->next=new edges;
ptr1=ptr1->next;
ptr1->edge_data=data;
//ptr1->cost=cost;
ptr1->next=NULL;



}

}

// return l;

}

}
//l:


//ptr1=new edges;
/* ptr1=edge_head;
for(; ptr1->next!=NULL; ptr1=ptr1->next){}
ptr1->next=new edges;
ptr1=ptr1->next;
ptr1->edge_data=y;
ptr1->cost=z;
ptr1->next=NULL;

//ptr1->next=ptr1;

*/




/* void display()
{

for(ptr=head;ptr!=NULL;ptr=ptr->next)
{

cout<<ptr->node;
cout<<"\t";
//cout<<ptr->status;
cout<<endl;
}
}*/


/* void display()
{

for(int i=0;i<3;i++)
{

cout<<arr[i];
cout<<endl;


}
}
*/



/*void enqueue(int newdata)
{
ptr2=new node;
//ptr=head;
if(node_head==NULL)
{

node_tail=ptr2;
node_head=ptr2;

}
else
{

node_tail->next=ptr2;
node_tail=ptr2;

}

node_tail->data=newdata;
node_tail->next=NULL;

}

void dequeue()
{

int data;
if (node_head==NULL && node_tail==NULL)
{
cout<<"Queue is Empty";



}
data=node_head->data;


if(node_head==node_tail)
{

node_head=NULL;
node_tail=NULL;


}

else
{

node_head=node_head->next;
}
}
*/

/*void bfs()
{int i=0;

ptr2=node_head;
enqueue(ptr->data);
arr[i]=dequeue();
while(ptr2!=NULL)
{ i=i+1;





if(ptr2->data==ptr->data)
{
}

else{

while(ptr->edge_next->next!=NULL)


{

enqueue(ptr->edge_next->edge_data);


}
arr[i]=dequeue();
ptr=ptr->next;
//dequeue();

}

}















}*/


void display1()
{

/* ptr=head->next;
cout<<ptr->node;
cout<<"->";
*/

for(ptr=head;ptr!=NULL;ptr=ptr->next)
{
cout<<ptr->node;
cout<<"->";

for(ptr1=ptr->edge_next; ptr1!=NULL; ptr1=ptr1->next)
{



cout<<ptr1->edge_data;

cout<<" ";

}
cout<<"\n";





}


}

int main()

{
int node,cost,edge,n1,x,data,n,a,rec;
int h,k,flag;








//tail=head;
//head->next=ptr;



cout<<"How many nodes you want in the graphs..?\n";
cin>>n;

cout<<"Enter the nodes\n";

for(int i=0;i<n;i++)
{
if(i==0)
{
head=new graph;
cin>>a;
head->node=a;
head->next=NULL;
head->edge_next=NULL;
}

else
{


cin>>x;
append_node(x);


}

}


cout<<"Enter the node for which you want an edge\n";

cin>>node;
for(ptr=head;ptr!=NULL;ptr=ptr->next)
{

if(ptr->node==node)
{

cout<<"How many edges do you want \n";
cin>>n1;
cout<<"Enter the edge to be connected to the node\n";



for(int j=0;j<n1;j++)
{

cin>>edge;
cout<<endl;

for(ptr=head;ptr!=NULL;ptr=ptr->next)
{ //cout<<ptr->node;
// cout<<endl;


// cout<<ptr->node<<endl;

if(ptr==NULL)
{

cout<<"NOT ALLOWED\n";

flag=1;

}



else if(ptr->node==edge)
{

//cout<<"enter the cost\n";
//cin>>cost;

append_edges( node, edge);
//goto h;
}
if (flag==1)
{
break;}

}

if (flag==1)
{
break;
}
}

cout<<endl;
//cout<<"The nodes are\n";

//display();
if (flag==1)
{
break;}
cout<<"The Edges and nodes are\n";
display1();


}
else if(ptr==NULL)
{

cout<<"Element not found\n";
}

if (flag==1)
{
break;}
}



//h:

















//k:
return 0;

}



Topic archived. No new replies allowed.