对以邻接矩阵为存储结构的图进行DFS和BFS遍历 25
#defineMaxEdgeNum20typedefintVertexType;typedefVertexTypeVexlist[8];typedefintadjmatr...
#define MaxEdgeNum 20
typedef int VertexType;
typedef VertexType Vexlist[8];
typedef int adjmatrix[8 ][8];
void Create(Vexlist GV,adjmatrix GA, int n,int e)
{
int i,j,k;
for (i=0;i<n;i++)scanf("%d",GV[i]);
for (i=0;i<n;i++)
for (j=0;j<n;j++){
if(i==j)GA[i][j]=0;
else GA[i][j]=1000 ;}
for (k=0;k<=e;k++)
{scanf("%d,%d",&i,&j);
GA[i][j]=1;
}
}
void dfs1(adjmatrix GA,int i)
{int j;
int visited[8];
printf("%d",i);
visited[i]=1;
for (j=0;j<5;j++)
if(GA[i][j]!=0&&GA[i][j]!=1000&&!visited[j])
dfs1(GA,j);
}
void bfs1(adjmatrix GA,int i)
{int visited[8];
int Q[100],front=-1,rear=-1;
printf("%d",i);
visited[i]=1;
rear++;Q[rear]=i;
while(front!=rear)
{int j;
front++;
for (j=0;j<5;j++)
{if(GA[i][j]!=0&&GA[i][j]!=1000&&!visited[j])
{printf("%d",j);
visited[j]=1;
rear++;Q[rear]=j;
}
}
}
}
main()
{Vexlist GV;
adjmatrix GA;
int n, e,i;
int m=1;
while(m!=4)
printf("** 1:Create**");printf("\n");
printf("** 2:dfs1**") ;printf("\n");
printf("** 3: bfs1**") ;printf("\n");
printf("**4:exit**");printf("\n");
printf("please input 1-4");printf("\n");
scanf("%d",&m);
switch(m)
{case 1: Create(GV,GA,5,7); break;
case 2: dfs1(GA,0); break;
case 3: bfs1(GA,0); break;
}
}
这是我的编程,已编译过,没语法错误,但运行乱码,请高手指教 展开
typedef int VertexType;
typedef VertexType Vexlist[8];
typedef int adjmatrix[8 ][8];
void Create(Vexlist GV,adjmatrix GA, int n,int e)
{
int i,j,k;
for (i=0;i<n;i++)scanf("%d",GV[i]);
for (i=0;i<n;i++)
for (j=0;j<n;j++){
if(i==j)GA[i][j]=0;
else GA[i][j]=1000 ;}
for (k=0;k<=e;k++)
{scanf("%d,%d",&i,&j);
GA[i][j]=1;
}
}
void dfs1(adjmatrix GA,int i)
{int j;
int visited[8];
printf("%d",i);
visited[i]=1;
for (j=0;j<5;j++)
if(GA[i][j]!=0&&GA[i][j]!=1000&&!visited[j])
dfs1(GA,j);
}
void bfs1(adjmatrix GA,int i)
{int visited[8];
int Q[100],front=-1,rear=-1;
printf("%d",i);
visited[i]=1;
rear++;Q[rear]=i;
while(front!=rear)
{int j;
front++;
for (j=0;j<5;j++)
{if(GA[i][j]!=0&&GA[i][j]!=1000&&!visited[j])
{printf("%d",j);
visited[j]=1;
rear++;Q[rear]=j;
}
}
}
}
main()
{Vexlist GV;
adjmatrix GA;
int n, e,i;
int m=1;
while(m!=4)
printf("** 1:Create**");printf("\n");
printf("** 2:dfs1**") ;printf("\n");
printf("** 3: bfs1**") ;printf("\n");
printf("**4:exit**");printf("\n");
printf("please input 1-4");printf("\n");
scanf("%d",&m);
switch(m)
{case 1: Create(GV,GA,5,7); break;
case 2: dfs1(GA,0); break;
case 3: bfs1(GA,0); break;
}
}
这是我的编程,已编译过,没语法错误,但运行乱码,请高手指教 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询