输出有向图的邻接矩阵

 1 void dispmgraph(AdjMatrix adj)//输出有向图的邻接矩阵
 2 {
 3     int i,j,n,e;
 4     n=adj.n;
 5     e=adj.e;
 6     printf("输出有向图的矩阵表示:\n");
 7     printf("  ");
 8     for(i=0;i<n;i++)
 9         printf("%3d",i);
10     printf("\n");
11     for(i=0;i<n;i++)
12     {
13         printf("%3d",i);
14         for(j=0;j<n;j++)
15             if(adj.edges[i][j]==0)
16             printf("..");
17         else
18             printf("%3d",adj.edges[i][j]);
19         printf("\n");
20     }
21 }

 

  1 void nattolist(AdjMatrix a,AdjList *&g)
  2 {
  3     int i,j,n;
  4     n=a.n;
  5     ArcNode *p;
  6     for(i=0;i<n;i++)
  7         g[i]->firstarc=NULL;
  8     for(i=0;i<n;i++)
  9         for(j=n-1;j>=0;j--)
 10         if(a.edges[i][j]!=0)
 11     {
 12         p=(ArcNode*)malloc(sizeof(Arcnode));
 13         p->adjvex=j;
 14         p->nextarc=g[i]->firstarc;
 15         g[i]->firstarc=p;
 16     }
 17 }
 18 
 19 int getnum(AdjList *g)
 20 {
 21     int i,n=0,visited[MAXVEX];
 22     for(i=0;i<MAXVEX;i++)
 23         visited[i]=0;
 24     dfs(g,0);
 25     for(i=0;i<g->n;i++)
 26         if(visited[i]==0)
 27     {
 28         n++;
 29         dfs(g,i);
 30     }
 31     return n;
 32 }
 33 
 34 void dfs1(AdjList *g,int v)
 35 {
 36     int i;
 37     ArcNode *p;
 38     int visited[MAXVEX],top=-1,stack[MAXVEX];
 39     for(i=0;i<MAXVEX;i++)
 40         visited[i]=0;
 41     printf("%d ",v);
 42     top++;
 43     stack[top]=v;
 44     visited[v]=1;
 45     while(top>=0)
 46     {
 47         v=stack[top];
 48         top--;
 49         p=g[v]->firstarc;
 50         while(p!=NULL&&visited[p->adjvex]==1)
 51             p=p->nextarc;
 52         if(p==NULL)
 53             top--;
 54         else{
 55             v=p->adjvex;
 56             printf("%d ",v);
 57             visited[v]=1;
 58             top++;
 59             stack[top]=v;
 60         }
 61     }
 62 }
 63 
 64 
 65 #define MAXVEX 100
 66 struct vertex
 67 {
 68     int num;
 69     char data;
 70 };
 71 typedef struct graph
 72 {
 73     int n;
 74     int e;
 75     struct vertex vexs[MAXVEX];
 76     int edges[MAXNVEX][MAXVEX];
 77 }AdjMatrix;
 78 
 79 AdjMatrix creategraph()
 80 {
 81   int i,j,k,w,n,e;
 82   char b,t;
 83   AdjMatrix adj;
 84   printf("顶点数(n)和边数(e):");
 85   scanf("%d%d",&n,&e);
 86   adj.n=n;
 87   adj.e=e;
 88   for(i=0;i<n;i++)
 89   {
 90       printf(" 第%d个顶点的信息:",i+1);
 91       scanf("%c",&adj.vex[i].data);
 92       adj.vex[i].num=i;
 93   }
 94   for(i=0;i<n;i++)
 95     for(j=0;j<n;j++)
 96     adj.edges[i][j]=0;
 97   for(k=0;k<e;k++)
 98   {
 99       printf("第%d条边:\n",k+1);
100       printf(" 起点:\n";
101       scanf("%d",&b);
102       printf(" 终点:");
103       scanf("%d",&t);
104       printf(" 权值:");
105       scanf("%d",&w);
106       i=0;
107       while(i<n&&adj.vexs[i].data!=b)
108         i++;
109       if(i>=n)
110 
111   }
112 }

 

posted @ 2013-12-15 09:49  zhangyongjian  阅读(926)  评论(0编辑  收藏  举报