有向图的邻接表
#include<iostream>
using namespace std;
#include<malloc.h>#define MAX_VERTEX_NUM 20
#define VerTexType chartypedef enum{DG,DN,UDG,UDN}GraphKind;typedef struct ArcNode//邊結點
{int adjvex;//鄰結頂點的頂點序號double weight;struct ArcNode *next;
}ArcNode;typedef struct VNode
{VerTexType data;ArcNode *firststar;
}VNode,AdjList[MAX_VERTEX_NUM];typedef struct ALGraph
{AdjList vertices;int pointnum;int edgenum;int kind;
}ALGraph;bool IsExistArc(ALGraph &G,int i,int j)
{ArcNode *t;for(t =G.vertices[i].firststar;t;t = t->next){if(t->adjvex == j)return true;}return false;
}int LocateVexAL(ALGraph &G,VerTexType v)
{int i;for(i = 0;i<G.pointnum;i++){if(v == G.vertices[i].data)return i;}return -1;
}void CreateADJ(ALGraph &G)
{int i;int j;int k;VerTexType temp;VerTexType a;VerTexType b;ArcNode *s;G.kind = DG;for(k = 0;k < G.pointnum;k++){cout<<"請輸入頂點的值"<<endl;cin>>temp;for(i = 0 ;i<k;i++){if(temp== G.vertices[i].data)break;}if(i < k){k--;cout<<"已經存在這樣的點,重新輸入"<<endl;continue;}G.vertices[k].data = temp;G.vertices[k].firststar = NULL;}for(k = 0; k<G.edgenum;k++){cout<<"請輸入一條弧尾";cin>>a;cout<<"請輸入一條弧頭";cin>>b;i = LocateVexAL(G,a);j = LocateVexAL(G,b);if(i == j || IsExistArc(G,i,j) == true){k--;cout<<"已經存在這樣的點,重新輸入"<<endl;continue;}s = (ArcNode*)malloc(sizeof(ArcNode));s ->adjvex = j;s->next = G.vertices[i].firststar;G.vertices[i].firststar = s;}
}
void DispADJ(ALGraph &G)
{int k;ArcNode *p;for(k = 0;k<G.pointnum;k++){cout<<"從"<<G.vertices[k].data<<"出發的能到達的頂點有";for(p = G.vertices[k].firststar;p;p = p->next){cout<<G.vertices[p->adjvex].data;}cout<<endl;}
}int main()
{ALGraph G;cout<<"輸入頂點數"<<endl;cin>>G.pointnum;cout<<"輸入邊數"<<endl;cin>>G.edgenum;CreateADJ(G);DispADJ(G);
}
運算結果
轉載于:https://www.cnblogs.com/ygsworld/p/10023784.html
總結
- 上一篇: python中协程实现的本质以及两个封装
- 下一篇: python集合