求邻接矩阵的所有路径.程序如下.邻接矩阵已经建立.
#include<stdlib.h>#include<stdio.h>#definestatusint#defineOK1#defineERROR0#definemaxs...
#include <stdlib.h>
#include <stdio.h>
#define status int
#define OK 1
#define ERROR 0
#define maxsize 100
#define elemtype char
#define maxchar 50
typedef struct vertex//顶点类型
{
int num;
char data[maxchar];
} Gnode;
typedef struct graph//图类型
{
int v;
int e;
Gnode ver[maxsize];
int edges[maxsize][maxsize];
}Graph;
/*typedef struct queue
{
elemtype qu[maxsize];
int head,rear;//head指向头元素,rear指向尾元素的下一个位置
}Queue;
*/
int strcmp(char *s1,char *s2)
{
int i=0;
for(;i<maxchar;i++)
{if(s1[i]!=s2[i])return 0;
else return 1;
}
}
int CreateGraph(Graph g)//建立图的邻接矩阵
{
int i;
printf("please input V and E\n");
scanf("%d %d",&g.v,&g.e);//输入图的顶点数和边数
printf("V is :%d,E is :%d\n",g.v,g.e);
for(i=0;i<g.v;i++)//初始化每个顶点
{
g.ver[i].num=i;
printf("please input V's data:\n");
char s[maxchar];
scanf("%s",&g.ver[i].data);
}
for(i=0;i<g.e;i++)//初始化每个边
{
int j,start,end;
char s1[maxchar],s2[maxchar];
printf("please input E's data:");
scanf("%s %s",&s1,&s2);
for(j=0;j<g.v;j++)
{
if(strcmp(g.ver[j].data,s1)) start=g.ver[j].num;
if(strcmp(g.ver[j].data,s2)) end=g.ver[j].num;
}
g.edges[start][end]=1;
printf("E is: %s -> %s, edge is: %d %d\n",s1,s2,start,end);
}
}
void BFS(Graph g)
{
int i=0;
for(;i<g.v;i++)
{}//这里不会写了
}
int main()
{
Graph g;
CreateGraph(g);
system("PAUSE");
return 0;
} 展开
#include <stdio.h>
#define status int
#define OK 1
#define ERROR 0
#define maxsize 100
#define elemtype char
#define maxchar 50
typedef struct vertex//顶点类型
{
int num;
char data[maxchar];
} Gnode;
typedef struct graph//图类型
{
int v;
int e;
Gnode ver[maxsize];
int edges[maxsize][maxsize];
}Graph;
/*typedef struct queue
{
elemtype qu[maxsize];
int head,rear;//head指向头元素,rear指向尾元素的下一个位置
}Queue;
*/
int strcmp(char *s1,char *s2)
{
int i=0;
for(;i<maxchar;i++)
{if(s1[i]!=s2[i])return 0;
else return 1;
}
}
int CreateGraph(Graph g)//建立图的邻接矩阵
{
int i;
printf("please input V and E\n");
scanf("%d %d",&g.v,&g.e);//输入图的顶点数和边数
printf("V is :%d,E is :%d\n",g.v,g.e);
for(i=0;i<g.v;i++)//初始化每个顶点
{
g.ver[i].num=i;
printf("please input V's data:\n");
char s[maxchar];
scanf("%s",&g.ver[i].data);
}
for(i=0;i<g.e;i++)//初始化每个边
{
int j,start,end;
char s1[maxchar],s2[maxchar];
printf("please input E's data:");
scanf("%s %s",&s1,&s2);
for(j=0;j<g.v;j++)
{
if(strcmp(g.ver[j].data,s1)) start=g.ver[j].num;
if(strcmp(g.ver[j].data,s2)) end=g.ver[j].num;
}
g.edges[start][end]=1;
printf("E is: %s -> %s, edge is: %d %d\n",s1,s2,start,end);
}
}
void BFS(Graph g)
{
int i=0;
for(;i<g.v;i++)
{}//这里不会写了
}
int main()
{
Graph g;
CreateGraph(g);
system("PAUSE");
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询