数据结构目前学习的基本大概已经完成了,图的讲解也基本结束,本次试验就是图的遍历。
将常用的头文件放在t11.h中
#include"stdio.h"
#include"string.h"
#include"ctype.h"
#include"malloc.h"
#include"stdlib.h" //atoi(),exit();
#include"io.h" //eof()
#include"math.h"
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
typedef int Status;
typedef int Boolean;
之后就是数据结构定义了,放在头文件tu.h中
typedef struct tunode // 定义邻接表中存放下标的结构体
{
int adjvex; // 存放当前图标识的位置
struct tunode *nextarc; // 链表的next指针
}tunode;
typedef struct vnode // 定义的存放图标识和邻接表的指针
{
char data;
tunode *firstarc;
}vnode;
typedef struct algraph // 管理邻接表的信息结构体
{
vnode *vertices;
int vexnum,arcnum;
}algraph;
int *p; // 全局变量,在深度优先
接下来就是实现功能函数的模块代码了,定义在文件tu.cpp中
void initgraph(algraph &T) // 图的初始化函数
{
printf("输入图结点的个数和弧的个数:");
scanf("%d%d",&T.vexnum,&T.arcnum);
getchar();
T.vertices=(vnode*)malloc(T.vexnum*sizeof(vnode));