最近复习到了数据结构中的图这一章,随手整理了些图的创建与遍历的完整代码,如下所示:
#include
using namespace std;
#define maxSize 100
typedef struct
{
int no; //顶点编号
char info; //顶点其他信息
}VertexType;
typedef struct
{
int edges[maxSize][maxSize];
int n,e;
VertexType vex[maxSize];
}MGraph;//图的邻接矩阵
typedef struct ArcNode//结点信息
{
int adjvex;
struct ArcNode *nextarc;
int info;
}ArcNode;
typedef struct VNode//头结点或链表信息
{
char data;
ArcNode *firstarc;
}VNode;
typedef struct
{
VNode adjlist[maxSize];//邻接表
int n,e;//顶点数,边数
}AGraph;//图的邻接表
//基本变量
int visit[maxSize] = {0};
void DFS(AGraph *G,int v);//深度优先搜索
void BFS(AGraph *G,int v);//广度优先搜索
int createGraph(AGraph &graph);//创建图
int getIndex(AGraph graph,int data);//由结点值得到索引
void main()
{
AGraph graph;
int ret = createGraph(graph);
if(ret==1)
{
/*cout<<"深度优先搜索"<
>graph.n&g