邻接表

 1 const int MAX_VERTEX_NUM = 100;     //最大顶点数为100 
 2 
 3 typedef struct EdgeNode             //边表结点 
 4 { 
 5    int adjvex;                       //该边指向的顶点在顺序表中的位置
 6     int weight;                                //边上的权重 
 7     struct EdgeNode* next;           //下一条边 
 8 }EdgeNode;                      
 9 
10 typedef struct VexNode              //表头结点 
11 { 
12     int vertex;                            //顶点中的数据信息 
13    EdgeNode* link;                  //指向单链表,即指向第一条边 
14 }VexNode;        
15 
16 typedef struct
17 {  
18    VexNode G[MAX_VERTEX_NUM];       //邻接表 
19    int Vertex,Edges;                 //顶点数和边数
20 }AdjGraph;                             // Adjgraph是以邻接表方式存储的图类型
21 
22 
23 void CreatGraph(AdjGraph& Graph, int a, int b, int c)    //a为起点,b为终点,c为权重,建立邻接表 
24 {
25     EdgeNode* p = Graph.G[a].link;
26     if(p == NULL)
27     {
28         p = new EdgeNode;
29         p->adjvex = b;
30         p->weight = c;
31         p->next = NULL;
32         Graph.G[a].link = p;
33     }
34     else
35     {
36         while(p->next != NULL)
37         {
38             p = p->next;
39         }
40         EdgeNode* q = new EdgeNode;
41         q->adjvex = b;
42         q->weight = c;
43         q->next = NULL;
44         p->next = q;
45     }
46 }

 

posted @ 2013-07-19 14:32  瓶哥  Views(177)  Comments(0Edit  收藏  举报