#include <stdio.h> #include <stdlib.h> #include <iostream> #include <queue> using namespace std; #define MaxVertexNum 100 //最大顶点数 typedef enum{DG,UDG,DN,UDN} GraphKind; //图的种类 typedef int InfoType; typedef char VertexType; typedef struct ArcNode { int adjvex; //邻接点域,存储该弧指向顶点的下标 struct ArcNode *next; //指向下一条弧的指针 InfoType* info; //该弧相关信息的指针 }ArcNode; typedef struct VertexNode { VertexType data; //数据域 ArcNode *firstArc; //指向第一条依附该顶点的弧的指针 }VertexNode,AdjList[MaxVertexNum]; typedef struct { AdjList vertices; //顶点集 int vexnum,arcnum; //图的顶点数和弧数 GraphKind kind; }ALGraph; //Adjacency List bool visited[MaxVertexNum]; //设访问标志数组,供遍历时使用 void CreateALGraph(ALGraph *G); int LocateVex(ALGraph *G,VertexType v); void Display(ALGraph *G); void DFS(ALGraph *G,int v); void DFSTraver(ALGraph *G); void BFSTraver(ALGraph *G); int main() { ALGraph G; CreateALGraph(&G); Display(&G); DFSTraver(&G); BFSTraver(&G); return 0; } //求顶点位置函数 int LocateVex(ALGraph *G,VertexType v) { int i; for(i=0;i<G->vexnum;i++) { if(G->vertices[i].data == v) return i; } return -1; } /* (a,b,1) (a,c,1) (b,d,1) (c,d,1) (d,a,1) */ //以又向带权图为例(DN) void CreateALGraph(ALGraph *G) { VertexType v1,v2; int weight; ArcNode *Arc; //1.确定顶点数和弧数 printf("请输入顶点数和边数:"); scanf("%d %d",&G->vexnum,&G->arcnum); //2.确定各个顶点的值 printf("请输入各顶点的数据:"); for(int i=0;i<G->vexnum;i++){ scanf(" %c",&(G->vertices[i].data)); G->vertices[i].firstArc = NULL; //顶点的边表设为空 } printf("请依次输入n组边对应的两个顶点以及权值,输入格式为(a,b,c):\n"); for(int k=0;k<G->arcnum;k++) { scanf(" (%c,%c,%d)",&v1,&v2,&weight); //printf("%c %c %d\n",v1,v2,weight); int i = LocateVex(G,v1); int j = LocateVex(G,v2); Arc = (ArcNode*)malloc(sizeof(ArcNode)); Arc->adjvex = j; Arc->info = (int *) malloc(sizeof(int)); *Arc->info = weight; Arc->next = G->vertices[i].firstArc; //头插 G->vertices[i].firstArc = Arc; //若是无向图,还要再生成一个节点表示<V2,v1> } } void Display(ALGraph *G) { ArcNode *p; printf("编号, 顶点, 相邻边的顶点\n"); for(int i=0;i<G->vexnum;i++) { printf("%4d %4c",i,G->vertices[i].data); for(p=G->vertices[i].firstArc;p!=NULL;p=p->next) printf("%8d",p->adjvex); printf("\n"); } } void Visit(ALGraph *G,int v) { printf("%c ",G->vertices[v].data); } //深度优先遍历 void DFS(ALGraph *G,int v) { Visit(G,v); visited[v] = true; ArcNode *p = G->vertices[v].firstArc; while(p) { if(!visited[p->adjvex]) { DFS(G,p->adjvex); }p = p->next; } } void DFSTraver(ALGraph *G) { for (int i = 0; i < G->vexnum; i++) { visited[i] = false; } printf("DFSTraver:\n"); for (int i = 0; i < G->vexnum; i++) { if(!visited[i]) DFS(G,i); } } //广度度优先遍历 void BFS(ALGraph *G,int v) { Visit(G,v); visited[v] = true; queue<int> q; q.push(v); while(!q.empty()) { int w; w = q.front(); q.pop(); //队头元素出队 ArcNode *p = G->vertices[w].firstArc; while(p!=NULL) { if(!visited[p->adjvex]) { Visit(G,p->adjvex); visited[p->adjvex] = true; q.push(p->adjvex); } p = p->next; } } } void BFSTraver(ALGraph *G) { printf("\nBFSTraver:\n"); for (int i = 0; i < G->vexnum; i++) { visited[i] = false; }; for (int i = 0; i < G->vexnum; i++) { if(!visited[i]) BFS(G,i); } }
原文地址:http://blog.csdn.net/huolang_vip/article/details/46368503