有向圖的十字鏈表表存儲表示


  1 //---------有向圖的十字鏈表表存儲表示-------
  2 
  3 #include<stdio.h>
  4 #include<stdlib.h>
  5 
  6 #define MAX_VERTEXT_NUM 20
  7 
  8 typedef int InfoType;
  9 typedef char VertextType;
 10 
 11 typedef struct ArcBox
 12 {
 13     int headVex;
 14     int tailVex;
 15     struct ArcBox *headLink;
 16     struct ArcBox *tailLink;
 17     InfoType *info;
 18 }ArcBox;
 19 
 20 typedef struct VexNode
 21 {
 22     VertextType data;
 23     ArcBox *firstIn;
 24     ArcBox *firstOut;
 25 }VexNode;
 26 
 27 typedef struct
 28 {
 29     VexNode xList[MAX_VERTEXT_NUM];
 30     int vexNum;
 31     int arcNum;
 32 }OLGraph;
 33 
 34 void CreateGraphic(OLGraph *G);
 35 void DisplayGraphic(OLGraph *G);
 36 
 37 
 38 int main()
 39 {
 40     OLGraph *Graph = (OLGraph *)malloc(sizeof(OLGraph));
 41     CreateGraphic(Graph);
 42     DisplayGraphic(Graph);
 43     system("pause");
 44 }
 45 
 46 void CreateGraphic(OLGraph *G)
 47 {
 48     int i,j,k;
 49     ArcBox *arcBox;
 50     printf_s("請輸入頂點數和弧數:");
 51     scanf_s("%d,%d",&G->vexNum, &G->arcNum);
 52 
 53     //建立頂點表
 54     printf_s("建立頂點表\n");
 55     for (i = 0; i < G->vexNum; i++)
 56     {
 57         printf_s("請輸入第%d個頂點:", i);
 58         fflush(stdin);//刷新緩沖區
 59 
 60         G->xList[i].data = getchar();//輸入頂點值
 61         G->xList [i].firstIn = NULL;//初始化指針
 62         G->xList[i].firstOut = NULL;//初始化指針
 63     }
 64 
 65     //建立弧表
 66     printf_s("建立弧表\n");
 67     for (k = 0; k < G->arcNum; k++)
 68     {
 69         printf_s("請輸入(headVex-tailVex)的頂點對序號");
 70         scanf_s("%d,%d", &i, &j);
 71         arcBox = (ArcBox *)malloc(sizeof(ArcBox));
 72         //對弧結點賦值
 73         arcBox->headVex = j;
 74         arcBox->tailVex = i;
 75         arcBox->headLink = G->xList[j].firstIn;
 76         arcBox->tailLink = G->xList[i].firstOut;
 77         arcBox->info = NULL;
 78 
 79         //完成在入弧和出弧鏈表表頭的插入
 80         G->xList[j].firstIn = arcBox;
 81         G->xList[i].firstOut = arcBox;
 82     }
 83 }
 84 
 85 void DisplayGraphic(OLGraph *G)
 86 {
 87     int i;
 88     ArcBox *arcBox;
 89     printf_s("共有%d個頂點,%d條弧\n",G->vexNum, G->arcNum);
 90 
 91     for (i = 0; i < G->vexNum; i++)
 92     {
 93         printf_s("頂點%c:", G->xList[i].data);
 94 
 95         arcBox = G->xList[i].firstIn;
 96         printf_s("入度:");
 97         while (arcBox)
 98         {
 99             printf_s("%c->%c ", G->xList[arcBox->tailVex].data,G->xList[i].data);
100             arcBox = arcBox->headLink;
101         }
102 
103         arcBox = G->xList[i].firstOut;
104         printf_s("出度:");
105         while (arcBox)
106         {
107             printf_s("%c->%c ", G->xList[i].data,G->xList[arcBox->headVex].data);
108             arcBox = arcBox->tailLink;
109         }
110         printf_s("\n");
111     }
112 }

請輸入頂點數和弧數:4,7
建立頂點表
請輸入第0個頂點:0
請輸入第1個頂點:1
請輸入第2個頂點:2
請輸入第3個頂點:3
建立弧表
請輸入(headVex-tailVex)的頂點對序號0,1
請輸入(headVex-tailVex)的頂點對序號0,2
請輸入(headVex-tailVex)的頂點對序號2,0
請輸入(headVex-tailVex)的頂點對序號2,3
請輸入(headVex-tailVex)的頂點對序號3,0
請輸入(headVex-tailVex)的頂點對序號3,1
請輸入(headVex-tailVex)的頂點對序號3,2
共有4個頂點,7條弧
頂點0:入度:3->0 2->0 出度:0->2 0->1
頂點1:入度:3->1 0->1 出度:
頂點2:入度:3->2 0->2 出度:2->3 2->0
頂點3:入度:2->3 出度:3->2 3->1 3->0
請按任意鍵繼續. . .

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM