1. #include "stdio.h"
    2. #include "stdlib.h"
    3. #include "io.h"
    4. #include "math.h"
    5. #include "time.h"
    6. #define OK 1
    7. #define ERROR 0
    8. #define TRUE 1
    9. #define FALSE 0
    10. #define MAXSIZE 9 /* 存储空间初始分配量 */
    11. #define MAXEDGE 15
    12. #define MAXVEX 9
    13. #define INFINITY 65535
    14. typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
    15. typedef int Boolean; /* Boolean是布尔类型,其值是TRUE或FALSE */
    16. typedef char VertexType; /* 顶点类型应由用户定义 */
    17. typedef int EdgeType; /* 边上的权值类型应由用户定义 */
    18. /* 邻接矩阵结构 */
    19. typedef struct
    20. {
    21. VertexType vexs[MAXVEX]; /* 顶点表 */
    22. EdgeType arc[MAXVEX][MAXVEX];/* 邻接矩阵,可看作边表 */
    23. int numVertexes, numEdges; /* 图中当前的顶点数和边数 */
    24. }MGraph;
    25. /* 邻接表结构****************** */
    26. typedef struct EdgeNode /* 边表结点 */
    27. {
    28. int adjvex; /* 邻接点域,存储该顶点对应的下标 */
    29. int weight; /* 用于存储权值,对于非网图可以不需要 */
    30. struct EdgeNode *next; /* 链域,指向下一个邻接点 */
    31. }EdgeNode;
    32. typedef struct VertexNode /* 顶点表结点 */
    33. {
    34. int in; /* 顶点入度 */
    35. char data; /* 顶点域,存储顶点信息 */
    36. EdgeNode *firstedge;/* 边表头指针 */
    37. }VertexNode, AdjList[MAXVEX];
    38. typedef struct
    39. {
    40. AdjList adjList;
    41. int numVertexes,numEdges; /* 图中当前顶点数和边数 */
    42. }graphAdjList,*GraphAdjList;
    43. /* **************************** */
    44. /* 用到的队列结构与函数********************************** */
    45. /* 循环队列的顺序存储结构 */
    46. typedef struct
    47. {
    48. int data[MAXSIZE];
    49. int front; /* 头指针 */
    50. int rear; /* 尾指针,若队列不空,指向队列尾元素的下一个位置 */
    51. }Queue;
    52. /* 初始化一个空队列Q */
    53. Status InitQueue(Queue *Q)
    54. {
    55. Q->front=0;
    56. Q->rear=0;
    57. return OK;
    58. }
    59. /* 若队列Q为空队列,则返回TRUE,否则返回FALSE */
    60. Status QueueEmpty(Queue Q)
    61. {
    62. if(Q.front==Q.rear) /* 队列空的标志 */
    63. return TRUE;
    64. else
    65. return FALSE;
    66. }
    67. /* 若队列未满,则插入元素e为Q新的队尾元素 */
    68. Status EnQueue(Queue *Q,int e)
    69. {
    70. if ((Q->rear+1)%MAXSIZE == Q->front) /* 队列满的判断 */
    71. return ERROR;
    72. Q->data[Q->rear]=e; /* 将元素e赋值给队尾 */
    73. Q->rear=(Q->rear+1)%MAXSIZE;/* rear指针向后移一位置, */
    74. /* 若到最后则转到数组头部 */
    75. return OK;
    76. }
    77. /* 若队列不空,则删除Q中队头元素,用e返回其值 */
    78. Status DeQueue(Queue *Q,int *e)
    79. {
    80. if (Q->front == Q->rear) /* 队列空的判断 */
    81. return ERROR;
    82. *e=Q->data[Q->front]; /* 将队头元素赋值给e */
    83. Q->front=(Q->front+1)%MAXSIZE; /* front指针向后移一位置, */
    84. /* 若到最后则转到数组头部 */
    85. return OK;
    86. }
    87. /* ****************************************************** */
    88. void CreateMGraph(MGraph *G)
    89. {
    90. int i, j;
    91. G->numEdges=15;
    92. G->numVertexes=9;
    93. /* 读入顶点信息,建立顶点表 */
    94. G->vexs[0]='A';
    95. G->vexs[1]='B';
    96. G->vexs[2]='C';
    97. G->vexs[3]='D';
    98. G->vexs[4]='E';
    99. G->vexs[5]='F';
    100. G->vexs[6]='G';
    101. G->vexs[7]='H';
    102. G->vexs[8]='I';
    103. for (i = 0; i < G->numVertexes; i++)/* 初始化图 */
    104. {
    105. for ( j = 0; j < G->numVertexes; j++)
    106. {
    107. G->arc[i][j]=0;
    108. }
    109. }
    110. G->arc[0][1]=1;
    111. G->arc[0][5]=1;
    112. G->arc[1][2]=1;
    113. G->arc[1][8]=1;
    114. G->arc[1][6]=1;
    115. G->arc[2][3]=1;
    116. G->arc[2][8]=1;
    117. G->arc[3][4]=1;
    118. G->arc[3][7]=1;
    119. G->arc[3][6]=1;
    120. G->arc[3][8]=1;
    121. G->arc[4][5]=1;
    122. G->arc[4][7]=1;
    123. G->arc[5][6]=1;
    124. G->arc[6][7]=1;
    125. for(i = 0; i < G->numVertexes; i++)
    126. {
    127. for(j = i; j < G->numVertexes; j++)
    128. {
    129. G->arc[j][i] =G->arc[i][j];
    130. }
    131. }
    132. }
    133. /* 利用邻接矩阵构建邻接表 */
    134. void CreateALGraph(MGraph G,GraphAdjList *GL)
    135. {
    136. int i,j;
    137. EdgeNode *e;
    138. *GL = (GraphAdjList)malloc(sizeof(graphAdjList));
    139. (*GL)->numVertexes=G.numVertexes;
    140. (*GL)->numEdges=G.numEdges;
    141. for(i= 0;i <G.numVertexes;i++) /* 读入顶点信息,建立顶点表 */
    142. {
    143. (*GL)->adjList[i].in=0;
    144. (*GL)->adjList[i].data=G.vexs[i];
    145. (*GL)->adjList[i].firstedge=NULL; /* 将边表置为空表 */
    146. }
    147. for(i=0;i<G.numVertexes;i++) /* 建立边表 */
    148. {
    149. for(j=0;j<G.numVertexes;j++)
    150. {
    151. if (G.arc[i][j]==1)
    152. {
    153. e=(EdgeNode *)malloc(sizeof(EdgeNode));
    154. e->adjvex=j; /* 邻接序号为j */
    155. e->next=(*GL)->adjList[i].firstedge; /* 将当前顶点上的指向的结点指针赋值给e */
    156. (*GL)->adjList[i].firstedge=e; /* 将当前顶点的指针指向e */
    157. (*GL)->adjList[j].in++;
    158. }
    159. }
    160. }
    161. }
    162. Boolean visited[MAXSIZE]; /* 访问标志的数组 */
    163. /* 邻接表的深度优先递归算法 */
    164. void DFS(GraphAdjList GL, int i)
    165. {
    166. EdgeNode *p;
    167. visited[i] = TRUE;
    168. printf("%c ",GL->adjList[i].data);/* 打印顶点,也可以其它操作 */
    169. p = GL->adjList[i].firstedge;
    170. while(p)
    171. {
    172. if(!visited[p->adjvex])
    173. DFS(GL, p->adjvex);/* 对为访问的邻接顶点递归调用 */
    174. p = p->next;
    175. }
    176. }
    177. /* 邻接表的深度遍历操作 */
    178. void DFSTraverse(GraphAdjList GL)
    179. {
    180. int i;
    181. for(i = 0; i < GL->numVertexes; i++)
    182. visited[i] = FALSE; /* 初始所有顶点状态都是未访问过状态 */
    183. for(i = 0; i < GL->numVertexes; i++)
    184. if(!visited[i]) /* 对未访问过的顶点调用DFS,若是连通图,只会执行一次 */
    185. DFS(GL, i);
    186. }
    187. /* 邻接表的广度遍历算法 */
    188. void BFSTraverse(GraphAdjList GL)
    189. {
    190. int i;
    191. EdgeNode *p;
    192. Queue Q;
    193. for(i = 0; i < GL->numVertexes; i++)
    194. visited[i] = FALSE;
    195. InitQueue(&Q);
    196. for(i = 0; i < GL->numVertexes; i++)
    197. {
    198. if (!visited[i])
    199. {
    200. visited[i]=TRUE;
    201. printf("%c ",GL->adjList[i].data);/* 打印顶点,也可以其它操作 */
    202. EnQueue(&Q,i);
    203. while(!QueueEmpty(Q))
    204. {
    205. DeQueue(&Q,&i);
    206. p = GL->adjList[i].firstedge; /* 找到当前顶点的边表链表头指针 */
    207. while(p)
    208. {
    209. if(!visited[p->adjvex]) /* 若此顶点未被访问 */
    210. {
    211. visited[p->adjvex]=TRUE;
    212. printf("%c ",GL->adjList[p->adjvex].data);
    213. EnQueue(&Q,p->adjvex); /* 将此顶点入队列 */
    214. }
    215. p = p->next; /* 指针指向下一个邻接点 */
    216. }
    217. }
    218. }
    219. }
    220. }
    221. int main(void)
    222. {
    223. MGraph G;
    224. GraphAdjList GL;
    225. CreateMGraph(&G);
    226. CreateALGraph(G,&GL);
    227. printf("\n深度遍历:");
    228. DFSTraverse(GL);
    229. printf("\n广度遍历:");
    230. BFSTraverse(GL);
    231. return 0;
    232. }