SHARE
TWEET

Untitled

a guest Dec 14th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <assert.h>
  3. #include <malloc.h>
  4.  
  5. enum { verMax = 10 };
  6.  
  7. typedef int VertexType;
  8. typedef int EdgeType;
  9.  
  10. typedef struct EdgeNode       //边表节点
  11. {
  12.     int adjvex;               //节点
  13.     EdgeType weight;          //权
  14.     struct EdgeNode* next;    
  15. }EdgeNode;
  16.  
  17. typedef struct VertexNode     //顶点表节点
  18. {
  19.     VertexType data;
  20.     EdgeNode *firstEdge;      //边表头指针
  21. }Adjlist[verMax], VertexNode; //Adglist 是 struct Vertex [verMax]类型的
  22.  
  23. typedef struct AdjGraph
  24. {
  25.     int EdgeNum, VertexNum;
  26.     Adjlist adjlist;
  27. }AdjGraph;
  28.  
  29.  
  30. void Create_graph(AdjGraph *G)
  31. {
  32.     int i, j, k, w;
  33.     EdgeNode *e;
  34.  
  35.     printf("请输入顶点,边数:");
  36.     scanf("%d,%d", &G->VertexNum, &G->EdgeNum);
  37.     printf("请输入顶点:");
  38.     for(i = 1; i <= G->VertexNum; i++)
  39.     {
  40.         scanf("%d", &G->adjlist[i].data);
  41.         G->adjlist[i].firstEdge = NULL;
  42.     }
  43.  
  44.     for(k = 1; k <= G->EdgeNum; k++)
  45.     {
  46.         printf("请输入边的两端节点和权v1, v2, w:");
  47.         scanf("%d,%d,%d", &i, &j, &w);
  48.         e = (EdgeNode*)malloc(sizeof(EdgeNode));
  49.         assert(e != NULL);
  50.         e->adjvex = j;
  51.         e->next = G->adjlist[i].firstEdge;
  52.         G->adjlist[i].firstEdge = e;
  53. /*     
  54.         e = (EdgeNode*)malloc(sizeof(EdgeNode));   //注释为逆邻接表的建立方式
  55.         assert(e != NULL);
  56.         e->adjvex = i;
  57.         e->next = G->adjlist[j].firstEdge;
  58.         G->adjlist[j].firstEdge = e;
  59. */
  60.     }
  61. }
  62.  
  63. int main()
  64. {
  65.    
  66.     AdjGraph *G;
  67.     int i = 0, j = 0;
  68.  
  69.     EdgeNode *e;
  70.     G = (AdjGraph *)malloc(sizeof(AdjGraph));
  71.     assert(G != NULL);
  72.     Create_graph(G);
  73.     printf("\n");
  74.     for(i = 1; i <= G->VertexNum; i++)
  75.     {
  76.         printf("|%d|->", G->adjlist[i].data);
  77.         e = G->adjlist[i].firstEdge;
  78.         while(e != NULL)
  79.         {
  80.             printf("%d->", e->adjvex);
  81.             e = e->next;
  82.         }
  83.         printf("NULL\n");
  84.     }
  85.  
  86.     return 0;
  87. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top