Я пытаюсь представить граф через список смежности.
Я объявил массив указателей, а затем вставил элементы в стиле связанного списка.
#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 возникает из-задоступа к неверному индексу. Но я не могу выяснить, где происходит внешний доступ.