日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

邻接表的图遍历

發布時間:2025/4/16 编程问答 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 邻接表的图遍历 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h"#define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0#define MAXSIZE 9 /* 存儲空間初始分配量 */ #define MAXEDGE 15 #define MAXVEX 9 #define INFINITY 65535typedef int Status; /* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */ typedef int Boolean; /* Boolean是布爾類型,其值是TRUE或FALSE */typedef char VertexType; /* 頂點類型應由用戶定義 */ typedef int EdgeType; /* 邊上的權值類型應由用戶定義 *//* 鄰接矩陣結構 */ typedef struct {VertexType vexs[MAXVEX]; /* 頂點表 */EdgeType arc[MAXVEX][MAXVEX];/* 鄰接矩陣,可看作邊表 */int numVertexes, numEdges; /* 圖中當前的頂點數和邊數 */ }MGraph;/* 鄰接表結構****************** */ typedef struct EdgeNode /* 邊表結點 */ {int adjvex; /* 鄰接點域,存儲該頂點對應的下標 */int weight; /* 用于存儲權值,對于非網圖可以不需要 */struct EdgeNode *next; /* 鏈域,指向下一個鄰接點 */ }EdgeNode;typedef struct VertexNode /* 頂點表結點 */ {int in; /* 頂點入度 */char data; /* 頂點域,存儲頂點信息 */EdgeNode *firstedge;/* 邊表頭指針 */ }VertexNode, AdjList[MAXVEX];typedef struct {AdjList adjList; int numVertexes,numEdges; /* 圖中當前頂點數和邊數 */ }graphAdjList,*GraphAdjList; /* **************************** *//* 用到的隊列結構與函數********************************** */ /* 循環隊列的順序存儲結構 */ typedef struct {int data[MAXSIZE];int front; /* 頭指針 */int rear; /* 尾指針,若隊列不空,指向隊列尾元素的下一個位置 */ }Queue;/* 初始化一個空隊列Q */ Status InitQueue(Queue *Q) {Q->front=0;Q->rear=0;return OK; }/* 若隊列Q為空隊列,則返回TRUE,否則返回FALSE */ Status QueueEmpty(Queue Q) { if(Q.front==Q.rear) /* 隊列空的標志 */return TRUE;elsereturn FALSE; }/* 若隊列未滿,則插入元素e為Q新的隊尾元素 */ Status EnQueue(Queue *Q,int e) {if ((Q->rear+1)%MAXSIZE == Q->front) /* 隊列滿的判斷 */return ERROR;Q->data[Q->rear]=e; /* 將元素e賦值給隊尾 */Q->rear=(Q->rear+1)%MAXSIZE;/* rear指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; }/* 若隊列不空,則刪除Q中隊頭元素,用e返回其值 */ Status DeQueue(Queue *Q,int *e) {if (Q->front == Q->rear) /* 隊列空的判斷 */return ERROR;*e=Q->data[Q->front]; /* 將隊頭元素賦值給e */Q->front=(Q->front+1)%MAXSIZE; /* front指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; } /* ****************************************************** */void CreateMGraph(MGraph *G) {int i, j;G->numEdges=15;G->numVertexes=9;/* 讀入頂點信息,建立頂點表 */ G->vexs[0]='A';G->vexs[1]='B';G->vexs[2]='C';G->vexs[3]='D';G->vexs[4]='E';G->vexs[5]='F';G->vexs[6]='G';G->vexs[7]='H';G->vexs[8]='I';for (i = 0; i < G->numVertexes; i++)/* 初始化圖 */{for ( j = 0; j < G->numVertexes; j++){G->arc[i][j]=0;}}G->arc[0][1]=1;G->arc[0][5]=1;G->arc[1][2]=1; G->arc[1][8]=1; G->arc[1][6]=1; G->arc[2][3]=1; G->arc[2][8]=1; G->arc[3][4]=1;G->arc[3][7]=1;G->arc[3][6]=1;G->arc[3][8]=1;G->arc[4][5]=1;G->arc[4][7]=1;G->arc[5][6]=1; G->arc[6][7]=1; for(i = 0; i < G->numVertexes; i++){for(j = i; j < G->numVertexes; j++){G->arc[j][i] =G->arc[i][j];}}}/* 利用鄰接矩陣構建鄰接表 */ void CreateALGraph(MGraph G,GraphAdjList *GL) {int i,j;EdgeNode *e;*GL = (GraphAdjList)malloc(sizeof(graphAdjList));(*GL)->numVertexes=G.numVertexes;(*GL)->numEdges=G.numEdges;for(i= 0;i <G.numVertexes;i++) /* 讀入頂點信息,建立頂點表 */ {(*GL)->adjList[i].in=0;(*GL)->adjList[i].data=G.vexs[i];(*GL)->adjList[i].firstedge=NULL; /* 將邊表置為空表 */}for(i=0;i<G.numVertexes;i++) /* 建立邊表 */{ for(j=0;j<G.numVertexes;j++){if (G.arc[i][j]==1){e=(EdgeNode *)malloc(sizeof(EdgeNode));e->adjvex=j; /* 鄰接序號為j */ e->next=(*GL)->adjList[i].firstedge; /* 將當前頂點上的指向的結點指針賦值給e */(*GL)->adjList[i].firstedge=e; /* 將當前頂點的指針指向e */ (*GL)->adjList[j].in++;}}}}Boolean visited[MAXSIZE]; /* 訪問標志的數組 *//* 鄰接表的深度優先遞歸算法 */ void DFS(GraphAdjList GL, int i) {EdgeNode *p;visited[i] = TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */p = GL->adjList[i].firstedge;while(p){if(!visited[p->adjvex])DFS(GL, p->adjvex);/* 對為訪問的鄰接頂點遞歸調用 */p = p->next;} }/* 鄰接表的深度遍歷操作 */ void DFSTraverse(GraphAdjList GL) {int i;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE; /* 初始所有頂點狀態都是未訪問過狀態 */for(i = 0; i < GL->numVertexes; i++)if(!visited[i]) /* 對未訪問過的頂點調用DFS,若是連通圖,只會執行一次 */ DFS(GL, i); }/* 鄰接表的廣度遍歷算法 */ void BFSTraverse(GraphAdjList GL) {int i;EdgeNode *p;Queue Q;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE;InitQueue(&Q);for(i = 0; i < GL->numVertexes; i++){if (!visited[i]){visited[i]=TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */EnQueue(&Q,i);while(!QueueEmpty(Q)){DeQueue(&Q,&i);p = GL->adjList[i].firstedge; /* 找到當前頂點的邊表鏈表頭指針 */while(p){if(!visited[p->adjvex]) /* 若此頂點未被訪問 */{visited[p->adjvex]=TRUE;printf("%c ",GL->adjList[p->adjvex].data);EnQueue(&Q,p->adjvex); /* 將此頂點入隊列 */}p = p->next; /* 指針指向下一個鄰接點 */}}}} }int main(void) { MGraph G; GraphAdjList GL; CreateMGraph(&G);CreateALGraph(G,&GL);printf("\n深度遍歷:");DFSTraverse(GL);printf("\n廣度遍歷:");BFSTraverse(GL);return 0; }#include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h"#define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0#define MAXSIZE 9 /* 存儲空間初始分配量 */ #define MAXEDGE 15 #define MAXVEX 9 #define INFINITY 65535typedef int Status; /* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */ typedef int Boolean; /* Boolean是布爾類型,其值是TRUE或FALSE */typedef char VertexType; /* 頂點類型應由用戶定義 */ typedef int EdgeType; /* 邊上的權值類型應由用戶定義 *//* 鄰接矩陣結構 */ typedef struct {VertexType vexs[MAXVEX]; /* 頂點表 */EdgeType arc[MAXVEX][MAXVEX];/* 鄰接矩陣,可看作邊表 */int numVertexes, numEdges; /* 圖中當前的頂點數和邊數 */ }MGraph;/* 鄰接表結構****************** */ typedef struct EdgeNode /* 邊表結點 */ {int adjvex; /* 鄰接點域,存儲該頂點對應的下標 */int weight; /* 用于存儲權值,對于非網圖可以不需要 */struct EdgeNode *next; /* 鏈域,指向下一個鄰接點 */ }EdgeNode;typedef struct VertexNode /* 頂點表結點 */ {int in; /* 頂點入度 */char data; /* 頂點域,存儲頂點信息 */EdgeNode *firstedge;/* 邊表頭指針 */ }VertexNode, AdjList[MAXVEX];typedef struct {AdjList adjList; int numVertexes,numEdges; /* 圖中當前頂點數和邊數 */ }graphAdjList,*GraphAdjList; /* **************************** *//* 用到的隊列結構與函數********************************** */ /* 循環隊列的順序存儲結構 */ typedef struct {int data[MAXSIZE];int front; /* 頭指針 */int rear; /* 尾指針,若隊列不空,指向隊列尾元素的下一個位置 */ }Queue;/* 初始化一個空隊列Q */ Status InitQueue(Queue *Q) {Q->front=0;Q->rear=0;return OK; }/* 若隊列Q為空隊列,則返回TRUE,否則返回FALSE */ Status QueueEmpty(Queue Q) { if(Q.front==Q.rear) /* 隊列空的標志 */return TRUE;elsereturn FALSE; }/* 若隊列未滿,則插入元素e為Q新的隊尾元素 */ Status EnQueue(Queue *Q,int e) {if ((Q->rear+1)%MAXSIZE == Q->front) /* 隊列滿的判斷 */return ERROR;Q->data[Q->rear]=e; /* 將元素e賦值給隊尾 */Q->rear=(Q->rear+1)%MAXSIZE;/* rear指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; }/* 若隊列不空,則刪除Q中隊頭元素,用e返回其值 */ Status DeQueue(Queue *Q,int *e) {if (Q->front == Q->rear) /* 隊列空的判斷 */return ERROR;*e=Q->data[Q->front]; /* 將隊頭元素賦值給e */Q->front=(Q->front+1)%MAXSIZE; /* front指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; } /* ****************************************************** */void CreateMGraph(MGraph *G) {int i, j;G->numEdges=15;G->numVertexes=9;/* 讀入頂點信息,建立頂點表 */ G->vexs[0]='A';G->vexs[1]='B';G->vexs[2]='C';G->vexs[3]='D';G->vexs[4]='E';G->vexs[5]='F';G->vexs[6]='G';G->vexs[7]='H';G->vexs[8]='I';for (i = 0; i < G->numVertexes; i++)/* 初始化圖 */{for ( j = 0; j < G->numVertexes; j++){G->arc[i][j]=0;}}G->arc[0][1]=1;G->arc[0][5]=1;G->arc[1][2]=1; G->arc[1][8]=1; G->arc[1][6]=1; G->arc[2][3]=1; G->arc[2][8]=1; G->arc[3][4]=1;G->arc[3][7]=1;G->arc[3][6]=1;G->arc[3][8]=1;G->arc[4][5]=1;G->arc[4][7]=1;G->arc[5][6]=1; G->arc[6][7]=1; for(i = 0; i < G->numVertexes; i++){for(j = i; j < G->numVertexes; j++){G->arc[j][i] =G->arc[i][j];}}}/* 利用鄰接矩陣構建鄰接表 */ void CreateALGraph(MGraph G,GraphAdjList *GL) {int i,j;EdgeNode *e;*GL = (GraphAdjList)malloc(sizeof(graphAdjList));(*GL)->numVertexes=G.numVertexes;(*GL)->numEdges=G.numEdges;for(i= 0;i <G.numVertexes;i++) /* 讀入頂點信息,建立頂點表 */ {(*GL)->adjList[i].in=0;(*GL)->adjList[i].data=G.vexs[i];(*GL)->adjList[i].firstedge=NULL; /* 將邊表置為空表 */}for(i=0;i<G.numVertexes;i++) /* 建立邊表 */{ for(j=0;j<G.numVertexes;j++){if (G.arc[i][j]==1){e=(EdgeNode *)malloc(sizeof(EdgeNode));e->adjvex=j; /* 鄰接序號為j */ e->next=(*GL)->adjList[i].firstedge; /* 將當前頂點上的指向的結點指針賦值給e */(*GL)->adjList[i].firstedge=e; /* 將當前頂點的指針指向e */ (*GL)->adjList[j].in++;}}}}Boolean visited[MAXSIZE]; /* 訪問標志的數組 *//* 鄰接表的深度優先遞歸算法 */ void DFS(GraphAdjList GL, int i) {EdgeNode *p;visited[i] = TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */p = GL->adjList[i].firstedge;while(p){if(!visited[p->adjvex])DFS(GL, p->adjvex);/* 對為訪問的鄰接頂點遞歸調用 */p = p->next;} }/* 鄰接表的深度遍歷操作 */ void DFSTraverse(GraphAdjList GL) {int i;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE; /* 初始所有頂點狀態都是未訪問過狀態 */for(i = 0; i < GL->numVertexes; i++)if(!visited[i]) /* 對未訪問過的頂點調用DFS,若是連通圖,只會執行一次 */ DFS(GL, i); }/* 鄰接表的廣度遍歷算法 */ void BFSTraverse(GraphAdjList GL) {int i;EdgeNode *p;Queue Q;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE;InitQueue(&Q);for(i = 0; i < GL->numVertexes; i++){if (!visited[i]){visited[i]=TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */EnQueue(&Q,i);while(!QueueEmpty(Q)){DeQueue(&Q,&i);p = GL->adjList[i].firstedge; /* 找到當前頂點的邊表鏈表頭指針 */while(p){if(!visited[p->adjvex]) /* 若此頂點未被訪問 */{visited[p->adjvex]=TRUE;printf("%c ",GL->adjList[p->adjvex].data);EnQueue(&Q,p->adjvex); /* 將此頂點入隊列 */}p = p->next; /* 指針指向下一個鄰接點 */}}}} }int main(void) { MGraph G; GraphAdjList GL; CreateMGraph(&G);CreateALGraph(G,&GL);printf("\n深度遍歷:");DFSTraverse(GL);printf("\n廣度遍歷:");BFSTraverse(GL);return 0; }#include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h"#define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0#define MAXSIZE 9 /* 存儲空間初始分配量 */ #define MAXEDGE 15 #define MAXVEX 9 #define INFINITY 65535typedef int Status; /* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */ typedef int Boolean; /* Boolean是布爾類型,其值是TRUE或FALSE */typedef char VertexType; /* 頂點類型應由用戶定義 */ typedef int EdgeType; /* 邊上的權值類型應由用戶定義 *//* 鄰接矩陣結構 */ typedef struct {VertexType vexs[MAXVEX]; /* 頂點表 */EdgeType arc[MAXVEX][MAXVEX];/* 鄰接矩陣,可看作邊表 */int numVertexes, numEdges; /* 圖中當前的頂點數和邊數 */ }MGraph;/* 鄰接表結構****************** */ typedef struct EdgeNode /* 邊表結點 */ {int adjvex; /* 鄰接點域,存儲該頂點對應的下標 */int weight; /* 用于存儲權值,對于非網圖可以不需要 */struct EdgeNode *next; /* 鏈域,指向下一個鄰接點 */ }EdgeNode;typedef struct VertexNode /* 頂點表結點 */ {int in; /* 頂點入度 */char data; /* 頂點域,存儲頂點信息 */EdgeNode *firstedge;/* 邊表頭指針 */ }VertexNode, AdjList[MAXVEX];typedef struct {AdjList adjList; int numVertexes,numEdges; /* 圖中當前頂點數和邊數 */ }graphAdjList,*GraphAdjList; /* **************************** *//* 用到的隊列結構與函數********************************** */ /* 循環隊列的順序存儲結構 */ typedef struct {int data[MAXSIZE];int front; /* 頭指針 */int rear; /* 尾指針,若隊列不空,指向隊列尾元素的下一個位置 */ }Queue;/* 初始化一個空隊列Q */ Status InitQueue(Queue *Q) {Q->front=0;Q->rear=0;return OK; }/* 若隊列Q為空隊列,則返回TRUE,否則返回FALSE */ Status QueueEmpty(Queue Q) { if(Q.front==Q.rear) /* 隊列空的標志 */return TRUE;elsereturn FALSE; }/* 若隊列未滿,則插入元素e為Q新的隊尾元素 */ Status EnQueue(Queue *Q,int e) {if ((Q->rear+1)%MAXSIZE == Q->front) /* 隊列滿的判斷 */return ERROR;Q->data[Q->rear]=e; /* 將元素e賦值給隊尾 */Q->rear=(Q->rear+1)%MAXSIZE;/* rear指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; }/* 若隊列不空,則刪除Q中隊頭元素,用e返回其值 */ Status DeQueue(Queue *Q,int *e) {if (Q->front == Q->rear) /* 隊列空的判斷 */return ERROR;*e=Q->data[Q->front]; /* 將隊頭元素賦值給e */Q->front=(Q->front+1)%MAXSIZE; /* front指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; } /* ****************************************************** */void CreateMGraph(MGraph *G) {int i, j;G->numEdges=15;G->numVertexes=9;/* 讀入頂點信息,建立頂點表 */ G->vexs[0]='A';G->vexs[1]='B';G->vexs[2]='C';G->vexs[3]='D';G->vexs[4]='E';G->vexs[5]='F';G->vexs[6]='G';G->vexs[7]='H';G->vexs[8]='I';for (i = 0; i < G->numVertexes; i++)/* 初始化圖 */{for ( j = 0; j < G->numVertexes; j++){G->arc[i][j]=0;}}G->arc[0][1]=1;G->arc[0][5]=1;G->arc[1][2]=1; G->arc[1][8]=1; G->arc[1][6]=1; G->arc[2][3]=1; G->arc[2][8]=1; G->arc[3][4]=1;G->arc[3][7]=1;G->arc[3][6]=1;G->arc[3][8]=1;G->arc[4][5]=1;G->arc[4][7]=1;G->arc[5][6]=1; G->arc[6][7]=1; for(i = 0; i < G->numVertexes; i++){for(j = i; j < G->numVertexes; j++){G->arc[j][i] =G->arc[i][j];}}}/* 利用鄰接矩陣構建鄰接表 */ void CreateALGraph(MGraph G,GraphAdjList *GL) {int i,j;EdgeNode *e;*GL = (GraphAdjList)malloc(sizeof(graphAdjList));(*GL)->numVertexes=G.numVertexes;(*GL)->numEdges=G.numEdges;for(i= 0;i <G.numVertexes;i++) /* 讀入頂點信息,建立頂點表 */ {(*GL)->adjList[i].in=0;(*GL)->adjList[i].data=G.vexs[i];(*GL)->adjList[i].firstedge=NULL; /* 將邊表置為空表 */}for(i=0;i<G.numVertexes;i++) /* 建立邊表 */{ for(j=0;j<G.numVertexes;j++){if (G.arc[i][j]==1){e=(EdgeNode *)malloc(sizeof(EdgeNode));e->adjvex=j; /* 鄰接序號為j */ e->next=(*GL)->adjList[i].firstedge; /* 將當前頂點上的指向的結點指針賦值給e */(*GL)->adjList[i].firstedge=e; /* 將當前頂點的指針指向e */ (*GL)->adjList[j].in++;}}}}Boolean visited[MAXSIZE]; /* 訪問標志的數組 *//* 鄰接表的深度優先遞歸算法 */ void DFS(GraphAdjList GL, int i) {EdgeNode *p;visited[i] = TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */p = GL->adjList[i].firstedge;while(p){if(!visited[p->adjvex])DFS(GL, p->adjvex);/* 對為訪問的鄰接頂點遞歸調用 */p = p->next;} }/* 鄰接表的深度遍歷操作 */ void DFSTraverse(GraphAdjList GL) {int i;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE; /* 初始所有頂點狀態都是未訪問過狀態 */for(i = 0; i < GL->numVertexes; i++)if(!visited[i]) /* 對未訪問過的頂點調用DFS,若是連通圖,只會執行一次 */ DFS(GL, i); }/* 鄰接表的廣度遍歷算法 */ void BFSTraverse(GraphAdjList GL) {int i;EdgeNode *p;Queue Q;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE;InitQueue(&Q);for(i = 0; i < GL->numVertexes; i++){if (!visited[i]){visited[i]=TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */EnQueue(&Q,i);while(!QueueEmpty(Q)){DeQueue(&Q,&i);p = GL->adjList[i].firstedge; /* 找到當前頂點的邊表鏈表頭指針 */while(p){if(!visited[p->adjvex]) /* 若此頂點未被訪問 */{visited[p->adjvex]=TRUE;printf("%c ",GL->adjList[p->adjvex].data);EnQueue(&Q,p->adjvex); /* 將此頂點入隊列 */}p = p->next; /* 指針指向下一個鄰接點 */}}}} }int main(void) { MGraph G; GraphAdjList GL; CreateMGraph(&G);CreateALGraph(G,&GL);printf("\n深度遍歷:");DFSTraverse(GL);printf("\n廣度遍歷:");BFSTraverse(GL);return 0; } #include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h"#define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0#define MAXSIZE 9 /* 存儲空間初始分配量 */ #define MAXEDGE 15 #define MAXVEX 9 #define INFINITY 65535typedef int Status; /* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */ typedef int Boolean; /* Boolean是布爾類型,其值是TRUE或FALSE */typedef char VertexType; /* 頂點類型應由用戶定義 */ typedef int EdgeType; /* 邊上的權值類型應由用戶定義 *//* 鄰接矩陣結構 */ typedef struct {VertexType vexs[MAXVEX]; /* 頂點表 */EdgeType arc[MAXVEX][MAXVEX];/* 鄰接矩陣,可看作邊表 */int numVertexes, numEdges; /* 圖中當前的頂點數和邊數 */ }MGraph;/* 鄰接表結構****************** */ typedef struct EdgeNode /* 邊表結點 */ {int adjvex; /* 鄰接點域,存儲該頂點對應的下標 */int weight; /* 用于存儲權值,對于非網圖可以不需要 */struct EdgeNode *next; /* 鏈域,指向下一個鄰接點 */ }EdgeNode;typedef struct VertexNode /* 頂點表結點 */ {int in; /* 頂點入度 */char data; /* 頂點域,存儲頂點信息 */EdgeNode *firstedge;/* 邊表頭指針 */ }VertexNode, AdjList[MAXVEX];typedef struct {AdjList adjList; int numVertexes,numEdges; /* 圖中當前頂點數和邊數 */ }graphAdjList,*GraphAdjList; /* **************************** *//* 用到的隊列結構與函數********************************** */ /* 循環隊列的順序存儲結構 */ typedef struct {int data[MAXSIZE];int front; /* 頭指針 */int rear; /* 尾指針,若隊列不空,指向隊列尾元素的下一個位置 */ }Queue;/* 初始化一個空隊列Q */ Status InitQueue(Queue *Q) {Q->front=0;Q->rear=0;return OK; }/* 若隊列Q為空隊列,則返回TRUE,否則返回FALSE */ Status QueueEmpty(Queue Q) { if(Q.front==Q.rear) /* 隊列空的標志 */return TRUE;elsereturn FALSE; }/* 若隊列未滿,則插入元素e為Q新的隊尾元素 */ Status EnQueue(Queue *Q,int e) {if ((Q->rear+1)%MAXSIZE == Q->front) /* 隊列滿的判斷 */return ERROR;Q->data[Q->rear]=e; /* 將元素e賦值給隊尾 */Q->rear=(Q->rear+1)%MAXSIZE;/* rear指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; }/* 若隊列不空,則刪除Q中隊頭元素,用e返回其值 */ Status DeQueue(Queue *Q,int *e) {if (Q->front == Q->rear) /* 隊列空的判斷 */return ERROR;*e=Q->data[Q->front]; /* 將隊頭元素賦值給e */Q->front=(Q->front+1)%MAXSIZE; /* front指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; } /* ****************************************************** */void CreateMGraph(MGraph *G) {int i, j;G->numEdges=15;G->numVertexes=9;/* 讀入頂點信息,建立頂點表 */ G->vexs[0]='A';G->vexs[1]='B';G->vexs[2]='C';G->vexs[3]='D';G->vexs[4]='E';G->vexs[5]='F';G->vexs[6]='G';G->vexs[7]='H';G->vexs[8]='I';for (i = 0; i < G->numVertexes; i++)/* 初始化圖 */{for ( j = 0; j < G->numVertexes; j++){G->arc[i][j]=0;}}G->arc[0][1]=1;G->arc[0][5]=1;G->arc[1][2]=1; G->arc[1][8]=1; G->arc[1][6]=1; G->arc[2][3]=1; G->arc[2][8]=1; G->arc[3][4]=1;G->arc[3][7]=1;G->arc[3][6]=1;G->arc[3][8]=1;G->arc[4][5]=1;G->arc[4][7]=1;G->arc[5][6]=1; G->arc[6][7]=1; for(i = 0; i < G->numVertexes; i++){for(j = i; j < G->numVertexes; j++){G->arc[j][i] =G->arc[i][j];}}}/* 利用鄰接矩陣構建鄰接表 */ void CreateALGraph(MGraph G,GraphAdjList *GL) {int i,j;EdgeNode *e;*GL = (GraphAdjList)malloc(sizeof(graphAdjList));(*GL)->numVertexes=G.numVertexes;(*GL)->numEdges=G.numEdges;for(i= 0;i <G.numVertexes;i++) /* 讀入頂點信息,建立頂點表 */ {(*GL)->adjList[i].in=0;(*GL)->adjList[i].data=G.vexs[i];(*GL)->adjList[i].firstedge=NULL; /* 將邊表置為空表 */}for(i=0;i<G.numVertexes;i++) /* 建立邊表 */{ for(j=0;j<G.numVertexes;j++){if (G.arc[i][j]==1){e=(EdgeNode *)malloc(sizeof(EdgeNode));e->adjvex=j; /* 鄰接序號為j */ e->next=(*GL)->adjList[i].firstedge; /* 將當前頂點上的指向的結點指針賦值給e */(*GL)->adjList[i].firstedge=e; /* 將當前頂點的指針指向e */ (*GL)->adjList[j].in++;}}}}Boolean visited[MAXSIZE]; /* 訪問標志的數組 *//* 鄰接表的深度優先遞歸算法 */ void DFS(GraphAdjList GL, int i) {EdgeNode *p;visited[i] = TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */p = GL->adjList[i].firstedge;while(p){if(!visited[p->adjvex])DFS(GL, p->adjvex);/* 對為訪問的鄰接頂點遞歸調用 */p = p->next;} }/* 鄰接表的深度遍歷操作 */ void DFSTraverse(GraphAdjList GL) {int i;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE; /* 初始所有頂點狀態都是未訪問過狀態 */for(i = 0; i < GL->numVertexes; i++)if(!visited[i]) /* 對未訪問過的頂點調用DFS,若是連通圖,只會執行一次 */ DFS(GL, i); }/* 鄰接表的廣度遍歷算法 */ void BFSTraverse(GraphAdjList GL) {int i;EdgeNode *p;Queue Q;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE;InitQueue(&Q);for(i = 0; i < GL->numVertexes; i++){if (!visited[i]){visited[i]=TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */EnQueue(&Q,i);while(!QueueEmpty(Q)){DeQueue(&Q,&i);p = GL->adjList[i].firstedge; /* 找到當前頂點的邊表鏈表頭指針 */while(p){if(!visited[p->adjvex]) /* 若此頂點未被訪問 */{visited[p->adjvex]=TRUE;printf("%c ",GL->adjList[p->adjvex].data);EnQueue(&Q,p->adjvex); /* 將此頂點入隊列 */}p = p->next; /* 指針指向下一個鄰接點 */}}}} }int main(void) { MGraph G; GraphAdjList GL; CreateMGraph(&G);CreateALGraph(G,&GL);printf("\n深度遍歷:");DFSTraverse(GL);printf("\n廣度遍歷:");BFSTraverse(GL);return 0; } #include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h"#define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0#define MAXSIZE 9 /* 存儲空間初始分配量 */ #define MAXEDGE 15 #define MAXVEX 9 #define INFINITY 65535typedef int Status; /* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */ typedef int Boolean; /* Boolean是布爾類型,其值是TRUE或FALSE */typedef char VertexType; /* 頂點類型應由用戶定義 */ typedef int EdgeType; /* 邊上的權值類型應由用戶定義 *//* 鄰接矩陣結構 */ typedef struct {VertexType vexs[MAXVEX]; /* 頂點表 */EdgeType arc[MAXVEX][MAXVEX];/* 鄰接矩陣,可看作邊表 */int numVertexes, numEdges; /* 圖中當前的頂點數和邊數 */ }MGraph;/* 鄰接表結構****************** */ typedef struct EdgeNode /* 邊表結點 */ {int adjvex; /* 鄰接點域,存儲該頂點對應的下標 */int weight; /* 用于存儲權值,對于非網圖可以不需要 */struct EdgeNode *next; /* 鏈域,指向下一個鄰接點 */ }EdgeNode;typedef struct VertexNode /* 頂點表結點 */ {int in; /* 頂點入度 */char data; /* 頂點域,存儲頂點信息 */EdgeNode *firstedge;/* 邊表頭指針 */ }VertexNode, AdjList[MAXVEX];typedef struct {AdjList adjList; int numVertexes,numEdges; /* 圖中當前頂點數和邊數 */ }graphAdjList,*GraphAdjList; /* **************************** *//* 用到的隊列結構與函數********************************** */ /* 循環隊列的順序存儲結構 */ typedef struct {int data[MAXSIZE];int front; /* 頭指針 */int rear; /* 尾指針,若隊列不空,指向隊列尾元素的下一個位置 */ }Queue;/* 初始化一個空隊列Q */ Status InitQueue(Queue *Q) {Q->front=0;Q->rear=0;return OK; }/* 若隊列Q為空隊列,則返回TRUE,否則返回FALSE */ Status QueueEmpty(Queue Q) { if(Q.front==Q.rear) /* 隊列空的標志 */return TRUE;elsereturn FALSE; }/* 若隊列未滿,則插入元素e為Q新的隊尾元素 */ Status EnQueue(Queue *Q,int e) {if ((Q->rear+1)%MAXSIZE == Q->front) /* 隊列滿的判斷 */return ERROR;Q->data[Q->rear]=e; /* 將元素e賦值給隊尾 */Q->rear=(Q->rear+1)%MAXSIZE;/* rear指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; }/* 若隊列不空,則刪除Q中隊頭元素,用e返回其值 */ Status DeQueue(Queue *Q,int *e) {if (Q->front == Q->rear) /* 隊列空的判斷 */return ERROR;*e=Q->data[Q->front]; /* 將隊頭元素賦值給e */Q->front=(Q->front+1)%MAXSIZE; /* front指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; } /* ****************************************************** */void CreateMGraph(MGraph *G) {int i, j;G->numEdges=15;G->numVertexes=9;/* 讀入頂點信息,建立頂點表 */ G->vexs[0]='A';G->vexs[1]='B';G->vexs[2]='C';G->vexs[3]='D';G->vexs[4]='E';G->vexs[5]='F';G->vexs[6]='G';G->vexs[7]='H';G->vexs[8]='I';for (i = 0; i < G->numVertexes; i++)/* 初始化圖 */{for ( j = 0; j < G->numVertexes; j++){G->arc[i][j]=0;}}G->arc[0][1]=1;G->arc[0][5]=1;G->arc[1][2]=1; G->arc[1][8]=1; G->arc[1][6]=1; G->arc[2][3]=1; G->arc[2][8]=1; G->arc[3][4]=1;G->arc[3][7]=1;G->arc[3][6]=1;G->arc[3][8]=1;G->arc[4][5]=1;G->arc[4][7]=1;G->arc[5][6]=1; G->arc[6][7]=1; for(i = 0; i < G->numVertexes; i++){for(j = i; j < G->numVertexes; j++){G->arc[j][i] =G->arc[i][j];}}}/* 利用鄰接矩陣構建鄰接表 */ void CreateALGraph(MGraph G,GraphAdjList *GL) {int i,j;EdgeNode *e;*GL = (GraphAdjList)malloc(sizeof(graphAdjList));(*GL)->numVertexes=G.numVertexes;(*GL)->numEdges=G.numEdges;for(i= 0;i <G.numVertexes;i++) /* 讀入頂點信息,建立頂點表 */ {(*GL)->adjList[i].in=0;(*GL)->adjList[i].data=G.vexs[i];(*GL)->adjList[i].firstedge=NULL; /* 將邊表置為空表 */}for(i=0;i<G.numVertexes;i++) /* 建立邊表 */{ for(j=0;j<G.numVertexes;j++){if (G.arc[i][j]==1){e=(EdgeNode *)malloc(sizeof(EdgeNode));e->adjvex=j; /* 鄰接序號為j */ e->next=(*GL)->adjList[i].firstedge; /* 將當前頂點上的指向的結點指針賦值給e */(*GL)->adjList[i].firstedge=e; /* 將當前頂點的指針指向e */ (*GL)->adjList[j].in++;}}}}Boolean visited[MAXSIZE]; /* 訪問標志的數組 *//* 鄰接表的深度優先遞歸算法 */ void DFS(GraphAdjList GL, int i) {EdgeNode *p;visited[i] = TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */p = GL->adjList[i].firstedge;while(p){if(!visited[p->adjvex])DFS(GL, p->adjvex);/* 對為訪問的鄰接頂點遞歸調用 */p = p->next;} }/* 鄰接表的深度遍歷操作 */ void DFSTraverse(GraphAdjList GL) {int i;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE; /* 初始所有頂點狀態都是未訪問過狀態 */for(i = 0; i < GL->numVertexes; i++)if(!visited[i]) /* 對未訪問過的頂點調用DFS,若是連通圖,只會執行一次 */ DFS(GL, i); }/* 鄰接表的廣度遍歷算法 */ void BFSTraverse(GraphAdjList GL) {int i;EdgeNode *p;Queue Q;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE;InitQueue(&Q);for(i = 0; i < GL->numVertexes; i++){if (!visited[i]){visited[i]=TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */EnQueue(&Q,i);while(!QueueEmpty(Q)){DeQueue(&Q,&i);p = GL->adjList[i].firstedge; /* 找到當前頂點的邊表鏈表頭指針 */while(p){if(!visited[p->adjvex]) /* 若此頂點未被訪問 */{visited[p->adjvex]=TRUE;printf("%c ",GL->adjList[p->adjvex].data);EnQueue(&Q,p->adjvex); /* 將此頂點入隊列 */}p = p->next; /* 指針指向下一個鄰接點 */}}}} }int main(void) { MGraph G; GraphAdjList GL; CreateMGraph(&G);CreateALGraph(G,&GL);printf("\n深度遍歷:");DFSTraverse(GL);printf("\n廣度遍歷:");BFSTraverse(GL);return 0; } #include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h"#define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0#define MAXSIZE 9 /* 存儲空間初始分配量 */ #define MAXEDGE 15 #define MAXVEX 9 #define INFINITY 65535typedef int Status; /* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */ typedef int Boolean; /* Boolean是布爾類型,其值是TRUE或FALSE */typedef char VertexType; /* 頂點類型應由用戶定義 */ typedef int EdgeType; /* 邊上的權值類型應由用戶定義 *//* 鄰接矩陣結構 */ typedef struct {VertexType vexs[MAXVEX]; /* 頂點表 */EdgeType arc[MAXVEX][MAXVEX];/* 鄰接矩陣,可看作邊表 */int numVertexes, numEdges; /* 圖中當前的頂點數和邊數 */ }MGraph;/* 鄰接表結構****************** */ typedef struct EdgeNode /* 邊表結點 */ {int adjvex; /* 鄰接點域,存儲該頂點對應的下標 */int weight; /* 用于存儲權值,對于非網圖可以不需要 */struct EdgeNode *next; /* 鏈域,指向下一個鄰接點 */ }EdgeNode;typedef struct VertexNode /* 頂點表結點 */ {int in; /* 頂點入度 */char data; /* 頂點域,存儲頂點信息 */EdgeNode *firstedge;/* 邊表頭指針 */ }VertexNode, AdjList[MAXVEX];typedef struct {AdjList adjList; int numVertexes,numEdges; /* 圖中當前頂點數和邊數 */ }graphAdjList,*GraphAdjList; /* **************************** *//* 用到的隊列結構與函數********************************** */ /* 循環隊列的順序存儲結構 */ typedef struct {int data[MAXSIZE];int front; /* 頭指針 */int rear; /* 尾指針,若隊列不空,指向隊列尾元素的下一個位置 */ }Queue;/* 初始化一個空隊列Q */ Status InitQueue(Queue *Q) {Q->front=0;Q->rear=0;return OK; }/* 若隊列Q為空隊列,則返回TRUE,否則返回FALSE */ Status QueueEmpty(Queue Q) { if(Q.front==Q.rear) /* 隊列空的標志 */return TRUE;elsereturn FALSE; }/* 若隊列未滿,則插入元素e為Q新的隊尾元素 */ Status EnQueue(Queue *Q,int e) {if ((Q->rear+1)%MAXSIZE == Q->front) /* 隊列滿的判斷 */return ERROR;Q->data[Q->rear]=e; /* 將元素e賦值給隊尾 */Q->rear=(Q->rear+1)%MAXSIZE;/* rear指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; }/* 若隊列不空,則刪除Q中隊頭元素,用e返回其值 */ Status DeQueue(Queue *Q,int *e) {if (Q->front == Q->rear) /* 隊列空的判斷 */return ERROR;*e=Q->data[Q->front]; /* 將隊頭元素賦值給e */Q->front=(Q->front+1)%MAXSIZE; /* front指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; } /* ****************************************************** */void CreateMGraph(MGraph *G) {int i, j;G->numEdges=15;G->numVertexes=9;/* 讀入頂點信息,建立頂點表 */ G->vexs[0]='A';G->vexs[1]='B';G->vexs[2]='C';G->vexs[3]='D';G->vexs[4]='E';G->vexs[5]='F';G->vexs[6]='G';G->vexs[7]='H';G->vexs[8]='I';for (i = 0; i < G->numVertexes; i++)/* 初始化圖 */{for ( j = 0; j < G->numVertexes; j++){G->arc[i][j]=0;}}G->arc[0][1]=1;G->arc[0][5]=1;G->arc[1][2]=1; G->arc[1][8]=1; G->arc[1][6]=1; G->arc[2][3]=1; G->arc[2][8]=1; G->arc[3][4]=1;G->arc[3][7]=1;G->arc[3][6]=1;G->arc[3][8]=1;G->arc[4][5]=1;G->arc[4][7]=1;G->arc[5][6]=1; G->arc[6][7]=1; for(i = 0; i < G->numVertexes; i++){for(j = i; j < G->numVertexes; j++){G->arc[j][i] =G->arc[i][j];}}}/* 利用鄰接矩陣構建鄰接表 */ void CreateALGraph(MGraph G,GraphAdjList *GL) {int i,j;EdgeNode *e;*GL = (GraphAdjList)malloc(sizeof(graphAdjList));(*GL)->numVertexes=G.numVertexes;(*GL)->numEdges=G.numEdges;for(i= 0;i <G.numVertexes;i++) /* 讀入頂點信息,建立頂點表 */ {(*GL)->adjList[i].in=0;(*GL)->adjList[i].data=G.vexs[i];(*GL)->adjList[i].firstedge=NULL; /* 將邊表置為空表 */}for(i=0;i<G.numVertexes;i++) /* 建立邊表 */{ for(j=0;j<G.numVertexes;j++){if (G.arc[i][j]==1){e=(EdgeNode *)malloc(sizeof(EdgeNode));e->adjvex=j; /* 鄰接序號為j */ e->next=(*GL)->adjList[i].firstedge; /* 將當前頂點上的指向的結點指針賦值給e */(*GL)->adjList[i].firstedge=e; /* 將當前頂點的指針指向e */ (*GL)->adjList[j].in++;}}}}Boolean visited[MAXSIZE]; /* 訪問標志的數組 *//* 鄰接表的深度優先遞歸算法 */ void DFS(GraphAdjList GL, int i) {EdgeNode *p;visited[i] = TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */p = GL->adjList[i].firstedge;while(p){if(!visited[p->adjvex])DFS(GL, p->adjvex);/* 對為訪問的鄰接頂點遞歸調用 */p = p->next;} }/* 鄰接表的深度遍歷操作 */ void DFSTraverse(GraphAdjList GL) {int i;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE; /* 初始所有頂點狀態都是未訪問過狀態 */for(i = 0; i < GL->numVertexes; i++)if(!visited[i]) /* 對未訪問過的頂點調用DFS,若是連通圖,只會執行一次 */ DFS(GL, i); }/* 鄰接表的廣度遍歷算法 */ void BFSTraverse(GraphAdjList GL) {int i;EdgeNode *p;Queue Q;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE;InitQueue(&Q);for(i = 0; i < GL->numVertexes; i++){if (!visited[i]){visited[i]=TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */EnQueue(&Q,i);while(!QueueEmpty(Q)){DeQueue(&Q,&i);p = GL->adjList[i].firstedge; /* 找到當前頂點的邊表鏈表頭指針 */while(p){if(!visited[p->adjvex]) /* 若此頂點未被訪問 */{visited[p->adjvex]=TRUE;printf("%c ",GL->adjList[p->adjvex].data);EnQueue(&Q,p->adjvex); /* 將此頂點入隊列 */}p = p->next; /* 指針指向下一個鄰接點 */}}}} }int main(void) { MGraph G; GraphAdjList GL; CreateMGraph(&G);CreateALGraph(G,&GL);printf("\n深度遍歷:");DFSTraverse(GL);printf("\n廣度遍歷:");BFSTraverse(GL);return 0; } #include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h"#define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0#define MAXSIZE 9 /* 存儲空間初始分配量 */ #define MAXEDGE 15 #define MAXVEX 9 #define INFINITY 65535typedef int Status; /* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */ typedef int Boolean; /* Boolean是布爾類型,其值是TRUE或FALSE */typedef char VertexType; /* 頂點類型應由用戶定義 */ typedef int EdgeType; /* 邊上的權值類型應由用戶定義 *//* 鄰接矩陣結構 */ typedef struct {VertexType vexs[MAXVEX]; /* 頂點表 */EdgeType arc[MAXVEX][MAXVEX];/* 鄰接矩陣,可看作邊表 */int numVertexes, numEdges; /* 圖中當前的頂點數和邊數 */ }MGraph;/* 鄰接表結構****************** */ typedef struct EdgeNode /* 邊表結點 */ {int adjvex; /* 鄰接點域,存儲該頂點對應的下標 */int weight; /* 用于存儲權值,對于非網圖可以不需要 */struct EdgeNode *next; /* 鏈域,指向下一個鄰接點 */ }EdgeNode;typedef struct VertexNode /* 頂點表結點 */ {int in; /* 頂點入度 */char data; /* 頂點域,存儲頂點信息 */EdgeNode *firstedge;/* 邊表頭指針 */ }VertexNode, AdjList[MAXVEX];typedef struct {AdjList adjList; int numVertexes,numEdges; /* 圖中當前頂點數和邊數 */ }graphAdjList,*GraphAdjList; /* **************************** *//* 用到的隊列結構與函數********************************** */ /* 循環隊列的順序存儲結構 */ typedef struct {int data[MAXSIZE];int front; /* 頭指針 */int rear; /* 尾指針,若隊列不空,指向隊列尾元素的下一個位置 */ }Queue;/* 初始化一個空隊列Q */ Status InitQueue(Queue *Q) {Q->front=0;Q->rear=0;return OK; }/* 若隊列Q為空隊列,則返回TRUE,否則返回FALSE */ Status QueueEmpty(Queue Q) { if(Q.front==Q.rear) /* 隊列空的標志 */return TRUE;elsereturn FALSE; }/* 若隊列未滿,則插入元素e為Q新的隊尾元素 */ Status EnQueue(Queue *Q,int e) {if ((Q->rear+1)%MAXSIZE == Q->front) /* 隊列滿的判斷 */return ERROR;Q->data[Q->rear]=e; /* 將元素e賦值給隊尾 */Q->rear=(Q->rear+1)%MAXSIZE;/* rear指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; }/* 若隊列不空,則刪除Q中隊頭元素,用e返回其值 */ Status DeQueue(Queue *Q,int *e) {if (Q->front == Q->rear) /* 隊列空的判斷 */return ERROR;*e=Q->data[Q->front]; /* 將隊頭元素賦值給e */Q->front=(Q->front+1)%MAXSIZE; /* front指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; } /* ****************************************************** */void CreateMGraph(MGraph *G) {int i, j;G->numEdges=15;G->numVertexes=9;/* 讀入頂點信息,建立頂點表 */ G->vexs[0]='A';G->vexs[1]='B';G->vexs[2]='C';G->vexs[3]='D';G->vexs[4]='E';G->vexs[5]='F';G->vexs[6]='G';G->vexs[7]='H';G->vexs[8]='I';for (i = 0; i < G->numVertexes; i++)/* 初始化圖 */{for ( j = 0; j < G->numVertexes; j++){G->arc[i][j]=0;}}G->arc[0][1]=1;G->arc[0][5]=1;G->arc[1][2]=1; G->arc[1][8]=1; G->arc[1][6]=1; G->arc[2][3]=1; G->arc[2][8]=1; G->arc[3][4]=1;G->arc[3][7]=1;G->arc[3][6]=1;G->arc[3][8]=1;G->arc[4][5]=1;G->arc[4][7]=1;G->arc[5][6]=1; G->arc[6][7]=1; for(i = 0; i < G->numVertexes; i++){for(j = i; j < G->numVertexes; j++){G->arc[j][i] =G->arc[i][j];}}}/* 利用鄰接矩陣構建鄰接表 */ void CreateALGraph(MGraph G,GraphAdjList *GL) {int i,j;EdgeNode *e;*GL = (GraphAdjList)malloc(sizeof(graphAdjList));(*GL)->numVertexes=G.numVertexes;(*GL)->numEdges=G.numEdges;for(i= 0;i <G.numVertexes;i++) /* 讀入頂點信息,建立頂點表 */ {(*GL)->adjList[i].in=0;(*GL)->adjList[i].data=G.vexs[i];(*GL)->adjList[i].firstedge=NULL; /* 將邊表置為空表 */}for(i=0;i<G.numVertexes;i++) /* 建立邊表 */{ for(j=0;j<G.numVertexes;j++){if (G.arc[i][j]==1){e=(EdgeNode *)malloc(sizeof(EdgeNode));e->adjvex=j; /* 鄰接序號為j */ e->next=(*GL)->adjList[i].firstedge; /* 將當前頂點上的指向的結點指針賦值給e */(*GL)->adjList[i].firstedge=e; /* 將當前頂點的指針指向e */ (*GL)->adjList[j].in++;}}}}Boolean visited[MAXSIZE]; /* 訪問標志的數組 *//* 鄰接表的深度優先遞歸算法 */ void DFS(GraphAdjList GL, int i) {EdgeNode *p;visited[i] = TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */p = GL->adjList[i].firstedge;while(p){if(!visited[p->adjvex])DFS(GL, p->adjvex);/* 對為訪問的鄰接頂點遞歸調用 */p = p->next;} }/* 鄰接表的深度遍歷操作 */ void DFSTraverse(GraphAdjList GL) {int i;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE; /* 初始所有頂點狀態都是未訪問過狀態 */for(i = 0; i < GL->numVertexes; i++)if(!visited[i]) /* 對未訪問過的頂點調用DFS,若是連通圖,只會執行一次 */ DFS(GL, i); }/* 鄰接表的廣度遍歷算法 */ void BFSTraverse(GraphAdjList GL) {int i;EdgeNode *p;Queue Q;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE;InitQueue(&Q);for(i = 0; i < GL->numVertexes; i++){if (!visited[i]){visited[i]=TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */EnQueue(&Q,i);while(!QueueEmpty(Q)){DeQueue(&Q,&i);p = GL->adjList[i].firstedge; /* 找到當前頂點的邊表鏈表頭指針 */while(p){if(!visited[p->adjvex]) /* 若此頂點未被訪問 */{visited[p->adjvex]=TRUE;printf("%c ",GL->adjList[p->adjvex].data);EnQueue(&Q,p->adjvex); /* 將此頂點入隊列 */}p = p->next; /* 指針指向下一個鄰接點 */}}}} }int main(void) { MGraph G; GraphAdjList GL; CreateMGraph(&G);CreateALGraph(G,&GL);printf("\n深度遍歷:");DFSTraverse(GL);printf("\n廣度遍歷:");BFSTraverse(GL);return 0; } #include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h"#define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0#define MAXSIZE 9 /* 存儲空間初始分配量 */ #define MAXEDGE 15 #define MAXVEX 9 #define INFINITY 65535typedef int Status; /* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */ typedef int Boolean; /* Boolean是布爾類型,其值是TRUE或FALSE */typedef char VertexType; /* 頂點類型應由用戶定義 */ typedef int EdgeType; /* 邊上的權值類型應由用戶定義 *//* 鄰接矩陣結構 */ typedef struct {VertexType vexs[MAXVEX]; /* 頂點表 */EdgeType arc[MAXVEX][MAXVEX];/* 鄰接矩陣,可看作邊表 */int numVertexes, numEdges; /* 圖中當前的頂點數和邊數 */ }MGraph;/* 鄰接表結構****************** */ typedef struct EdgeNode /* 邊表結點 */ {int adjvex; /* 鄰接點域,存儲該頂點對應的下標 */int weight; /* 用于存儲權值,對于非網圖可以不需要 */struct EdgeNode *next; /* 鏈域,指向下一個鄰接點 */ }EdgeNode;typedef struct VertexNode /* 頂點表結點 */ {int in; /* 頂點入度 */char data; /* 頂點域,存儲頂點信息 */EdgeNode *firstedge;/* 邊表頭指針 */ }VertexNode, AdjList[MAXVEX];typedef struct {AdjList adjList; int numVertexes,numEdges; /* 圖中當前頂點數和邊數 */ }graphAdjList,*GraphAdjList; /* **************************** *//* 用到的隊列結構與函數********************************** */ /* 循環隊列的順序存儲結構 */ typedef struct {int data[MAXSIZE];int front; /* 頭指針 */int rear; /* 尾指針,若隊列不空,指向隊列尾元素的下一個位置 */ }Queue;/* 初始化一個空隊列Q */ Status InitQueue(Queue *Q) {Q->front=0;Q->rear=0;return OK; }/* 若隊列Q為空隊列,則返回TRUE,否則返回FALSE */ Status QueueEmpty(Queue Q) { if(Q.front==Q.rear) /* 隊列空的標志 */return TRUE;elsereturn FALSE; }/* 若隊列未滿,則插入元素e為Q新的隊尾元素 */ Status EnQueue(Queue *Q,int e) {if ((Q->rear+1)%MAXSIZE == Q->front) /* 隊列滿的判斷 */return ERROR;Q->data[Q->rear]=e; /* 將元素e賦值給隊尾 */Q->rear=(Q->rear+1)%MAXSIZE;/* rear指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; }/* 若隊列不空,則刪除Q中隊頭元素,用e返回其值 */ Status DeQueue(Queue *Q,int *e) {if (Q->front == Q->rear) /* 隊列空的判斷 */return ERROR;*e=Q->data[Q->front]; /* 將隊頭元素賦值給e */Q->front=(Q->front+1)%MAXSIZE; /* front指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; } /* ****************************************************** */void CreateMGraph(MGraph *G) {int i, j;G->numEdges=15;G->numVertexes=9;/* 讀入頂點信息,建立頂點表 */ G->vexs[0]='A';G->vexs[1]='B';G->vexs[2]='C';G->vexs[3]='D';G->vexs[4]='E';G->vexs[5]='F';G->vexs[6]='G';G->vexs[7]='H';G->vexs[8]='I';for (i = 0; i < G->numVertexes; i++)/* 初始化圖 */{for ( j = 0; j < G->numVertexes; j++){G->arc[i][j]=0;}}G->arc[0][1]=1;G->arc[0][5]=1;G->arc[1][2]=1; G->arc[1][8]=1; G->arc[1][6]=1; G->arc[2][3]=1; G->arc[2][8]=1; G->arc[3][4]=1;G->arc[3][7]=1;G->arc[3][6]=1;G->arc[3][8]=1;G->arc[4][5]=1;G->arc[4][7]=1;G->arc[5][6]=1; G->arc[6][7]=1; for(i = 0; i < G->numVertexes; i++){for(j = i; j < G->numVertexes; j++){G->arc[j][i] =G->arc[i][j];}}}/* 利用鄰接矩陣構建鄰接表 */ void CreateALGraph(MGraph G,GraphAdjList *GL) {int i,j;EdgeNode *e;*GL = (GraphAdjList)malloc(sizeof(graphAdjList));(*GL)->numVertexes=G.numVertexes;(*GL)->numEdges=G.numEdges;for(i= 0;i <G.numVertexes;i++) /* 讀入頂點信息,建立頂點表 */ {(*GL)->adjList[i].in=0;(*GL)->adjList[i].data=G.vexs[i];(*GL)->adjList[i].firstedge=NULL; /* 將邊表置為空表 */}for(i=0;i<G.numVertexes;i++) /* 建立邊表 */{ for(j=0;j<G.numVertexes;j++){if (G.arc[i][j]==1){e=(EdgeNode *)malloc(sizeof(EdgeNode));e->adjvex=j; /* 鄰接序號為j */ e->next=(*GL)->adjList[i].firstedge; /* 將當前頂點上的指向的結點指針賦值給e */(*GL)->adjList[i].firstedge=e; /* 將當前頂點的指針指向e */ (*GL)->adjList[j].in++;}}}}Boolean visited[MAXSIZE]; /* 訪問標志的數組 *//* 鄰接表的深度優先遞歸算法 */ void DFS(GraphAdjList GL, int i) {EdgeNode *p;visited[i] = TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */p = GL->adjList[i].firstedge;while(p){if(!visited[p->adjvex])DFS(GL, p->adjvex);/* 對為訪問的鄰接頂點遞歸調用 */p = p->next;} }/* 鄰接表的深度遍歷操作 */ void DFSTraverse(GraphAdjList GL) {int i;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE; /* 初始所有頂點狀態都是未訪問過狀態 */for(i = 0; i < GL->numVertexes; i++)if(!visited[i]) /* 對未訪問過的頂點調用DFS,若是連通圖,只會執行一次 */ DFS(GL, i); }/* 鄰接表的廣度遍歷算法 */ void BFSTraverse(GraphAdjList GL) {int i;EdgeNode *p;Queue Q;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE;InitQueue(&Q);for(i = 0; i < GL->numVertexes; i++){if (!visited[i]){visited[i]=TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */EnQueue(&Q,i);while(!QueueEmpty(Q)){DeQueue(&Q,&i);p = GL->adjList[i].firstedge; /* 找到當前頂點的邊表鏈表頭指針 */while(p){if(!visited[p->adjvex]) /* 若此頂點未被訪問 */{visited[p->adjvex]=TRUE;printf("%c ",GL->adjList[p->adjvex].data);EnQueue(&Q,p->adjvex); /* 將此頂點入隊列 */}p = p->next; /* 指針指向下一個鄰接點 */}}}} }int main(void) { MGraph G; GraphAdjList GL; CreateMGraph(&G);CreateALGraph(G,&GL);printf("\n深度遍歷:");DFSTraverse(GL);printf("\n廣度遍歷:");BFSTraverse(GL);return 0; } #include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h"#define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0#define MAXSIZE 9 /* 存儲空間初始分配量 */ #define MAXEDGE 15 #define MAXVEX 9 #define INFINITY 65535typedef int Status; /* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */ typedef int Boolean; /* Boolean是布爾類型,其值是TRUE或FALSE */typedef char VertexType; /* 頂點類型應由用戶定義 */ typedef int EdgeType; /* 邊上的權值類型應由用戶定義 *//* 鄰接矩陣結構 */ typedef struct {VertexType vexs[MAXVEX]; /* 頂點表 */EdgeType arc[MAXVEX][MAXVEX];/* 鄰接矩陣,可看作邊表 */int numVertexes, numEdges; /* 圖中當前的頂點數和邊數 */ }MGraph;/* 鄰接表結構****************** */ typedef struct EdgeNode /* 邊表結點 */ {int adjvex; /* 鄰接點域,存儲該頂點對應的下標 */int weight; /* 用于存儲權值,對于非網圖可以不需要 */struct EdgeNode *next; /* 鏈域,指向下一個鄰接點 */ }EdgeNode;typedef struct VertexNode /* 頂點表結點 */ {int in; /* 頂點入度 */char data; /* 頂點域,存儲頂點信息 */EdgeNode *firstedge;/* 邊表頭指針 */ }VertexNode, AdjList[MAXVEX];typedef struct {AdjList adjList; int numVertexes,numEdges; /* 圖中當前頂點數和邊數 */ }graphAdjList,*GraphAdjList; /* **************************** *//* 用到的隊列結構與函數********************************** */ /* 循環隊列的順序存儲結構 */ typedef struct {int data[MAXSIZE];int front; /* 頭指針 */int rear; /* 尾指針,若隊列不空,指向隊列尾元素的下一個位置 */ }Queue;/* 初始化一個空隊列Q */ Status InitQueue(Queue *Q) {Q->front=0;Q->rear=0;return OK; }/* 若隊列Q為空隊列,則返回TRUE,否則返回FALSE */ Status QueueEmpty(Queue Q) { if(Q.front==Q.rear) /* 隊列空的標志 */return TRUE;elsereturn FALSE; }/* 若隊列未滿,則插入元素e為Q新的隊尾元素 */ Status EnQueue(Queue *Q,int e) {if ((Q->rear+1)%MAXSIZE == Q->front) /* 隊列滿的判斷 */return ERROR;Q->data[Q->rear]=e; /* 將元素e賦值給隊尾 */Q->rear=(Q->rear+1)%MAXSIZE;/* rear指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; }/* 若隊列不空,則刪除Q中隊頭元素,用e返回其值 */ Status DeQueue(Queue *Q,int *e) {if (Q->front == Q->rear) /* 隊列空的判斷 */return ERROR;*e=Q->data[Q->front]; /* 將隊頭元素賦值給e */Q->front=(Q->front+1)%MAXSIZE; /* front指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; } /* ****************************************************** */void CreateMGraph(MGraph *G) {int i, j;G->numEdges=15;G->numVertexes=9;/* 讀入頂點信息,建立頂點表 */ G->vexs[0]='A';G->vexs[1]='B';G->vexs[2]='C';G->vexs[3]='D';G->vexs[4]='E';G->vexs[5]='F';G->vexs[6]='G';G->vexs[7]='H';G->vexs[8]='I';for (i = 0; i < G->numVertexes; i++)/* 初始化圖 */{for ( j = 0; j < G->numVertexes; j++){G->arc[i][j]=0;}}G->arc[0][1]=1;G->arc[0][5]=1;G->arc[1][2]=1; G->arc[1][8]=1; G->arc[1][6]=1; G->arc[2][3]=1; G->arc[2][8]=1; G->arc[3][4]=1;G->arc[3][7]=1;G->arc[3][6]=1;G->arc[3][8]=1;G->arc[4][5]=1;G->arc[4][7]=1;G->arc[5][6]=1; G->arc[6][7]=1; for(i = 0; i < G->numVertexes; i++){for(j = i; j < G->numVertexes; j++){G->arc[j][i] =G->arc[i][j];}}}/* 利用鄰接矩陣構建鄰接表 */ void CreateALGraph(MGraph G,GraphAdjList *GL) {int i,j;EdgeNode *e;*GL = (GraphAdjList)malloc(sizeof(graphAdjList));(*GL)->numVertexes=G.numVertexes;(*GL)->numEdges=G.numEdges;for(i= 0;i <G.numVertexes;i++) /* 讀入頂點信息,建立頂點表 */ {(*GL)->adjList[i].in=0;(*GL)->adjList[i].data=G.vexs[i];(*GL)->adjList[i].firstedge=NULL; /* 將邊表置為空表 */}for(i=0;i<G.numVertexes;i++) /* 建立邊表 */{ for(j=0;j<G.numVertexes;j++){if (G.arc[i][j]==1){e=(EdgeNode *)malloc(sizeof(EdgeNode));e->adjvex=j; /* 鄰接序號為j */ e->next=(*GL)->adjList[i].firstedge; /* 將當前頂點上的指向的結點指針賦值給e */(*GL)->adjList[i].firstedge=e; /* 將當前頂點的指針指向e */ (*GL)->adjList[j].in++;}}}}Boolean visited[MAXSIZE]; /* 訪問標志的數組 *//* 鄰接表的深度優先遞歸算法 */ void DFS(GraphAdjList GL, int i) {EdgeNode *p;visited[i] = TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */p = GL->adjList[i].firstedge;while(p){if(!visited[p->adjvex])DFS(GL, p->adjvex);/* 對為訪問的鄰接頂點遞歸調用 */p = p->next;} }/* 鄰接表的深度遍歷操作 */ void DFSTraverse(GraphAdjList GL) {int i;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE; /* 初始所有頂點狀態都是未訪問過狀態 */for(i = 0; i < GL->numVertexes; i++)if(!visited[i]) /* 對未訪問過的頂點調用DFS,若是連通圖,只會執行一次 */ DFS(GL, i); }/* 鄰接表的廣度遍歷算法 */ void BFSTraverse(GraphAdjList GL) {int i;EdgeNode *p;Queue Q;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE;InitQueue(&Q);for(i = 0; i < GL->numVertexes; i++){if (!visited[i]){visited[i]=TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */EnQueue(&Q,i);while(!QueueEmpty(Q)){DeQueue(&Q,&i);p = GL->adjList[i].firstedge; /* 找到當前頂點的邊表鏈表頭指針 */while(p){if(!visited[p->adjvex]) /* 若此頂點未被訪問 */{visited[p->adjvex]=TRUE;printf("%c ",GL->adjList[p->adjvex].data);EnQueue(&Q,p->adjvex); /* 將此頂點入隊列 */}p = p->next; /* 指針指向下一個鄰接點 */}}}} }int main(void) { MGraph G; GraphAdjList GL; CreateMGraph(&G);CreateALGraph(G,&GL);printf("\n深度遍歷:");DFSTraverse(GL);printf("\n廣度遍歷:");BFSTraverse(GL);return 0; }#include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h"#define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0#define MAXSIZE 9 /* 存儲空間初始分配量 */ #define MAXEDGE 15 #define MAXVEX 9 #define INFINITY 65535typedef int Status; /* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */ typedef int Boolean; /* Boolean是布爾類型,其值是TRUE或FALSE */typedef char VertexType; /* 頂點類型應由用戶定義 */ typedef int EdgeType; /* 邊上的權值類型應由用戶定義 *//* 鄰接矩陣結構 */ typedef struct {VertexType vexs[MAXVEX]; /* 頂點表 */EdgeType arc[MAXVEX][MAXVEX];/* 鄰接矩陣,可看作邊表 */int numVertexes, numEdges; /* 圖中當前的頂點數和邊數 */ }MGraph;/* 鄰接表結構****************** */ typedef struct EdgeNode /* 邊表結點 */ {int adjvex; /* 鄰接點域,存儲該頂點對應的下標 */int weight; /* 用于存儲權值,對于非網圖可以不需要 */struct EdgeNode *next; /* 鏈域,指向下一個鄰接點 */ }EdgeNode;typedef struct VertexNode /* 頂點表結點 */ {int in; /* 頂點入度 */char data; /* 頂點域,存儲頂點信息 */EdgeNode *firstedge;/* 邊表頭指針 */ }VertexNode, AdjList[MAXVEX];typedef struct {AdjList adjList; int numVertexes,numEdges; /* 圖中當前頂點數和邊數 */ }graphAdjList,*GraphAdjList; /* **************************** *//* 用到的隊列結構與函數********************************** */ /* 循環隊列的順序存儲結構 */ typedef struct {int data[MAXSIZE];int front; /* 頭指針 */int rear; /* 尾指針,若隊列不空,指向隊列尾元素的下一個位置 */ }Queue;/* 初始化一個空隊列Q */ Status InitQueue(Queue *Q) {Q->front=0;Q->rear=0;return OK; }/* 若隊列Q為空隊列,則返回TRUE,否則返回FALSE */ Status QueueEmpty(Queue Q) { if(Q.front==Q.rear) /* 隊列空的標志 */return TRUE;elsereturn FALSE; }/* 若隊列未滿,則插入元素e為Q新的隊尾元素 */ Status EnQueue(Queue *Q,int e) {if ((Q->rear+1)%MAXSIZE == Q->front) /* 隊列滿的判斷 */return ERROR;Q->data[Q->rear]=e; /* 將元素e賦值給隊尾 */Q->rear=(Q->rear+1)%MAXSIZE;/* rear指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; }/* 若隊列不空,則刪除Q中隊頭元素,用e返回其值 */ Status DeQueue(Queue *Q,int *e) {if (Q->front == Q->rear) /* 隊列空的判斷 */return ERROR;*e=Q->data[Q->front]; /* 將隊頭元素賦值給e */Q->front=(Q->front+1)%MAXSIZE; /* front指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; } /* ****************************************************** */void CreateMGraph(MGraph *G) {int i, j;G->numEdges=15;G->numVertexes=9;/* 讀入頂點信息,建立頂點表 */ G->vexs[0]='A';G->vexs[1]='B';G->vexs[2]='C';G->vexs[3]='D';G->vexs[4]='E';G->vexs[5]='F';G->vexs[6]='G';G->vexs[7]='H';G->vexs[8]='I';for (i = 0; i < G->numVertexes; i++)/* 初始化圖 */{for ( j = 0; j < G->numVertexes; j++){G->arc[i][j]=0;}}G->arc[0][1]=1;G->arc[0][5]=1;G->arc[1][2]=1; G->arc[1][8]=1; G->arc[1][6]=1; G->arc[2][3]=1; G->arc[2][8]=1; G->arc[3][4]=1;G->arc[3][7]=1;G->arc[3][6]=1;G->arc[3][8]=1;G->arc[4][5]=1;G->arc[4][7]=1;G->arc[5][6]=1; G->arc[6][7]=1; for(i = 0; i < G->numVertexes; i++){for(j = i; j < G->numVertexes; j++){G->arc[j][i] =G->arc[i][j];}}}/* 利用鄰接矩陣構建鄰接表 */ void CreateALGraph(MGraph G,GraphAdjList *GL) {int i,j;EdgeNode *e;*GL = (GraphAdjList)malloc(sizeof(graphAdjList));(*GL)->numVertexes=G.numVertexes;(*GL)->numEdges=G.numEdges;for(i= 0;i <G.numVertexes;i++) /* 讀入頂點信息,建立頂點表 */ {(*GL)->adjList[i].in=0;(*GL)->adjList[i].data=G.vexs[i];(*GL)->adjList[i].firstedge=NULL; /* 將邊表置為空表 */}for(i=0;i<G.numVertexes;i++) /* 建立邊表 */{ for(j=0;j<G.numVertexes;j++){if (G.arc[i][j]==1){e=(EdgeNode *)malloc(sizeof(EdgeNode));e->adjvex=j; /* 鄰接序號為j */ e->next=(*GL)->adjList[i].firstedge; /* 將當前頂點上的指向的結點指針賦值給e */(*GL)->adjList[i].firstedge=e; /* 將當前頂點的指針指向e */ (*GL)->adjList[j].in++;}}}}Boolean visited[MAXSIZE]; /* 訪問標志的數組 *//* 鄰接表的深度優先遞歸算法 */ void DFS(GraphAdjList GL, int i) {EdgeNode *p;visited[i] = TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */p = GL->adjList[i].firstedge;while(p){if(!visited[p->adjvex])DFS(GL, p->adjvex);/* 對為訪問的鄰接頂點遞歸調用 */p = p->next;} }/* 鄰接表的深度遍歷操作 */ void DFSTraverse(GraphAdjList GL) {int i;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE; /* 初始所有頂點狀態都是未訪問過狀態 */for(i = 0; i < GL->numVertexes; i++)if(!visited[i]) /* 對未訪問過的頂點調用DFS,若是連通圖,只會執行一次 */ DFS(GL, i); }/* 鄰接表的廣度遍歷算法 */ void BFSTraverse(GraphAdjList GL) {int i;EdgeNode *p;Queue Q;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE;InitQueue(&Q);for(i = 0; i < GL->numVertexes; i++){if (!visited[i]){visited[i]=TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */EnQueue(&Q,i);while(!QueueEmpty(Q)){DeQueue(&Q,&i);p = GL->adjList[i].firstedge; /* 找到當前頂點的邊表鏈表頭指針 */while(p){if(!visited[p->adjvex]) /* 若此頂點未被訪問 */{visited[p->adjvex]=TRUE;printf("%c ",GL->adjList[p->adjvex].data);EnQueue(&Q,p->adjvex); /* 將此頂點入隊列 */}p = p->next; /* 指針指向下一個鄰接點 */}}}} }int main(void) { MGraph G; GraphAdjList GL; CreateMGraph(&G);CreateALGraph(G,&GL);printf("\n深度遍歷:");DFSTraverse(GL);printf("\n廣度遍歷:");BFSTraverse(GL);return 0; } #include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h"#define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0#define MAXSIZE 9 /* 存儲空間初始分配量 */ #define MAXEDGE 15 #define MAXVEX 9 #define INFINITY 65535typedef int Status; /* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */ typedef int Boolean; /* Boolean是布爾類型,其值是TRUE或FALSE */typedef char VertexType; /* 頂點類型應由用戶定義 */ typedef int EdgeType; /* 邊上的權值類型應由用戶定義 *//* 鄰接矩陣結構 */ typedef struct {VertexType vexs[MAXVEX]; /* 頂點表 */EdgeType arc[MAXVEX][MAXVEX];/* 鄰接矩陣,可看作邊表 */int numVertexes, numEdges; /* 圖中當前的頂點數和邊數 */ }MGraph;/* 鄰接表結構****************** */ typedef struct EdgeNode /* 邊表結點 */ {int adjvex; /* 鄰接點域,存儲該頂點對應的下標 */int weight; /* 用于存儲權值,對于非網圖可以不需要 */struct EdgeNode *next; /* 鏈域,指向下一個鄰接點 */ }EdgeNode;typedef struct VertexNode /* 頂點表結點 */ {int in; /* 頂點入度 */char data; /* 頂點域,存儲頂點信息 */EdgeNode *firstedge;/* 邊表頭指針 */ }VertexNode, AdjList[MAXVEX];typedef struct {AdjList adjList; int numVertexes,numEdges; /* 圖中當前頂點數和邊數 */ }graphAdjList,*GraphAdjList; /* **************************** *//* 用到的隊列結構與函數********************************** */ /* 循環隊列的順序存儲結構 */ typedef struct {int data[MAXSIZE];int front; /* 頭指針 */int rear; /* 尾指針,若隊列不空,指向隊列尾元素的下一個位置 */ }Queue;/* 初始化一個空隊列Q */ Status InitQueue(Queue *Q) {Q->front=0;Q->rear=0;return OK; }/* 若隊列Q為空隊列,則返回TRUE,否則返回FALSE */ Status QueueEmpty(Queue Q) { if(Q.front==Q.rear) /* 隊列空的標志 */return TRUE;elsereturn FALSE; }/* 若隊列未滿,則插入元素e為Q新的隊尾元素 */ Status EnQueue(Queue *Q,int e) {if ((Q->rear+1)%MAXSIZE == Q->front) /* 隊列滿的判斷 */return ERROR;Q->data[Q->rear]=e; /* 將元素e賦值給隊尾 */Q->rear=(Q->rear+1)%MAXSIZE;/* rear指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; }/* 若隊列不空,則刪除Q中隊頭元素,用e返回其值 */ Status DeQueue(Queue *Q,int *e) {if (Q->front == Q->rear) /* 隊列空的判斷 */return ERROR;*e=Q->data[Q->front]; /* 將隊頭元素賦值給e */Q->front=(Q->front+1)%MAXSIZE; /* front指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; } /* ****************************************************** */void CreateMGraph(MGraph *G) {int i, j;G->numEdges=15;G->numVertexes=9;/* 讀入頂點信息,建立頂點表 */ G->vexs[0]='A';G->vexs[1]='B';G->vexs[2]='C';G->vexs[3]='D';G->vexs[4]='E';G->vexs[5]='F';G->vexs[6]='G';G->vexs[7]='H';G->vexs[8]='I';for (i = 0; i < G->numVertexes; i++)/* 初始化圖 */{for ( j = 0; j < G->numVertexes; j++){G->arc[i][j]=0;}}G->arc[0][1]=1;G->arc[0][5]=1;G->arc[1][2]=1; G->arc[1][8]=1; G->arc[1][6]=1; G->arc[2][3]=1; G->arc[2][8]=1; G->arc[3][4]=1;G->arc[3][7]=1;G->arc[3][6]=1;G->arc[3][8]=1;G->arc[4][5]=1;G->arc[4][7]=1;G->arc[5][6]=1; G->arc[6][7]=1; for(i = 0; i < G->numVertexes; i++){for(j = i; j < G->numVertexes; j++){G->arc[j][i] =G->arc[i][j];}}}/* 利用鄰接矩陣構建鄰接表 */ void CreateALGraph(MGraph G,GraphAdjList *GL) {int i,j;EdgeNode *e;*GL = (GraphAdjList)malloc(sizeof(graphAdjList));(*GL)->numVertexes=G.numVertexes;(*GL)->numEdges=G.numEdges;for(i= 0;i <G.numVertexes;i++) /* 讀入頂點信息,建立頂點表 */ {(*GL)->adjList[i].in=0;(*GL)->adjList[i].data=G.vexs[i];(*GL)->adjList[i].firstedge=NULL; /* 將邊表置為空表 */}for(i=0;i<G.numVertexes;i++) /* 建立邊表 */{ for(j=0;j<G.numVertexes;j++){if (G.arc[i][j]==1){e=(EdgeNode *)malloc(sizeof(EdgeNode));e->adjvex=j; /* 鄰接序號為j */ e->next=(*GL)->adjList[i].firstedge; /* 將當前頂點上的指向的結點指針賦值給e */(*GL)->adjList[i].firstedge=e; /* 將當前頂點的指針指向e */ (*GL)->adjList[j].in++;}}}}Boolean visited[MAXSIZE]; /* 訪問標志的數組 *//* 鄰接表的深度優先遞歸算法 */ void DFS(GraphAdjList GL, int i) {EdgeNode *p;visited[i] = TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */p = GL->adjList[i].firstedge;while(p){if(!visited[p->adjvex])DFS(GL, p->adjvex);/* 對為訪問的鄰接頂點遞歸調用 */p = p->next;} }/* 鄰接表的深度遍歷操作 */ void DFSTraverse(GraphAdjList GL) {int i;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE; /* 初始所有頂點狀態都是未訪問過狀態 */for(i = 0; i < GL->numVertexes; i++)if(!visited[i]) /* 對未訪問過的頂點調用DFS,若是連通圖,只會執行一次 */ DFS(GL, i); }/* 鄰接表的廣度遍歷算法 */ void BFSTraverse(GraphAdjList GL) {int i;EdgeNode *p;Queue Q;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE;InitQueue(&Q);for(i = 0; i < GL->numVertexes; i++){if (!visited[i]){visited[i]=TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */EnQueue(&Q,i);while(!QueueEmpty(Q)){DeQueue(&Q,&i);p = GL->adjList[i].firstedge; /* 找到當前頂點的邊表鏈表頭指針 */while(p){if(!visited[p->adjvex]) /* 若此頂點未被訪問 */{visited[p->adjvex]=TRUE;printf("%c ",GL->adjList[p->adjvex].data);EnQueue(&Q,p->adjvex); /* 將此頂點入隊列 */}p = p->next; /* 指針指向下一個鄰接點 */}}}} }int main(void) { MGraph G; GraphAdjList GL; CreateMGraph(&G);CreateALGraph(G,&GL);printf("\n深度遍歷:");DFSTraverse(GL);printf("\n廣度遍歷:");BFSTraverse(GL);return 0; } #include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h"#define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0#define MAXSIZE 9 /* 存儲空間初始分配量 */ #define MAXEDGE 15 #define MAXVEX 9 #define INFINITY 65535typedef int Status; /* Status是函數的類型,其值是函數結果狀態代碼,如OK等 */ typedef int Boolean; /* Boolean是布爾類型,其值是TRUE或FALSE */typedef char VertexType; /* 頂點類型應由用戶定義 */ typedef int EdgeType; /* 邊上的權值類型應由用戶定義 *//* 鄰接矩陣結構 */ typedef struct {VertexType vexs[MAXVEX]; /* 頂點表 */EdgeType arc[MAXVEX][MAXVEX];/* 鄰接矩陣,可看作邊表 */int numVertexes, numEdges; /* 圖中當前的頂點數和邊數 */ }MGraph;/* 鄰接表結構****************** */ typedef struct EdgeNode /* 邊表結點 */ {int adjvex; /* 鄰接點域,存儲該頂點對應的下標 */int weight; /* 用于存儲權值,對于非網圖可以不需要 */struct EdgeNode *next; /* 鏈域,指向下一個鄰接點 */ }EdgeNode;typedef struct VertexNode /* 頂點表結點 */ {int in; /* 頂點入度 */char data; /* 頂點域,存儲頂點信息 */EdgeNode *firstedge;/* 邊表頭指針 */ }VertexNode, AdjList[MAXVEX];typedef struct {AdjList adjList; int numVertexes,numEdges; /* 圖中當前頂點數和邊數 */ }graphAdjList,*GraphAdjList; /* **************************** *//* 用到的隊列結構與函數********************************** */ /* 循環隊列的順序存儲結構 */ typedef struct {int data[MAXSIZE];int front; /* 頭指針 */int rear; /* 尾指針,若隊列不空,指向隊列尾元素的下一個位置 */ }Queue;/* 初始化一個空隊列Q */ Status InitQueue(Queue *Q) {Q->front=0;Q->rear=0;return OK; }/* 若隊列Q為空隊列,則返回TRUE,否則返回FALSE */ Status QueueEmpty(Queue Q) { if(Q.front==Q.rear) /* 隊列空的標志 */return TRUE;elsereturn FALSE; }/* 若隊列未滿,則插入元素e為Q新的隊尾元素 */ Status EnQueue(Queue *Q,int e) {if ((Q->rear+1)%MAXSIZE == Q->front) /* 隊列滿的判斷 */return ERROR;Q->data[Q->rear]=e; /* 將元素e賦值給隊尾 */Q->rear=(Q->rear+1)%MAXSIZE;/* rear指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; }/* 若隊列不空,則刪除Q中隊頭元素,用e返回其值 */ Status DeQueue(Queue *Q,int *e) {if (Q->front == Q->rear) /* 隊列空的判斷 */return ERROR;*e=Q->data[Q->front]; /* 將隊頭元素賦值給e */Q->front=(Q->front+1)%MAXSIZE; /* front指針向后移一位置, *//* 若到最后則轉到數組頭部 */return OK; } /* ****************************************************** */void CreateMGraph(MGraph *G) {int i, j;G->numEdges=15;G->numVertexes=9;/* 讀入頂點信息,建立頂點表 */ G->vexs[0]='A';G->vexs[1]='B';G->vexs[2]='C';G->vexs[3]='D';G->vexs[4]='E';G->vexs[5]='F';G->vexs[6]='G';G->vexs[7]='H';G->vexs[8]='I';for (i = 0; i < G->numVertexes; i++)/* 初始化圖 */{for ( j = 0; j < G->numVertexes; j++){G->arc[i][j]=0;}}G->arc[0][1]=1;G->arc[0][5]=1;G->arc[1][2]=1; G->arc[1][8]=1; G->arc[1][6]=1; G->arc[2][3]=1; G->arc[2][8]=1; G->arc[3][4]=1;G->arc[3][7]=1;G->arc[3][6]=1;G->arc[3][8]=1;G->arc[4][5]=1;G->arc[4][7]=1;G->arc[5][6]=1; G->arc[6][7]=1; for(i = 0; i < G->numVertexes; i++){for(j = i; j < G->numVertexes; j++){G->arc[j][i] =G->arc[i][j];}}}/* 利用鄰接矩陣構建鄰接表 */ void CreateALGraph(MGraph G,GraphAdjList *GL) {int i,j;EdgeNode *e;*GL = (GraphAdjList)malloc(sizeof(graphAdjList));(*GL)->numVertexes=G.numVertexes;(*GL)->numEdges=G.numEdges;for(i= 0;i <G.numVertexes;i++) /* 讀入頂點信息,建立頂點表 */ {(*GL)->adjList[i].in=0;(*GL)->adjList[i].data=G.vexs[i];(*GL)->adjList[i].firstedge=NULL; /* 將邊表置為空表 */}for(i=0;i<G.numVertexes;i++) /* 建立邊表 */{ for(j=0;j<G.numVertexes;j++){if (G.arc[i][j]==1){e=(EdgeNode *)malloc(sizeof(EdgeNode));e->adjvex=j; /* 鄰接序號為j */ e->next=(*GL)->adjList[i].firstedge; /* 將當前頂點上的指向的結點指針賦值給e */(*GL)->adjList[i].firstedge=e; /* 將當前頂點的指針指向e */ (*GL)->adjList[j].in++;}}}}Boolean visited[MAXSIZE]; /* 訪問標志的數組 *//* 鄰接表的深度優先遞歸算法 */ void DFS(GraphAdjList GL, int i) {EdgeNode *p;visited[i] = TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */p = GL->adjList[i].firstedge;while(p){if(!visited[p->adjvex])DFS(GL, p->adjvex);/* 對為訪問的鄰接頂點遞歸調用 */p = p->next;} }/* 鄰接表的深度遍歷操作 */ void DFSTraverse(GraphAdjList GL) {int i;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE; /* 初始所有頂點狀態都是未訪問過狀態 */for(i = 0; i < GL->numVertexes; i++)if(!visited[i]) /* 對未訪問過的頂點調用DFS,若是連通圖,只會執行一次 */ DFS(GL, i); }/* 鄰接表的廣度遍歷算法 */ void BFSTraverse(GraphAdjList GL) {int i;EdgeNode *p;Queue Q;for(i = 0; i < GL->numVertexes; i++)visited[i] = FALSE;InitQueue(&Q);for(i = 0; i < GL->numVertexes; i++){if (!visited[i]){visited[i]=TRUE;printf("%c ",GL->adjList[i].data);/* 打印頂點,也可以其它操作 */EnQueue(&Q,i);while(!QueueEmpty(Q)){DeQueue(&Q,&i);p = GL->adjList[i].firstedge; /* 找到當前頂點的邊表鏈表頭指針 */while(p){if(!visited[p->adjvex]) /* 若此頂點未被訪問 */{visited[p->adjvex]=TRUE;printf("%c ",GL->adjList[p->adjvex].data);EnQueue(&Q,p->adjvex); /* 將此頂點入隊列 */}p = p->next; /* 指針指向下一個鄰接點 */}}}} }int main(void) { MGraph G; GraphAdjList GL; CreateMGraph(&G);CreateALGraph(G,&GL);printf("\n深度遍歷:");DFSTraverse(GL);printf("\n廣度遍歷:");BFSTraverse(GL);return 0; }

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的邻接表的图遍历的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。