图的存储和遍历c语言,图的邻接矩阵存储 深度优先遍历 广度优先遍历 C语言实现...

MGraph. h

#pragma once

#include "Queue.h"

#define MaxVertexNum 100

typedef char VertexType;

typedef int EdgeType;

typedef struct

{

VertexType vexs[MaxVertexNum];

EdgeType edges[MaxVertexNum][MaxVertexNum];

int n;//当前图顶点数

int e;//当前边数

}MGraph;

bool visited[MaxVertexNum];

MGraph* initMGraph();

bool DFSTraverseM(MGraph* m);

bool DFSM(MGraph* m, int i);

bool BFSTraverseM(MGraph* m);

bool BFSM(MGraph* m, int i);

MGraph.c

#include "MGraph.h"

#include

#include

#include

/************************************************************************/

/* 初始化图*/

/************************************************************************/

MGraph* initMGraph()

{

MGraph* m = NULL;

int i, j, k;

char v1, v2;

m = (MGraph*)malloc(sizeof(MGraph));

memset(m->vexs, 0, MaxVertexNum);

printf("please input the number of vertex and edges('v,e'): ");

scanf("%d,%d", &i, &j);

if(i<0 && j<0)

{

printf("error number");

return NULL;

}

m->n = i;

m->e = j;

printf("please input the vertexs by order:/n");

for(i=0; in; i++)

{

fflush(stdin);

scanf("%c", &m->vexs[i]);

}

for(i=0; in; i++)

for(j=0; jn; j++)

m->edges[i][j] = 0;

for(k=0; ke; k++)

{

printf("please input the edges by order('v1,v2'): ");

fflush(stdin);

scanf("%c,%c", &v1, &v2);

for(i=0; v1!=m->vexs[i]; i++);

for(j=0; v2!=m->vexs[j]; j++);

m->edges[i][j] = 1;

}

return m;

}

/************************************************************************/

/* 深度优先遍历(深度优先搜索) */

/************************************************************************/

bool DFSTraverseM(MGraph* m)

{

int i;

if(m == NULL)

return FALSE;

for(i=0; in; i++)

visited[i] = FALSE;

for(i=0; in; i++)

DFSM(m, i);

return TRUE;

}

bool DFSM(MGraph* m, int i)

{

int j;

if(m == NULL)

return FALSE;

if(visited[i] == FALSE)

{

printf("DFSM: node %c/n", m->vexs[i]);

visited[i] = TRUE;

}

for(j=0; jn; j++)

if(visited[j] == FALSE && m->edges[i][j] == 1)

DFSM(m, j);

return TRUE;

}

/************************************************************************/

/* 广度优先遍历(广度优先搜索) */

/************************************************************************/

bool BFSM(MGraph* m, int i)

{

int j;

Queue* q = NULL;

if(m == NULL)

return FALSE;

visited[i] = TRUE;

printf("BFSM: node %c/n", m->vexs[i]);

q = initQueue();

enQueue(q, i);

while(q->size != 0)

{

i = deQueue(q);

for(j=0; jn; j++)

if(visited[i] == FALSE && m->edges[i][j] == 1)

{

enQueue(q, j);

printf("BFSM: node %c/n", m->vexs[j]);

visited[j] = TRUE;

}

}

return TRUE;

}

bool BFSTraverseM(MGraph* m)

{

Queue* q = NULL;

int i;

if(m == NULL)

return FALSE;

q = initQueue();

for(i=0; in; i++)

visited[i] = FALSE;

for(i=0; in; i++)

if(visited[i] == FALSE)

BFSM(m, i);

return TRUE;

}

Queue.h

#pragma once

typedef enum{TRUE, FALSE}bool;

#define CAPACITY 10

typedef int ElemType;

typedef struct

{

int front;

int rear;

int size;

ElemType data[CAPACITY];

}Queue;

Queue* initQueue();

ElemType deQueue(Queue* q);

bool enQueue(Queue* q, ElemType data);

Queue.c

#include "Queue.h"

#include

#include

/************************************************************************/

/* 初始化队列*/

/************************************************************************/

Queue* initQueue()

{

Queue *q = NULL;

q = (Queue*)malloc(sizeof(Queue));

if(q == NULL)

return NULL;

memset(q->data, 0, CAPACITY);

q->front = q->rear = 0;

q->size = 0;

return q;

}

/************************************************************************/

/* 队尾入队*/

/************************************************************************/

bool enQueue(Queue* q, ElemType data)

{

if(q == NULL)

return FALSE;

if(q->size == CAPACITY)

return FALSE;

q->data[q->rear] = data;

q->rear = (q->rear+1) % CAPACITY;

q->size++;

return TRUE;

}

/************************************************************************/

/* 队首出队*/

/************************************************************************/

ElemType deQueue(Queue* q)

{

ElemType res;

if(q == NULL)

exit(0);

if(q->size == 0)

return FALSE;

res = q->data[q->front];

q->front = (q->front+1) % CAPACITY;

q->size--;

return res;

}

main.c

#include "MGraph.h"

#include "Queue.h"

int main()

{

MGraph* m = initMGraph();

DFSTraverseM(m);

printf("/n");

BFSTraverseM(m);

return 0;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值