代码参照了严蔚敏、吴伟民编写的数据结构(C语言版)。 部分内容参考了这位大佬:
https://blog.csdn.net/jeffleo/article/details/53326648
所有代码采用C语言编写。讲解请查看注释。
头文件及宏定义
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define INFINITY 0
#define MAX_VERTEX_NUM 20
#define OK 1
#define Fail 0
#define False 0
#define True 1
#define Error 0;
typedef定义数据类型和结构体
typedef int VRType;
typedef int Status;
typedef char InfoType;
typedef char VertexType;
typedef enum{DG,DN,UDG,UDN}GraphKind;
typedef struct ArcCell{
VRType adj;
InfoType *info;
} ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct{
VertexType vexs[MAX_VERTEX_NUM];
AdjMatrix arcs;
int vexnum,arcnum;
GraphKind kind;
}MGraph;
LocateVex
Status LocateVex(MGraph G, VertexType u){
int i=0;
for (i=0; i<G.vexnum; i++){
if (G.vexs[i]==u)
return i;
}
return False;
}
CreateGraph
Status CreateGraph(MGraph *G){
int i=0,j=0,k=0,w=0;
printf("请输入图的顶点个数和弧数\n");
fflush(stdin);
scanf("%d",&G->vexnum);
fflush(stdin);
scanf("%d",&G->arcnum);
printf("请输入所有顶点\n");
for(i=0;i<G->vexnum;i++){
fflush(stdin);
scanf("%c",&G->vexs[i]);
}
for(i=0;i<G->vexnum;i++){
for(j=0;j<G->vexnum;j++) {
G->arcs[i][j].adj=INFINITY;
G->arcs[i][j].info=NULL;
}
}
char v1,v2;
printf("请输入弧的两个顶点和权值\n");
for(k=0;k<G->arcnum;k++){
fflush(stdin);
scanf("%c%c%d",&v1,&v2,&w);
i=LocateVex(*G,v1);
j=LocateVex(*G,v2);
G->arcs[i][j].adj=w;
G->arcs[j][i].adj=w;
}
G->kind=UDN;
return OK;
}
Display
void Display(MGraph G)
{
int i=0,j=0;
for (i = 0; i < G.vexnum; ++i)
for (j = 0; j < G.vexnum; ++j)
{
printf("%d ", G.arcs[i][j].adj);
if ((j + 1) % G.vexnum == 0)
printf("\n");
}
}
Destory
Status Destory(MGraph *G){
int i=0,j=0;
for(i=0;i<G->vexnum;i++){
for(j=0;j<G->vexnum;j++) {
G->arcs[i][j].adj=INFINITY;
G->arcs[i][j].info=NULL;
}
}
G->vexnum=0;
G->arcnum=0;
}
GetVex
Status GetVex(MGraph G,VertexType v){
int i=0;
for (i=0; i<G.vexnum; i++){
if (G.vexs[i]==v)
return i;
}
return False;
}
PutVex
Status PutVex(MGraph *G,VertexType v,VertexType value){
int i=0;
for (i=0; i<G->vexnum; i++){
if (G->vexs[i]==v)
G->vexs[i]=value;
return OK;
}
return False;
}
FirstAdjVex
Status FirstAdjVex(MGraph G,VertexType v){
int i=0,j=0;
i=LocateVex(G,v);
for(j=0;j<G.vexnum;j++){
if(G.arcs[i][j].adj!=INFINITY){
return j;
}
}
return Fail;
}
NextAdjVex
Status NextAdjVex(MGraph G,VertexType v,VertexType w){
int i=0,j=0,n=0;
i=LocateVex(G,v);
j=LocateVex(G,w);
for(n=j+1;n<G.vexnum;n++){
if(G.arcs[i][n].adj!=INFINITY){
return n;
}
}
return Fail;
}
InsertVex
Status InsertVex(MGraph *G,VertexType v){
int i=0;
G->vexnum++;
G->vexs[G->vexnum-1]=v;
for(i=0;i<G->vexnum;i++){
G->arcs[G->vexnum-1][i].adj=0;
G->arcs[G->vexnum-1][i].info=NULL;
G->arcs[i][G->vexnum-1].adj=0;
G->arcs[i][G->vexnum-1].info=NULL;
}
return OK;
}
DeleteVex
Status DeleteVex(MGraph *G,VertexType v){
int i=0,j=0,temp=0;
i=LocateVex(*G,v);
temp=i;
for(temp;temp<G->vexnum;temp++){
G->vexs[temp]=G->vexs[temp+1];
}
G->vexnum--;
for(j=0;j<G->vexnum+1;j++){
for(i;i<G->vexnum+1;i++){
G->arcs[j][i]=G->arcs[j][i+1];
G->arcs[i][j]=G->arcs[i+1][j];
}
}
for(i=0;i<G->vexnum;i++){
G->arcs[G->vexnum][i].adj=0;
G->arcs[G->vexnum][i].info=NULL;
G->arcs[i][G->vexnum].adj=0;
G->arcs[i][G->vexnum].info=NULL;
}
return OK;
}
InsertArc
Status InsertArc(MGraph *G,VertexType v,VertexType w,VRType value){
int i=0,j=0;
i=LocateVex(*G,v);
j=LocateVex(*G,w);
G->arcs[i][j].adj=value;
G->arcs[i][j].info=NULL;
G->arcs[j][i].adj=value;
G->arcs[j][i].info=NULL;
return OK;
}
DeleteArc
Status DeleteArc(MGraph *G,VertexType v,VertexType w){
int i=0,j=0;
i=LocateVex(*G,v);
j=LocateVex(*G,w);
G->arcs[i][j].adj=INFINITY;
G->arcs[i][j].info=NULL;
G->arcs[j][i].adj=INFINITY;
G->arcs[j][i].info=NULL;
return OK;
}
定义全局变量
int visited[MAX_VERTEX_NUM];
DFS
void DFS(MGraph G,int n){
int i;
printf("%c", G.vexs[n]);
visited[n]=True;
for(i=0;i<G.vexnum;i++){
if(G.arcs[n][i].adj!=INFINITY&&visited[i]==False)
DFS(G,i);
}
}
DFSTraverse
void DFSTraverse(MGraph G){
int i=0,n=0;
for(i=0;i<G.vexnum;i++) visited[i]=False;
for(n=0;n<G.vexnum;n++){
if(visited[n]==False)
DFS(G,n);
}
}
BFS
void BFS(MGraph G,int n){
int que[MAX_VERTEX_NUM],front=0,rear=0;
int i=0,w=0;
printf("%c", G.vexs[n]);
visited[n]=True;
rear=(rear+1)%MAX_VERTEX_NUM;
que[rear]=n;
while(front!=rear){
front=(front+1)%MAX_VERTEX_NUM;
i=que[front];
for(w=FirstAdjVex(G,G.vexs[i]);w!=Fail;w=NextAdjVex(G,G.vexs[i],G.vexs[w])){
if(visited[w]==False){
visited[w]=True;
printf("%c", G.vexs[w]);
rear=(rear+1)%MAX_VERTEX_NUM;
que[rear]=w;
}
}
}
}
BFSTraverse
void BFSTraverse(MGraph G){
int i=0,n=0;
for(i=0;i<G.vexnum;i++) visited[i]=False;
for(n=0;n<G.vexnum;n++){
if(visited[n]==False)
BFS(G,n);
}
}
输入样例
很多函数被我注释掉了,需要使用的话自行取消注释。
int main(void){
MGraph G;
CreateGraph(&G);
Display(G);
printf("DFS:\n");
DFSTraverse(G);
printf("\nBFS:\n");
BFSTraverse(G);
}
输入:
5 6 a b c d e ab1 ad2 bc3 be4 cd5 ce6
输出结果
|