高手在帮忙看一下吧……关系到毕业啊……小弟多谢了!!!
把一个无环有向图存储电脑里,随机生成遍历序列的程序,编译没有出错,运行的时候老是提示非法操作,debug一下的提示是:Unhandled exception in mysearch.exe:)xC0000005:Access Violation.
,还有对v的值,提示:CXX0030:Error:expression cannot be evaluated。搞了以下午了,头都大了,求求各位大侠帮忙调调吧,小弟多谢!!!源码是这样的:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#define n 3 // 图中顶点数
#define e 3 // 图中边数
typedef struct Graph
{
int v[n+1]; // 存放顶点信息v1,v2,….vn,不使用v[0]存储空间
int arcs[n+1][n+1]; // 邻接矩阵
} graph;
void creatadj(graph* p);
void randomsearch(int i, graph *p, int *path,int depth);
void main()
{
graph *p;
p=(graph *)malloc(sizeof(struct Graph));
int m;
for(m=1;m<=n;m++)
p->v[m]=0;
creatadj(p);
int a,b;
for(a=1;a<=n;a++)
{
for(b=1;b<=n;b++)
cout<<p->arcs[a][b];
cout<<endl;
}
cout<<"1 ";
int h;
int path[n];
for(h=0;h<n;h++)
path[h]=0;
randomsearch(1, p, path,0);
}
void randomsearch(int i, graph *p, int *path,int depth)
{
cout<<"aaa";
int outdegree=0; //i的出度
int nozero[n];
int j,k;
int l=0;
int m;
for(m=0;m<n;m++)
nozero[m]=0;
cout<<"2 ";//
path[depth]=i;
for(j=1;j<=n;j++)
{
outdegree=outdegree+(p->arcs[i][j]);
if(p->arcs[i][j]!=0)
{
nozero[l]=j;
l=l+1;
}
}
cout<<" 3 ";
if(outdegree!=0)
{
int t;
depth=depth+1;
t=(rand()%outdegree);
path[depth]=nozero[t];
randomsearch(t,p,path,depth);
cout<<"4";
}
else
{
cout<<" 5 ";
for(k=0;k<=depth;k++)
cout<<path[k]<<",";
cout<<endl;
}
}
void creatadj(graph* p)
{
int i,j,k ;
for (k=1; k<=n; k++)
{
cout<<"输入第"<<k<<"个顶点。"<<endl;
cin>>p->v[k];
}
for (i=1; i<=n; i++ )
for (j=1; j<=n; j++)
p->arcs[i][j]=0;
for (k=1; k<=e; k++)
{ cout<<"输入已存在的边"<<endl;
cin>>i>>j;
p->arcs[i][j]=1;
}
}