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

1.有向图、无向图邻接表的构造
2.判断边是否存在
3.求顶点的度数

#include<iostream>
#include<stdlib.h>
using namespace std;
#define MAX_NUM 30
typedef struct ArcNode{
struct ArcNode *nextarc;
int weight;
}ArcNode;
typedef struct VNode{
int data;
ArcNode *firstarc;
typedef struct{
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->nextarc = AG->vertices[h].firstarc;
AG->vertices[h].firstarc = an;

an = (ArcNode*)malloc(sizeof(ArcNode));
an->nextarc = AG->vertices[e].firstarc;
AG->vertices[e].firstarc = an;

an = (ArcNode*)malloc(sizeof(ArcNode));
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->nextarc = AG->vertices[h].firstarc;
AG->vertices[h].firstarc = an;

an = (ArcNode*)malloc(sizeof(ArcNode));
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)
{
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)
{
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)
{
{
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

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]: https://www.cnblogs.com/hslzju/p/5396883.html
[2]:《数据结构（C语言版）》严蔚敏 吴伟民著

• 1
点赞
• 0
评论
• 6
收藏
• 一键三连
• 扫一扫，分享海报

05-08 221

12-02 4001
06-06 2万+
12-24 2万+
05-06 1万+
08-26 591
01-14 1万+
01-11 2190
12-09 2973
07-23 1989
02-15 5828
03-27 117
08-25 2475
04-04 1719