这里将顶点通过数组的方式写入,直接输入边的数量,顶点信息和权值,如果要更改数组元素的数量则也需要修改最多顶点数,以及标记数组的元素的数量,并将其初始化为1,这里太懒了不想弄。
#include <stdio.h>
#include <stdlib.h>
#define MVNUM 5 //最多的顶点数
typedef char VerTexType; //顶点元素的类型
VerTexType vexs[MVNUM]={'a','b','c','d','e'}; //顶点数组
int visited[5]={0,0,0,0,0}; //深度and广度优先遍历的标记数组
typedef struct ArcNode //边节点
{
int adjvex,info; //adjvex该边所指向的顶点的位置 info和边相关的信息
struct ArcNode *nextarc; //指向下一条边的指针
}ArcNode;
typedef struct VNode //顶点数组
{
VerTexType data; //顶点信息
struct ArcNode *firstarc; //指向第一条依附该顶点的边的指针
}VNode,AdjList[MVNUM]; //AdjList表示邻接表的类型
typedef struct //ALGraph定义
{
AdjList vertices; //顶点数组
int vexnum,arcnum; //图当前的顶点数和弧数
}ALGraph;
typedef struct //定义广度优先遍历的辅助队列
{
int data[MVNUM];
int front,rear;
}SqQueue;
//函数声明
void InitALGraph(ALGraph *G,VerTexType vex[],int len); //初始化图
ArcNode *CreateArcNode(int adjvex,int info); //生成ArcNode也就是边的结点
int FindVexIndex(ALGraph *G,VerTexType v,int len);
void DFS(ALGraph *G,int v); //深度优先遍历
void BFS(ALGraph *G,int v); //广度优先遍历
void InitSqQueue(SqQueue *Q); //初始化队列
void EnQueue(SqQueue *Q,int v);//入队
int QueueEmpty(SqQueue *Q); //判断队列是否为空
void DeQueue(SqQueue *Q,int *u); //出队
int main()
{
ALGraph G;
int i=0;
InitALGraph(&G,vexs,MVNUM);
printf("顶点元素:\n");
for(i=0;i<MVNUM;i++)
{
printf("%c ",G.vertices[i].data);
}
printf("\n");
printf("\n深度优先遍历:\n");
DFS(&G,2); //这里0的是顶点的下标
for(i=0;i<MVNUM;i++) //这里因为深度优先遍历标记过了,所以重新初始化为0
{
visited[i]=0;
}
printf("\n");
printf("\n广度优先遍历:\n");
BFS(&G,1);
printf("\n");
}
void InitALGraph(ALGraph *G,VerTexType vex[],int len) //初始化图
{
int i,j,k,w;
char v1=' ',v2=' ';
G->vexnum=len;
printf("请输入边的数量:\t");
scanf("%d",&G->arcnum);
for(i=0;i<len;i++)
{
G->vertices[i].data=vex[i];
G->vertices[i].firstarc=NULL;
}
for(k=0;k<G->arcnum;k++)
{
ArcNode *p1,*p2;
printf("第条%d边邻接的顶点以及权值,输入是请用空格格开 ###格式为:a b 5\n",k+1);
scanf(" %c %c %d",&v1,&v2,&w);
i=FindVexIndex(G,v1,len);
j=FindVexIndex(G,v2,len);
p1=CreateArcNode(j,w); //有向网的邻接表,只需将p2的代码注释即可 把p1注释 便是逆邻接表
p2=CreateArcNode(i,w);
p1->nextarc=G->vertices[i].firstarc;
G->vertices[i].firstarc=p1;
p2->nextarc=G->vertices[j].firstarc;
G->vertices[j].firstarc=p2;
}
}
int FindVexIndex(ALGraph *G,VerTexType v,int len) //找到v的下标
{
int i=0;
for(i=0;i<len;i++)
{
if(G->vertices[i].data==v)return i;
}
return -1;
}
ArcNode *CreateArcNode(int adjvex,int info) //生成ArcNode也就是边的结点
{
ArcNode *newNode=(ArcNode*)malloc(sizeof(ArcNode));
newNode->adjvex=adjvex;
newNode->info=info;
newNode->nextarc=NULL;
return newNode;
}
void DFS(ALGraph *G,int v) //深度优先遍历
{
ArcNode *p;
printf("%c ",G->vertices[v].data);
visited[v]=1;
p=G->vertices[v].firstarc;
while(p!=NULL)
{
if(visited[p->adjvex]==0)
{
DFS(G,p->adjvex);
}
p=p->nextarc;
}
}
void BFS(ALGraph *G,int v) //广度优先遍历
{
int u;
SqQueue Q;
ArcNode *p;
visited[v]=1;
printf("%c ",G->vertices[v].data);
InitSqQueue(&Q);
EnQueue(&Q,v);
while(Q.front!=Q.rear)
{
DeQueue(&Q,&u);
p=G->vertices[u].firstarc;
while(p!=NULL)
{
if(visited[p->adjvex]==0)
{
printf("%c ",G->vertices[p->adjvex]);
visited[p->adjvex]=1;
EnQueue(&Q,p->adjvex);
}
p=p->nextarc;
}
}
}
void InitSqQueue(SqQueue *Q) //初始化队列
{
Q->front=Q->rear=0;
}
void EnQueue(SqQueue *Q,int v) //入队
{
Q->data[Q->rear]=v;
Q->rear=(Q->rear+1)%MVNUM;
}
int QueueEmpty(SqQueue *Q) //判断队列是否为空
{
if(Q->front==Q->rear){
return 1;
}
return 0;
}
void DeQueue(SqQueue *Q,int *u) //出队
{
*u=Q->data[Q->front];
Q->front=(Q->front+1)%MVNUM;
}