数据结构实验报告-图.doc

本章共4道实验题目。 一、采用邻接矩阵表示法创建无向图 1.定义图的邻接矩阵存储表示 2.采用邻接矩阵表示法创建无向图(CreateUDG) 3.输出所建图的邻接矩阵(PrintAMGraph) 例如: 5 6? ? ? ? //顶点数和边数的输入 1 2 3 4 5? ? //顶点信息的输入 1 2? ? ? ? //边信息的输入 1 4 2 3 3 4 2 5 3 5 0 1 0 1 0? ? //邻接矩阵的输出,每行最后一项后面均有空格的输出 1 0 1 0 1? 0 1 0 1 1? 1 0 1 0 0? 0 1 1 0 0? 程序: #include iostream using namespace std; #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status; #define MaxInt 32767 //表示极大值,即∞ #define MVNum 100 //最大顶点数 typedef int VerTexType;//假设顶点的数据类型为整型 typedef int ArcType; //假设边的权值类型为整型 typedef struct { VerTexType vexs[MVNum]; //顶点表 ArcType arcs[MVNum][MVNum]; //邻接矩阵 int vexnum,arcnum; //图的当前点数和边数 }AMGraph; int LocateVex(AMGraph G,VerTexType v) //查询顶点v在图G中的下标位置 { int i; for(i=0;iG.vexnum;++i) if(G.vexs[i]==v) return i; return -1; } //此处定义无向图的创建 //此处定义无向图的邻接矩阵的输出 int mat[666][666]; int edge, point, num, u, v; void PrintAMGraph() { for (int i = 1; i = point; ++i) { for (int j = 1; j = point; ++j) { cout mat[i][j] ; } cout endl; } } int main() { AMGraph G; int CreateUDG; cin point edge; for (int i = 1; i = point; ++i) cin num; //调用利用邻接矩阵创建无向图的函数CreateUDG for (int i = 1; i = edge; ++i) { cin u v; mat[u][v] = mat[v][u] = 1; } //调用输出邻接矩阵的函数PrintAMGraph PrintAMGraph(); return 0; } 二、采用邻接表表示法创建无向图 1.定义图的邻接表存储表示(ALGraph) 2.采用邻接表表示法创建无向图(CreateUDG) 3.输出所建图的邻接表(PrintALGraph) 例如: 5 6? ? ? ? //顶点数和边数的输入 1 2 3 4 5? ? //顶点信息的输入 1 2? ? ? ? //边信息的输入 1 4 2 3 3 4 2 5 3 5 1-3-1^ 2-4-2-0^ 3-4-3-1^ 4-2-0^ 5-2-1^ 程序: #include iostream using namespace std; #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 typedef int Status; #define MVNum 100 //最大顶点数 typedef int VerTexType; typedef int OtherInfo; //可以存储边的权值 typedef struct ArcNode //边结点 { int adjvex; //该边所指向的顶点的位置 struct ArcNode *nextarc; //指向下一条边的指针 OtherInfo info; //和边相关的信息 }ArcNode; typedef struct VNode { VerTexType data; ArcNod

文档评论(0)

1亿VIP精品文档

相关文档