有向圖的十字鏈表表示方法


  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #define OK 1
  4 #define NO 0
  5 #define TRUE 1
  6 #define FALSE 0
  7 #define ERROR -1
  8 #define MAX_VERTEX_NUM 20 //最大頂點個數
  9 typedef int Status;
 10 
 11 typedef struct ArcBox
 12 {
 13     int tailvex,headvex;   //該弧的頭結點和尾結點  尾--->頭
 14     struct ArcBox *hlink,*tlink;//分別指向具有相同弧頭和相同弧尾的鏈域
 15 
 16 }ArcBox;
 17 
 18 typedef char VertexType_OL;//有向圖的頂點類型
 19 typedef struct VexNode
 20 {
 21     VertexType_OL data;
 22     ArcBox *firstin,*firstout;//指向第一條入弧和出弧
 23 
 24 }VexNode;
 25 
 26 typedef struct
 27 {
 28     VexNode xlist[MAX_VERTEX_NUM+1];//表頭向量
 29     int vexnum,arcnum;
 30 }OLGraph;
 31 
 32 Status CreateDG_OL(OLGraph *G);
 33 
 34 
 35 int LocateVex_AL(OLGraph G,VertexType_OL u);
 36 
 37 void OutputOLGraph(OLGraph G);
 38 
 39 int main(int argc,char**argv)
 40 {
 41     OLGraph G;
 42     printf("1\n函數CreateDG_OL等測試..\n");
 43     {
 44         printf("初始化有向圖G..\n");
 45         CreateDG_OL(&G);
 46         printf("\n");
 47     
 48     }
 49     printf("15\n函數OutputOLGraph 測試..\n");
 50     {
 51         printf("輸出有向圖的十字鏈表 G=\n");
 52         OutputOLGraph(G);
 53         printf("\n");
 54     }
 55 
 56 
 57 
 58 
 59 return 0;
 60 }
 61 
 62 Status CreateDG_OL(OLGraph *G){
 63     int i,j,k;
 64     VertexType_OL v1,v2;
 65     ArcBox *p;
 66      printf("輸入頂點數 ");
 67     scanf("%d",&(G->vexnum));
 68     printf("輸入弧數 ");
 69     scanf("%d",&(G->arcnum));
 70     printf("輸入各個頂點值 ");
 71     getchar();
 72     for(i=1;i<=G->vexnum;i++)
 73     {
 74         scanf("%c",&(G->xlist[i].data));
 75         G->xlist[i].firstin=NULL;
 76         G->xlist[i].firstout=NULL;
 77     }
 78     
 79     printf("讀取各邊,構造十字鏈表\n");
 80     for(k=1;k<=G->arcnum;k++)
 81     {
 82         getchar();
 83         printf("輸入相鄰結點(添加弧的信息)");
 84         scanf("%c%c",&v1,&v2);
 85         i=LocateVex_AL(*G,v1); //第i個結點的鏈表相連
 86         j=LocateVex_AL(*G,v2);
 87         p=(ArcBox*)malloc(sizeof(ArcBox));
 88         if(!p)
 89             exit(ERROR);
 90         p->tailvex=i;
 91         p->headvex=j;
 92         p->hlink=G->xlist[j].firstin;//把p插到第一個結點中去
 93         G->xlist[j].firstin=p;
 94         
 95         p->tlink=G->xlist[i].firstout;
 96         G->xlist[i].firstout=p;
 97     
 98     }
 99 
100 return OK;
101 
102 
103 
104 }
105 
106 
107 int LocateVex_AL(OLGraph G,VertexType_OL u)
108 {
109     int i;
110     for(i=1;i<=G.vexnum;i++)
111     {
112         if(G.xlist[i].data==u)
113             return i;
114     }
115     return 0;
116 
117 }
118 
119 void OutputOLGraph(OLGraph G){
120     int i,j;
121     ArcBox *p;
122     if(!G.arcnum&&!G.vexnum)
123         printf("空圖!!\n");
124     else
125     {
126         for(i=1;i<=G.vexnum;i++)
127         {
128             printf("%c-> ",G.xlist[i].data);
129             p=G.xlist[i].firstout;
130             j=1;
131             while(p)   
132             {
133                 while(p->headvex!=j) //已經確定尾部都是i因此找到所有的頭部存在的弧就好,另一端弧的head從小到大,因為輸入的時候就是從大到小
134                 {
135                     printf("    ");
136                     j++;
137                 }
138                 printf("(%c,%c)",G.xlist[p->tailvex].data,G.xlist[p->headvex].data);
139                 p=p->tlink;
140                 j++;
141             
142             }
143         printf("\n");
144         
145         }
146     
147     
148     
149     }
150 
151 
152 
153 }


免責聲明!

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



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