I am trying to represent a graph through adjacency list .
I have declared an array of pointers and then inserted elements in a linked list style.
#include<iostream>
using namespace std;
struct node
{
int data;
node* next;
};
int main()
{
int testcases,vertex,edge;
cin>>testcases>>vertex>>edge;
node* arr[1001];
for(int i=0;i<1001;++i)
arr[i]=NULL;
while(testcases--)
{
while(edge--)
{
int a,b;
cin>>a>>b;
node* p=arr[a];
if(!p)
{
p=new node;
p->data=b;
p->next=NULL;
}
else
{
while(!(p->next))
p=p->next;
node*t=new node;
t->data=b;
t->next=NULL;
p->next=t;
}
for(int i=0;i<vertex;++i)
{
cout<<i<<"->"<<" ";
node* t=arr[i];
while(t->next!=NULL)
{
cout<<t->data<<"->"<<" ";
}
cout<<endl;
}
}
}
}
SIGSEGV error occurs because of accessing of invalid index . But I'm not able to find out where the out-of-bound access is occurring .
Aucun commentaire:
Enregistrer un commentaire