邻接表 构造 有向/无向 图/网
//create graph by adjacent list #include<iostream> #define MAX 20 using namespace std; //定义图的类型 typedef enum{DG,UDG,DN,UDN}GraphKind; //定义弧结点 typedef struct ArcNode{ int vex; struct ArcNode *nextArc; int info;//权值 }ArcNode; //定义表头(顶点)结点 typedef struct{ int vex; ArcNode *firstArc; }VexNode; //定义领接表存储的图 typedef struct{ int vexnum,arcnum; VexNode vertices[MAX]; GraphKind kind; }ALGraph; //返回顶点v在图中的位置(下标) int Locate(const ALGraph &G,int vex){ for(int i=0;i<G.vexnum;++i){ if(G.vertices[i].vex==vex) return i; } return INT_MAX; } //清空一个图,释放堆分配的内存 bool deleteGraph(ALGraph &G){ for(int i=0;i<G.vexnum;++i){ while(G.vertices[i].firstArc){ ArcNode *q=G.vertices[i].firstArc; G.vertices[i].firstArc=q->nextArc; delete q; } } return true; } //创造有向图 bool createDG(ALGraph &G){ cout<<"\nInput "<<G.vexnum<<" Vertices : "; for(int i=0;i<G.vexnum;++i){ cin>>G.vertices[i].vex; G.vertices[i].firstArc=NULL; } cout<<"Input "<<G.arcnum<<" Arcs : "<<endl; for(int i=0;i<G.arcnum;++i){ int v1,v2; int p=Locate(G,v1); if(p>=INT_MAX) return false; ArcNode* q=new ArcNode; q->vex=v2; q->info=INT_MAX; q->nextArc=G.vertices[p].firstArc; G.vertices[p].firstArc=q; q=NULL; } return true; } //创造无向图 bool createUDG(ALGraph &G){ cout<<"\nInput "<<G.vexnum<<" Vertices : "; for(int i=0;i<G.vexnum;++i){ cin>>G.vertices[i].vex; G.vertices[i].firstArc=NULL; } cout<<"Input "<<G.arcnum<<" Arcs : "<<endl; for(int i=0;i<G.arcnum;++i){ int v1,v2; cin>>v1>>v2; int p=Locate(G,v1); int q=Locate(G,v2); if(p>=INT_MAX||q>=INT_MAX) return false; ArcNode *temp=new ArcNode; temp->vex=v2; temp->nextArc=G.vertices[p].firstArc; temp->info=INT_MAX; G.vertices[p].firstArc=temp; temp=new ArcNode; temp->vex=v1; temp->info=INT_MAX; temp->nextArc=G.vertices[q].firstArc; G.vertices[q].firstArc=temp; } return true; } //创造有向网 bool createDN(ALGraph &G){ cout<<"\nInput "<<G.vexnum<<" Vertices : "; for(int i=0;i<G.vexnum;++i){ cin>>G.vertices[i].vex; G.vertices[i].firstArc=NULL; } cout<<"Input "<<G.arcnum<<" Arcs : "<<endl; for(int i=0;i<G.arcnum;++i){ int v1,v2,w; cin>>v1>>v2>>w; int p=Locate(G,v1); if(p>=INT_MAX) return false; ArcNode *temp=new ArcNode; temp->vex=v2; temp->nextArc=G.vertices[p].firstArc; temp->info=w; G.vertices[p].firstArc=temp; } return true; } //创造无向网 bool createUDN(ALGraph &G){ cout<<"\nInput "<<G.vexnum<<" Vertices : "; for(int i=0;i<G.vexnum;++i){ cin>>G.vertices[i].vex; G.vertices[i].firstArc=NULL; } cout<<"Input "<<G.arcnum<<" Arcs : "<<endl; for(int i=0;i<G.arcnum;++i){ int v1,v2,w; cin>>v1>>v2>>w; int p=Locate(G,v1); int q=Locate(G,v2); if(p>=INT_MAX||q>=INT_MAX) return false; ArcNode *temp=new ArcNode; temp->info=w; temp->vex=v2; temp->nextArc=G.vertices[p].firstArc; G.vertices[p].firstArc=temp; temp=new ArcNode; temp->info=w; temp->vex=v1; temp->nextArc=G.vertices[q].firstArc; G.vertices[q].firstArc=temp; } return true; } //接口,创造图 bool createGraph(ALGraph &G){ switch(G.kind){ case DG: return createDG(G); case UDG: return createUDG(G); case DN: return createDN(G); case UDN: return createUDN(G); default: return false; } } //打印邻接链表创造的图 void displayALGraph(const ALGraph &G){ for(int i=0;i<G.vexnum;++i){ cout<<G.vertices[i].vex<<" "; ArcNode *p=G.vertices[i].firstArc; while(p){ cout<<"->"<<p->vex; if(p->info < INT_MAX) cout<<"("<<p->info<<")"; p=p->nextArc; } cout<<endl; } } //测试 int main(){ ALGraph graph; int kind; cout<<"Create A Graph in Adjacent List ."<<endl; cout<<"1-DG , 2-UDG , 3-DN , 4-UDN"<<endl; cout<<"Graph Kind : "; cin>>kind; if(kind==1)graph.kind=DG; else if(kind==2) graph.kind=UDG; else if(kind==3) graph.kind=DN; else if(kind==4) graph.kind=UDN; else{ cout<<"Invalid Graph Type."; return 0; } cout<<"Vertices Number : "; cin>>graph.vexnum; cout<<"Arcs Number : "; cin>>graph.arcnum; if (createGraph(graph)){ cout<<"\nthe Adjacent Matrix is : "<<endl; displayALGraph(graph); }else cout<<"Invalid input , None Matching Vertex."<<flush; if(deleteGraph(graph)){ cout<<"\n\nAll Deleted."; } }
Look, if you had one shot , one opportunity , to seize everything you ever wanted , in one moment.
Would you captrue it , or just let it slip ?

浙公网安备 33010602011771号