Creat_DG 注释

View Code
 1 //CreateDG.cpp
2 //This function is to create OLGraph
3 # include <iostream.h>
4 # include <malloc.h>
5 # include <conio.h>
6
7 # define MAX_VERTEX_NUM 20
8 # define OK 1
9 typedef int VertexType;
10 typedef int InfoType;
11
12 typedef struct ArcBox //结构名 //define structure OLGraph
13 { int tailvex,headvex;
14 struct ArcBox *hlink,*tlink;
15 InfoType *info; //权值之类的
16 }ArcBox;//结构别名
17
18 typedef struct VexNode
19 { VertexType data;
20 ArcBox *firstin,*firstout;
21 }VexNode;
22
23 typedef struct //该结构体的名字就是OLGraph
24 { VexNode xlist[MAX_VERTEX_NUM];//表头向量
25 int vexnum,arcnum;//顶点数、弧数
26 }OLGraph;
27
28 int CreateDG(OLGraph &G) //CreateDG() sub-fuction
29 { int IncInfo,i,j,k,v1,v2,w; //IncInfo表示要不要权值呢? i,j分别表示弧尾顶点弧头顶点 同时i还表示总共顶点数 v1,v2表示输入的弧头和弧尾顶点
30 cout<<endl<<"Please input the number of G.vexnum (eg,G.vexnum=4): ";
31 cin>>G.vexnum; //input the number of vex
32 cout<<"Please input the number of G.arcnum (eg,G.arcnum=4): ";
33 cin>>G.arcnum; //input the number of arc
34 cout<<"Please input IncInfo (0 for none) : ";
35 cin>>IncInfo; //有无权值判定符 //not need information, input 0
36 for(i=0;i<G.vexnum;++i)
37 { cout<<"Please input the value of the "<<i+1<<"th vex : ";
38 cin>>G.xlist[i].data;
39 G.xlist[i].firstin=NULL;
40 G.xlist[i].firstout=NULL;
41 }
42 cout<<"Plese input arc(V1-->V2), For example: (V1=1,V2=3),(V1=2,V2=4)..."<<endl;
43 for(k=0;k<G.arcnum;++k) //input arc(v1,v2) //输入从v1到v3的弧 while循环很严谨!
44 { cout<<endl<<"Please input the "<<k+1<<"th arc's v1 (0<v1<G.vexnum): ";
45 cin>>v1;
46 cout<<"Please input the "<<k+1<<"th arc's v2 (0<v2<G.vexnum): ";
47 cin>>v2;
48 i=v1;
49 j=v2;
50 while(i<1||i>G.vexnum||j<1||j>G.vexnum) //value就是给这个顶点标号,从1开始,顶点的value值不能超过顶点数 //if (v1,v2) illegal
51 { cout<<endl<<"Please input the "<<k+1<<"th arc's v1 (0<v1<G.vexnum): ";
52 cin>>v1;
53 cout<<"Please input the "<<k+1<<"th arc's v2 (0<v2<G.vexnum): ";
54 cin>>v2;
55 i=v1;
56 j=v2;
57 } //while end
58 ArcBox *p;
59 p=(ArcBox *)malloc(sizeof(ArcBox)); //初始化完两个顶点了,该初始化他们之间的弧了 //assign memory
60 if(!p)
61 { cout<<"Overflow!"; //if voerflow
62 return (0);
63 }
64 p->tailvex=i; //把i赋给p弧的弧尾数 //assign p
65 p->headvex=j; //把j赋给p弧的弧头数
66 p->hlink=G.xlist[j].firstin; //把顶点j指向的第一条入弧 赋给 p弧的弧头
67 p->tlink=G.xlist[i].firstout; //把从顶点i出发的第一条出弧 赋给 p弧的弧尾
68 p->info=NULL;
69 G.xlist[j].firstin=p;
70 if(IncInfo)//从输入端判断要不要权值呢?如果要!
71 { cout<<"Please input the info :";
72 cin>>*(p->info); //输入权值吧! //input information
73 }
74 } //for end
75 return (OK);
76 } //CreateDG() end
77
78 void main() //main() function
79 { OLGraph G;
80 cout<<endl<<endl<<"CreateDG.cpp";
81 cout<<endl<<"============"<<endl;
82 if(CreateDG(G)) //call CreateDG() function
83 cout<<endl<<"Create OLGraph success!"; //if success, output information
84 cout<<endl<<endl<<"...OK!...";
85 getch();
86 } //main() end
posted @ 2012-02-21 23:58  uniquews  阅读(243)  评论(0编辑  收藏  举报