• 数据结构(c语言实现)

    数据结构 /*校园共7个结点*/ #include <stdio.h> #include <stdlib.h> #define MAXSIZE 50 #define MAXINT 32700/*试着用32767,在算法中再+时,就会溢出出错*/ typedef int datatype; typedef struct { datatype vexs[MAXSIZE]; int edges[MAXSIZE][MAXSIZE]; int n,e; }Graph; void CreateGraph(Graph *graph) { /*按照图,手工建立邻接矩阵。然后写入程序*/ int i,j; graph->n=7; graph->e=10; /*所有数组从下标1开始使用,这是为了和顶点表示统一*/ for(i=1;i<=graph->n;i++) graph->vexs[i]=i; for(i=1;i<=graph->n;i++) for(j=1;j<=graph->n;j++) { graph->edges[i][j]=MAXINT;/*缺省值均为无穷大*/ if(i==j) graph->edges[i][j]=0; } graph->edges[1][2]=20; graph->edges[1][3]=10; graph->edges[1][4]=30; graph->edges[2][7]=9; graph->edges[3][5]=5; graph->edges[5][4]=12; graph->edges[5][7]=15; graph->edges[6][5]=8; graph->edges[6][7]=10; graph->edges[7][3]=18; } void PrintGraph(Graph *graph) { int i,j; printf(" "); for(j=1;j<=graph->n;j++) printf("%6d ",j);/*限定打印输出固定长度*/ printf("\n"); for(i=1;i<=graph->n;i++) { printf("%d ",i); for(j=1;j<=graph->n;j++) printf("%6d ",graph->edges[i][j]); printf("\n"); }

    0
    249
    374KB
    2009-10-13
    13
关注 私信
上传资源赚积分or赚钱