From 824aae1e43e9551898c0302d10926117a8b26a27 Mon Sep 17 00:00:00 2001 From: bala <13452927+rfgsdhshgfh@user.noreply.gitee.com> Date: Sat, 13 Jan 2024 15:47:15 +0000 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=9B=BE=E7=9A=84=E9=82=BB?= =?UTF-8?q?=E6=8E=A5=E7=9F=A9=E9=98=B5=E5=92=8C=E9=82=BB=E6=8E=A5=E8=A1=A8?= =?UTF-8?q?=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: bala <13452927+rfgsdhshgfh@user.noreply.gitee.com> --- ...5\350\241\250\345\255\230\345\202\250.cpp" | 154 ++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 "2224020152/\347\254\254\345\205\253\345\215\225\345\205\203/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\255\230\345\202\250.cpp" diff --git "a/2224020152/\347\254\254\345\205\253\345\215\225\345\205\203/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\255\230\345\202\250.cpp" "b/2224020152/\347\254\254\345\205\253\345\215\225\345\205\203/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\255\230\345\202\250.cpp" new file mode 100644 index 00000000..1cbb4a8f --- /dev/null +++ "b/2224020152/\347\254\254\345\205\253\345\215\225\345\205\203/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\255\230\345\202\250.cpp" @@ -0,0 +1,154 @@ +#include +#include +#define max 100 +#define INF 32767 +#define InfoType char +typedef struct +{ + int no; + InfoType info; +}VertexType; + +typedef struct +{ + int edges[max][max]; + int n,e; + VertexType vexs[max]; +}MatGraph; + + +typedef struct ANode +{ + int adjvex; + struct ANode *nextarc; + int weight; +}ArcNode; +typedef struct Vnode +{ + InfoType info; + ArcNode *firstarc; +}VNode; +typedef struct +{ + VNode adjlist[max]; + int n,e; +}AdjGraph; + +void Creatadj(AdjGraph *&G,int A[max][max],int n,int e) +{ + int i,j; + ArcNode *p; + G=(AdjGraph *)malloc(sizeof(AdjGraph)); + for(i=0;iadjlist[i].firstarc=NULL; + } + for(i=0;i=0;j--) + { + if(A[i][j]!=0&&A[i][j]!=INF) + { + p=(ArcNode *)malloc(sizeof(ArcNode)); + 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 Creatmat(MatGraph &g,int A[max][max],int n,int e) +{ + int i,j; + g.n=n; + g.e=e; + for(i=0;in;i++) + { + p=G->adjlist[i].firstarc; + printf("顶点%d ",i); + while(p!=NULL) + { + printf("%d[%d]->",p->adjvex,p->weight); + p=p->nextarc; + + } + printf("^\n"); + + } +} + + + +void DedtoryAdj(AdjGraph *&G) +{ + int i; + ArcNode *pre,*p; + for(i=0;in;i++) + { + pre=G->adjlist[i].firstarc; + while(pre!=NULL) + { + p=pre; + pre=pre->nextarc; + free(p); + } + } + free(G);G=NULL; +} +int main() +{ + AdjGraph *G; + MatGraph g; + int n=6; + int e=10; + int A[max][max]={{0,5,INF,7,INF,INF},{INF,0,4,INF,INF,INF},{8,INF,0,INF,9,INF},{INF,INF,5,0,INF,6},{INF,INF,INF,5,0,INF},{3,INF,INF,INF,1,0}}; + Creatmat(g,A,n,e); + printf("邻接矩阵为:\n\n"); + Dispmat(g); + Creatadj(G,A,n,e); + printf("邻接表为:\n\n"); + DispAdj(G); + printf("销毁邻接表为:\n\n"); + DedtoryAdj(G); + if(G==NULL) + printf("成功!\n\n"); + system("pause"); + return 0; +} -- Gitee