第十二周技术博客

数据结构

有向图与无向图

// 242陈坤鑫第十二周.cpp : Defines the entry point for the console application.

//

 

#include "stdafx.h"

#include <stdio.h>

#define MaxInt 32767                         //表示极大值,即∞

//DG表示有向图,AG表示无向图,WDG有向网 ,WAG无向网

typedef  enum{ DG,AG,WDG,WAG} Graph;

#define MVNum 100                       //最大顶点数

typedef char VexType;              //假设顶点的数据类型为字符型

typedef int AdjType;                       //假设边的权值类型为整型

typedef struct{

         Graph kind;

         VexType   vexs[MVNum];                           //顶点表

         AdjType   arcs[MVNum][MVNum];                  //邻接矩阵

         int vexnum,arcnum;                                  //图的当前顶点个数和边个数

}AdjGraph;

 

//此函数输入图的邻接矩阵结构pg和某个顶点数据v,输出与其邻接顶点的信息

void PrintfGraph(AdjGraph *pg,VexType v){

    int i,posi;

         for(i=0;i<pg->vexnum;i++){ //判断此图是否有该顶点V

                   if(pg->vexs[i]==v)

                            break;

         }

         if(i==pg->vexnum)

         {

                   printf("无此顶点%c数据\n",v);

                   return;;

         }

         posi=i;//有该顶点数据,保存此顶点存放在数组的位置posi

        

         for(i=0;i<pg->vexnum;i++){

                   if(pg->arcs[i][posi]!=MaxInt){

                            printf("%c顶点到%c顶点的距离为%d ",pg->vexs[i],v,pg->arcs[i][posi]);

                   }

         }

        

         printf("\n");

         for(i=0;i<pg->vexnum;i++){

                   if(pg->arcs[posi][i]!=MaxInt){

                            printf("%c顶点到%c顶点的距离为%d ",v,pg->vexs[i],pg->arcs[posi][i]);

                   }

         }

         printf("\n\n");

}

int main(int argc,char *argv[])

{

         printf("242陈坤鑫\n");

        

         AdjGraph ag;

         ag.vexnum=6;

         ag.kind=WAG;

         int i,j;

         for (i=0;i<MVNum;i++)

         {

                   ag.vexs[i]=0;

                   for (j=0;j<MVNum;j++)

                   {

                            ag.arcs[i][j]=MaxInt;

                   }

         }

         for(i=0;i<ag.vexnum;i++)

                   ag.vexs[i]='A'+i;

         int A=0,B=1,C=2,D=3,E=4,F=5;

         ag.arcs[A][B]=3;ag.arcs[B][A]=3;

         ag.arcs[A][C]=5;ag.arcs[C][A]=5;

         ag.arcs[B][D]=4;ag.arcs[D][B]=4;

         ag.arcs[C][E]=2;ag.arcs[E][C]=2;

         ag.arcs[D][F]=8;ag.arcs[F][D]=8;

         ag.arcs[E][F]=1;ag.arcs[F][E]=1;

         ag.arcs[A][F]=9;ag.arcs[F][A]=9;

        

         AdjGraph ag2;

         ag2.vexnum=6;

         ag2.kind=WDG;

         for (i=0;i<MVNum;i++)

         {

                   ag2.vexs[i]=0;

                   for (j=0;j<MVNum;j++)

                   {

                            ag2.arcs[i][j]=MaxInt;

                   }

         }

         for(i=0;i<ag.vexnum;i++)

                   ag2.vexs[i]='A'+i;

         ag2.arcs[A][B]=3;ag2.arcs[A][C]=5;

         ag2.arcs[B][D]=4;ag2.arcs[D][F]=8;

         ag2.arcs[C][E]=2;ag2.arcs[E][F]=1;

         ag2.arcs[A][F]=9;

         char c[20];

         while(true){

                   printf("请输入顶点数据:");

                   scanf("%s",c);

                   if(c[0]=='0')break;

                   printf("无向图\n");

                   PrintfGraph(&ag,c[0]);

                   printf("\n有向图\n");

                   PrintfGraph(&ag2,c[0]);

         }

         return 0;

}

周数

专业学习目标

专业学习时间

新增代码量

博客发表量

人文方面的学习

知识技能总结

 第十而周

数据结构有向图和无向图

3

350h

2

有向图和无向图难以区分运用

posted on 2016-05-18 21:23  废躯残骸  阅读(166)  评论(0编辑  收藏  举报