数据结构的C实现_图_邻接矩阵表示

本文介绍了如何使用C语言实现数据结构中的图,特别是邻接矩阵表示法。通过创建不同类型的图(有向图、无向图、有向网、无向网),进行广度优先遍历、深度优先遍历,并提供了插入顶点、删除顶点、添加弧和删除弧的功能。代码示例展示了具体的操作过程。
//编译环境 Visual Studio 2008 win32 console application
//MGragh.c
////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#define MaxVertexNum 100//最大顶点数
#define QM 100 //队列的最大元素个数
#define OK 1
#define ERROR 0
int visited[MaxVertexNum];

typedef enum{DG,DN,NDG,NDN}GraphKind;//图的种类,有向图,有向网,无向图,无向网

typedef int VertexType;//顶点类型

typedef struct
{
int adj;//相邻与否,或权值大小
}ArcCell;

typedef struct
{
VertexType vexs[MaxVertexNum];//顶点表
ArcCell arcs[MaxVertexNum][MaxVertexNum];//邻接矩阵,边表
int vexnum,arcnum;//图中当前的顶点数和边数
int Graphkind;//图的种类标志
}MGragh;
////////////////////////////////////////////定义队列,用于广度优先遍历//////////////////////////////////////
typedef struct
{
VertexType *base;
VertexType front,rear;
}SQueue;

/////////////////////////////////////////函数声明///////////////////////////////////////////////////////////
int LocateVex(MGragh *M,VertexType v);
int CreateDN(MGragh *M);//有向网,带权值
int CreateDG(MGragh *M);//有向图,无权值
int CreateUDN(MGragh *M);//无向网,带权值
int CreateUDG(MGragh *M);//无向图,带权值
int CreatGragh(MGragh *G);
int FirstAdjVex(MGragh *G,int v);//返回V的第一个邻接顶点
int NextAdjVex(MGragh *G,int v,int w);//返回V的相对于W的下一个邻接顶点
void BFSTraverse(MGragh *G);//广度遍历
void DFS(MGragh *G,int v0);
void DFSTaverse(MGragh *G);//深度遍历
void PrintMatrix(MGragh *G);
void InsertVex(MGragh *G,int v);
int DeVex(MGragh *G,VertexType v);
int InserArc(MGragh *G);
int DeArc(MGragh *G);

///////////////////与队列有关的声明/////////////////////////////////
void InitQueue(SQueue *Q);
void EnQueue(SQueue *Q,int e);
int  QueueEmpty(SQueue *Q);
void DeQueue(SQueue *Q,int*e);


///////////////////////////////////////主函数/////////////////////////////////////////////////////////
void main()
{
MGrag
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值