#includematgraphcpp#includestdioh#includestringh#includestdlibh#define maxv 10#define inf 32767typedef char infotype;typedef struct int no; infotype info; vertextype;typedef struct int edge
#include<stdio.h> #include<string.h> #include<stdlib.h> #define maxv 10 #define inf 32767 typedef char infotype; typedef struct { int no; infotype *info; } vertextype; typedef struct { int edges[maxv][maxv]; vertextype vexs[maxv]; int n; int e; } matgraph;
void creatematgraph(matgraph *&T, int n,int &e) {
e=0;
T = (matgraph *)malloc(sizeof(matgraph));
infotype ch[10];
int k = 0, i = 0, j = 0;
int r;
while (k < n) {
scanf("%s", ch);
T->vexs[k].no = k;
T->vexs[k].info = new char[strlen(ch) + 1]; // 分配足够的内存
strcpy(T->vexs[k].info, ch);
k++;
}
while (i < n) {
j = 0;
while (j < n) {
printf("Is the edge [%d][%d] exist?\n", i, j);
scanf("%d", &r);
T->edges[i][j] = r;
if(r!=0)e++;
j++;
}
i++;
}
}
void printgraph(matgraph *&T, int n,int e) { int k = 0; while (k < n) { printf("No%d:%s\n", T->vexs[k].no, T->vexs[k].info); k++; } int i = 0, j = 0; while (i < n) { j = 0; while (j < n) { printf("%d\t", T->edges[i][j]); j++; } printf("\n"); i++; } }
typedef struct anode { int adjvex; struct anode *nextarc; int weight; } anode; typedef struct vnode { infotype info; anode *firstarc; } vnode; typedef struct { vnode adjlist[maxv]; int n,e; }adjgraph;
void creategraph(adjgraph *&G, int A[maxv][maxv],int n ,int e) { G=(adjgraph *)malloc(sizeof(adjgraph)); int i,j; anode *p; for(i=0;i<n;i++) G->adjlist[i].firstarc=NULL; for(i=0;i<n;i++){ for(j=n-1;j>=0;j--) { if(A[i][j]!=0&&A[i][j]!=inf){ p=(anode *)malloc(sizeof(anode)); p->adjvex=j; p->weight=A[i][j]; p->nextarc=G->adjlist[i].firstarc; G->adjlist[i].firstarc=p; } } } G->n=n; G->e=e; }
void printgraph(adjgraph *G )
{
int i;
anode *p;
for(i=0;i
int main()
{
matgraph *T;
adjgraph *G;
int n = 3;
int e;
creatematgraph(T, n,e);
printgraph(T, n,e);
int B[maxv][maxv];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
B[i][j]=T->edges[i][j];
}
}
creategraph(G, B,n ,e);
printgraph(G);
return 0;
原文地址: http://www.cveoy.top/t/topic/iyY6 著作权归作者所有。请勿转载和采集!