图主要有两种存储结构,邻接矩阵表示法,邻接链表表示法。
以有9个顶点的图为例。如图所示

邻接矩阵表示法
typedef struct { //邻接矩阵
elemtype data[9];
elemtype connect[9][9];
}graph,* graphpoint;
建立9个元素的数组用于表示元素
并建立对应关系矩阵用于表示顶点之间的关系
邻接链表表示法
typedef struct node{
int post;
struct node *next;
}degree,*degreepoint;
typedef struct graphnode{ //临街链表
elemtype gdata;
degree next;
}graph_list,*listpoint;
建立一个结构体用于表示顶点的度,再建立顶点相关的数组
邻接矩阵DFS
status dfs(graph g,int post) //邻接矩阵dfs搜索
{
int i,j;
road[post] = 1;
printf("->%d(%d)",post,g.data[post]);
for(j = 0;j<9;j++)
{
if(g.connect[post][j] == 1&&j != post&&road[j] != 1)
{
dfs(g,j);
}
}
return ok;
}
建立一个结构体外数组road用于储存已经走过的位置,如果位置n走过那么
road[n] = 1
递归调用函数,当出现无路可走时函数退栈回溯至可走位置。
邻接链表DFS
status list_dfs(graph_list g[],int post) //邻接链表dfs
{
int i,j;
degreepoint m;
printf("%d->",post);
road[post] = 1;
m = g[post].next.next;
while(m != NULL)
{
if(road[m->post] == 0)
{
list_dfs(g,m->post);
}
else
{
m = m->next;
}
}
return ok;
}
完整代码
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<memory.h>
#define elemtype int
#define ok 1
#define fail 0
#define status int
int road[9];
typedef struct node{
int post;
struct node *next;
}degree,*degreepoint;
typedef struct graphnode{ //临街链表
elemtype gdata;
degree next;
}graph_list,*listpoint;
typedef struct { //邻接矩阵
elemtype data[9];
elemtype connect[9][9];
}graph,* graphpoint;
status initlist(graph_list g[]) //创建邻接链表
{
int i,m;
listpoint n;
degreepoint d;
for(i = 0;i<9;i++)
{
g[i].next.post = i;
d = &(g[i].next) ;
printf("enter connect %d\n",i);
while(scanf("%d",&m))
{
if(m == -1)
{
d->next = NULL;
break;
}
d->next = (degree *)malloc(sizeof(degree));
d = d->next;
d->post = m;
}
}
return ok;
}
status list_dfs(graph_list g[],int post) //邻接链表dfs
{
int i,j;
degreepoint m;
printf("%d->",post);
road[post] = 1;
m = g[post].next.next;
while(m != NULL)
{
if(road[m->post] == 0)
{
list_dfs(g,m->post);
}
else
{
m = m->next;
}
}
return ok;
}
status dfs(graph g,int post) //邻接矩阵dfs搜索
{
int i,j;
road[post] = 1;
printf("->%d(%d)",post,g.data[post]);
for(j = 0;j<9;j++)
{
if(g.connect[post][j] == 1&&j != post&&road[j] != 1)
{
dfs(g,j);
}
}
return ok;
}
void initgraph(graphpoint g) //邻接矩阵图初始化
{
int i;
int j;
int m;
for(i = 0;i < 9;i++)
{
g->data[i] = i+1;
}
for(i = 0;i<9;i++)
{
printf("输入%d关系:",i);
for(j = 0;j<9;j++)
{
scanf("%d",&g->connect[i][j]);
}
}
}
void showgraph(graph g) //打印邻接矩阵图
{
int i;
int j;
printf("图中顶点元素\n\n");
for(i = 0;i < 9;i++)
{
printf("%d ",g.data[i]);
}
printf("\n\n图中边\n\n");
for(i = 0;i<9;i++)
{
for(j = 0;j<9;j++)
{
printf("%d ",g.connect[i][j]);
}
printf("\n");
}
}
void showlist(graph_list g[]) //打印邻接链表
{
int i,j;
degreepoint m;
printf("\n\n");
for(i = 0;i < 9;i++)
{
m = &(g[i].next);
while(m->next != NULL)
{
printf("%d->",g[m->post].gdata );
m = m->next;
}
printf("%d\n",g[m->post].gdata );
}
printf("\n");
}
int main()
{ int i,j;
graph g;
graph_list g1[9] = {{1},{2},{3},{4},{5},{6},{7},{8},{9}};
for(i = 0;i<9;i++)
{
road[i] = 0;
}
initgraph(&g);
showgraph(g);
printf("\n\n");
dfs(g,4);
printf("\n\n邻接链表\n\n");
memset(road,0,sizeof(road));
initlist(g1);
showlist(g1);
list_dfs(g1,4);
return 0;
}
运行实例 编译器DEV C++
