关于一下代码,老提示内存出错
完整代码如下,编译正常,indegree函数无法运行,错误应该在红色字体标注的循环处。如果设J为一个定值可以运行,为什么循环就不行了,求大神解答
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
#define MAX_VERTEX_NUM 20
typedef struct ArcNode{
string adjvex;
int time;
struct ArcNode *nextarc;
}ArcNode;
ArcNode *q,*p;
ArcNode *a[MAX_VERTEX_NUM];
int x;
void datainput()
{
int i,n,j,m;
string k,l;
cout<<"课程总数"<<endl;
cin>>n;
x=n;
for(i=1;i<=n;i++)
{
p= new ArcNode;
cout<<"输入第"<<i<<"科"<<endl;
cin>>k;
p->adjvex=k;
a[i]=p;
a[i]->time=0;
cout<<"子课程总数"<<endl;
cin>>m;
if(m==0)
{
a[i]->nextarc=NULL;
}
for(j=1;j<=m;j++)
{
cout<<"第"<<j<<"个子课程"<<endl;
q=new ArcNode;
cin>>l;
q->adjvex=l;
p->nextarc=q;
q->nextarc=NULL;
p=q;
}
}
}
void dataoutput()
{
int i;
for(i=1;i<=x;i++)
{
p=a[i];
for(;p->nextarc!=NULL;)
{
cout<<p->adjvex<<" ";
p=p->nextarc;
}
cout<<p->adjvex<<endl;
}
}
void indegree()
{
int i,j;
for(i=1;i<=x;i++)
{
for(j=1;j<=x;j++)
{
p=a[j]->nextarc;
if(p->nextarc!=0)
{
if(strcmp(p->adjvex.c_str(),a[i]->adjvex.c_str())==0)
a[i]->time+=1;
p=p->nextarc;
}
if(strcmp(p->adjvex.c_str(),a[i]->adjvex.c_str())==0)
a[i]->time+=1;
}
}
for(i=1;i<=x;i++)
{
cout<<a[i]->time<<" ";
}
cout<<endl;
}
void main()
{
datainput();
dataoutput();
indegree();
cout<<x<<endl;
}