有向图的打印

来源:互联网 发布:网络创业qq群 编辑:程序博客网 时间:2024/05/01 05:25
#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#define N 100using namespace std;struct EdgeNode{ // 表节点 里面有邻接区域 adjvex(存放vi 和 vj 相连的序号) 和 链区域next(将所以表的节点链 放在一起)int adjvex;char data;EdgeNode *next; //边节点的下一个边节点};struct VertexNode{ //头节点 顶点vertex 存放vi ,first vi 的邻接表的头指针char data;EdgeNode *first; //头节点的下一个节点};struct AdjList{int Dnum; //节点数目int Bnum;  //边数VertexNode vertex[N];};void Creat(AdjList *adj) {int n, e;cout << "节点数目和边的数目:" << endl;cin >> n >> e;adj->Dnum = n;adj->Bnum = e;EdgeNode *p = NULL;for (int i = 1; i <= n; i++) {cout <<"节点"<<endl;cin >> adj->vertex[i].data;adj->vertex[i].first = NULL;}int s,d;for (int i = 1; i <= e; i++) {cout <<"有关连边的起点和终点"<<endl;cin >> s >> d;p = (EdgeNode *)malloc(sizeof(EdgeNode));if (p == NULL) return ;p->adjvex = d;p->next = adj->vertex[s].first;adj->vertex[s].first = p;}}void print(AdjList *adj) {int n = adj->Dnum;EdgeNode *p = NULL;for (int i = 1; i <= n; i++) {p = adj->vertex[i].first;if (p == NULL) {cout <<"没有从"<<adj->vertex[i].data <<"出发的节点"<<endl;}else {cout << "从节点"<<adj->vertex[i].data <<"出发的节点" <<endl;while (p) {cout <<adj->vertex[i].data<<"->"<<adj->vertex[p->adjvex].data<< endl;p = p->next;}}}}int main () {AdjList *adj = (AdjList *)malloc(sizeof(AdjList));Creat(adj);print(adj);}

0 0
原创粉丝点击