#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) { T = (matgraph *)malloc(sizeof(matgraph)); infotype ch[10]; int k = 0, i = 0, j = 0; bool 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++; } e = 0; while (i < n) { j = 0; while (j < n) { printf('Is the edge [%d][%d] exist? ', 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++; } }

int main() { matgraph *L; int n = 3; int e; creatematgraph(L, n, e); printgraph(L, n, e); return 0; }

C++ 实现邻接矩阵图的创建和打印

原文地址: https://www.cveoy.top/t/topic/qf8e 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录