ALGraph.h
#pragma once
#include "Queue.h"
/************************************************************************/
/* 图的邻接表存储结构 */
/************************************************************************/
#define MaxVertexNum 100
#define QueueSize 30
bool visited[MaxVertexNum];
typedef char VertexType;
typedef int EdgeType;
typedef struct node //边表结点
{
int adjvex; //邻接点域
struct node* next; //域链
//若是要表示边上的权,则应增加一个数据域
}EdgeNode;
typedef struct vnode //顶点边结点
{
VertexType vertex; //顶点域
EdgeNode* firstedge;//边表头指针
}VertexNode;
typedef VertexNode AdjList[MaxVertexNum]; //AdjList是邻接表类型
typedef struct
{
AdjList adjlist; //邻接表
int n;//图中当前顶点数
int e;//图中当前边数
}ALGraph; //对于简单的应用,无须定义此类型,可直接使用AdjList类型
ALGraph* initALGraph();
bool DFS(ALGraph* a, int i);
bool DFSTraverseM(ALGraph* a);
bool BFSTraverseM(ALGraph* a);
bool BFS(ALGraph* a, int i);
ALGraph.c
#include "ALGraph.h"
#include
#include
ALGraph* initALGraph()
{
ALGraph* a = NULL;
EdgeNode* e = NULL;
int i, j, k;
char v1, v2;
printf("请输入顶点数和边数(输入格式为:顶点数,边数): ");
scanf("%d,%d", &i, &j);
if(i<0 || j<0)
return NULL;
a = (ALGraph*)malloc(sizeof(ALGraph));
if(a == NULL)
return NULL;
a->n = i;
a->e = j;
for(i=0; in; i++)
{
printf("请输入顶点信息 每个顶点以回车作为结束: ");
fflush(stdin);
scanf("%c",&(a->adjlist[i].vertex)); // 读入顶点信息
a->adjlist[i].firstedge=NULL; // 点的边表头指针设为空
}
for(k=0; ke; k++)
{
printf("请输入边的信息(输入格式为:i,j): ");
fflush(stdin);
scanf("%c,%c", &v1, &v2);
for(i=0; v1!=a->adjlist[i].vertex; i++);//找到顶点对应的存储序号
for(j=0; v2!=a->adjlist[j].vertex; j++);//找到顶点对应的存储序号
e = (EdgeNode*)malloc(sizeof(EdgeNode));
e->adjvex = i;
e->next = a->adjlist[j].firstedge;
a->adjlist[j].firstedge = e;
e = (EdgeNode*)malloc(sizeof(EdgeNode));
e->adjvex = j;
e->next = a->adjlist[i].firstedge;
a->adjlist[i].firstedge = e;
}
return a;
}
/************************************************************************/
/* 深度优先遍历 */
/************************************************************************/
bool DFS(ALGraph* a, int i)
{
if(a == NULL)
return FALSE;
printf("DFS: node %c:/n", a->adjlist[i].vertex);
visited[i] = TRUE;
i = a->adjlist[i].firstedge->adjvex;
if(!visited[i])
DFS(a, i);
return TRUE;
}
bool DFSTraverseM(ALGraph* a)
{
int i;
if(a == NULL)
return FALSE;
for(i=0; in; i++)
visited[i] = FALSE;
for(i=0; in; i++)
if(!visited[i])
DFS(a, i);
return TRUE;
}
/************************************************************************/
/* 广度优先遍历(递归实现) */
/************************************************************************/
bool BFS(ALGraph* a, int i)
{
int j, k;
Queue *q = NULL;
EdgeNode *e = NULL;
if(a == NULL)
return FALSE;
q = initQueue();
if(!visited[i])
{
printf("BFS: node %c/n", a->adjlist[i].vertex);
visited[i] = TRUE;
}
j = a->adjlist[i].firstedge->adjvex;
e = a->adjlist[i].firstedge->next;
if(!visited[j])
{
enQueue(q, j);
while(e)
{
k = e->adjvex;
if(!visited[k])
{
enQueue(q, e->adjvex);
printf("BFS: node %c/n", a->adjlist[k].vertex);
visited[k] = TRUE;
}
e = e->next;
}
}
while(q->size != 0)
{
j = deQueue(q);
BFS(a, j);
}
}
bool BFSTraverseM(ALGraph* a)
{
int i;
if(a == NULL)
return FALSE;
for(i=0; in; i++)
visited[i] = FALSE;
for(i=0; in; i++)
BFS(a, i);
return TRUE;
}
Queue.h
#pragma once
typedef enum{FALSE, TRUE}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 "ALGraph.h"
int main()
{
ALGraph* a = initALGraph();
BFSTraverseM(a);
return 0;
}