有向图、无向图的邻接表构造

1.有向图、无向图邻接表的构造
2.判断边是否存在
3.求顶点的度数
结构体构造采用《数据结构(C语言版)》书上的方法
是一个实验。写的不咋样,就是记录一下。

#include<iostream>
#include<stdlib.h>
using namespace std;
#define MAX_NUM 30
typedef struct ArcNode{
	int adjvex;
	struct ArcNode *nextarc;
	int weight;
}ArcNode;
typedef struct VNode{
	int data;
	ArcNode *firstarc;
}VNode, AdjList[MAX_NUM];
typedef struct{
	AdjList vertices;
	int vexnum,arcnum;
	int kind;
}ALGraph,*AL;
ALGraph* creatALGraph(ALGraph *AG,AL &RAG,int &num){//创建一个图 
	int flag;
	ArcNode *an; 
	AG = (ALGraph *)malloc(sizeof(ALGraph));
	cout<<"输入节点数和边数"<<endl; //输入节点数和边数 
	cin>>AG->vexnum>>AG->arcnum;
	num = AG->vexnum;
	cout<<"输入节点内容"<<endl; 
	for(int i=1;i<=AG->vexnum;i++)//创建邻接表和逆邻接表 
	{
		cin>>AG->vertices[i].data;//输入表头内容 
		RAG->vertices[i].data =  AG->vertices[i].data;
		AG->vertices[i].firstarc = NULL;
		RAG->vertices[i].firstarc = NULL;
	}
	cout<<"有向图还是无向图??有向图输入1,无向图输入0"<<endl;//判断是什么图?有向还是无向 
	cin>>flag;
	if(flag == 0){
		cout<<"输入一条边的两头"<<endl;
		for(int i=1;i<=AG->arcnum;i++)
		{
			int h,e;
			cin>>h>>e;
			an = (ArcNode*)malloc(sizeof(ArcNode));
			an->adjvex = e;
			an->nextarc = AG->vertices[h].firstarc;
			AG->vertices[h].firstarc = an;
		
			an = (ArcNode*)malloc(sizeof(ArcNode));
			an->adjvex = h;
			an->nextarc = AG->vertices[e].firstarc;
			AG->vertices[e].firstarc = an;
			
			an = (ArcNode*)malloc(sizeof(ArcNode));
			an->adjvex = h;
			an->nextarc = RAG->vertices[e].firstarc;
			RAG->vertices[e].firstarc = an;
			 
		}
		cout<<"OK!"<<endl;
	}
	else
	{
		cout<<"输入一条弧的头和尾"<<endl; 
		for(int i=1;i<=AG->arcnum;i++)
		{
			int h,e;
			cin>>h>>e;
			an = (ArcNode*)malloc(sizeof(ArcNode));
			an->adjvex = e;
			an->nextarc = AG->vertices[h].firstarc;
			AG->vertices[h].firstarc = an;
			
			an = (ArcNode*)malloc(sizeof(ArcNode));
			an->adjvex = h;
			an->nextarc = RAG->vertices[e].firstarc;
			RAG->vertices[e].firstarc = an;
		}
		cout<<"OK!"<<endl;
	}
	
	return AG;
}
void printGraph(ALGraph *AG,int num,ALGraph *RAG)
{
	ArcNode *p;
	cout<<"邻接表"<<endl; 
	for(int i=1;i<=num;i++)//打印输出邻接表 
	{
		cout<<AG->vertices[i].data;
		p = AG->vertices[i].firstarc;
		while(p)
		{
			cout<<"->"<<p->adjvex;
			p = p->nextarc;
		}
		cout<<endl;
	}
	cout<<"逆邻接表"<<endl;//打印输出逆邻接表 
	for(int i=1;i<=num;i++)
	{
		cout<<RAG->vertices[i].data;
		p = RAG->vertices[i].firstarc;
		while(p)
		{
			cout<<"->"<<p->adjvex;
			p = p->nextarc;
		}
		cout<<endl;
	}	
}
void ifHaveSide(ALGraph *AG)
{
	int h,e;
	ArcNode *p;
	cout<<"输入希望查找边的两头"<<endl;//输入一条边的两端,判断是否存在边 
	cin>>h>>e;
	p = AG->vertices[h].firstarc;
	while(p)
	{
		if(p->adjvex == e)
		{
			cout<<"存在"<<endl;
			break;
		}
		p = p->nextarc;
	}
	if(p==NULL)
	{
		cout<<"不存在这条边!"<<endl;
	}	
}
void outdegree(ALGraph *AG,int num)
{
	ArcNode *p;
	int index = 0;
	for(int i=1;i<=num;i++)
	{
		p = AG->vertices[i].firstarc;
		while(p)//循环邻接表输出出度 
		{
			index++;
			p=p->nextarc;
		}	
		cout<<"顶点 "<<AG->vertices[i].data<<" 出度 "<<index<<endl;
		index=0;
	}
}
void indegree(ALGraph *RAG,int num)
{
	ArcNode *p;
	int index = 0;
	for(int i=1;i<=num;i++)
	{
		p = RAG->vertices[i].firstarc;
		while(p)//循环逆邻接表输出入度 
		{
			index++;
			p=p->nextarc;
		}	
		cout<<"顶点 "<<RAG->vertices[i].data<<" 入度 "<<index<<endl;
		index=0;
	}
}
int main()
{
	ALGraph *AG,*RAG;
	RAG = (ALGraph *)malloc(sizeof(ALGraph));
	int arcnum;
	AG = creatALGraph(AG,RAG,arcnum);
	printGraph(AG,arcnum,RAG);
	ifHaveSide(AG);
	system("pause");
	outdegree(AG,arcnum);
	indegree(RAG,arcnum);
} 

输出:
输入节点数和边数
4 5
输入节点内容
1 2 3 4
有向图还是无向图??有向图输入1,无向图输入0
1
输入一条弧的头和尾
1 2
1 3
1 4
2 3
3 4
OK!
邻接表
1->4->3->2
2->3
3->4
4
逆邻接表
1
2->1
3->2->1
4->3->1
输入希望查找边的两头
4 2
不存在这条边!
请按任意键继续. . .
顶点 1 出度 3
顶点 2 出度 1
顶点 3 出度 1
顶点 4 出度 0
顶点 1 入度 0
顶点 2 入度 1
顶点 3 入度 2
顶点 4 入度 2
请按任意键继续. . .

参考、借鉴:
[1]: https://www.cnblogs.com/hslzju/p/5396883.html
[2]:《数据结构(C语言版)》严蔚敏 吴伟民著

发布了1 篇原创文章 · 获赞 1 · 访问量 12

猜你喜欢

转载自blog.csdn.net/qq_38856833/article/details/103465884