#include<iostream>
#include<stdio.h>
using namespace std;
#include<malloc.h>
#include<string.h>
#define MAX_NAME 5 //顶点字符串的最大长度
#define MAX_VERTEX_NUM 20 //顶点最大长度
#define OK 1
#define FALSE -1
typedef char VertexType[MAX_NAME];
typedef int InfoType;
typedef struct ArcNode
{
int adjvex; //该弧所指向的顶点的位置
struct ArcNode *nextarc; //指向下一条弧的指针
InfoType *info; //该弧相关信息
}ArcNode;
typedef struct VNode
{
VertexType data; //顶点信息, 如 v1, v2,v3
ArcNode *firstarc; //指向第一条依附该顶点的弧的指针
}VNode,AdjList[MAX_VERTEX_NUM];
typedef struct
{
AdjList vertices;
int vexnum, arcnum; //图的当前顶点数和弧数
int king; //图的种类标志
}ALGraph;
int LocateVex(ALGraph &G, VertexType &u) //返回顶点u在vertices数组中的位置
{
for(int i = 0; i < G.vexnum; ++i)
if(strcmp(G.vertices[i].data,u) == 0)
return i;
return FALSE;
}
int CreateDG(ALGraph &G)
{
int IncInfo, i, j, k;
VertexType v1,v2;
cout << "请输入有向图的顶点数:" << endl;
cin >> G.vexnum;
cout << "请输入有向图的弧数:" << endl;
cin >> G.arcnum;
cout << "请输入有向图的弧是否含有其它信息:(有:1 无:0)" << endl;
cin >> IncInfo;
cout << "请输入有向图的所有顶点:" << endl;
for (i = 0; i < G.vexnum; ++i) //初始化G.vertices数组
{
cout << "请输入第" << i+1 << "个顶点" << endl;
scanf("%s",&G.vertices[i].data);
//gets(G.vertices[i].data);
G.vertices[i].firstarc = NULL;
}
cout << "请输入弧,如:(v1->v2)" << endl;
for (k = 0; k < G.arcnum; ++k)
{
cout << "请输入第" << k+1 << "条弧" << endl;
cin >> v1;
cout << v1 << "->";
cin >> v2;
i = LocateVex(G,v1); //如果v1存在,则返回v1在数组G.vertices[]中的位置,否则返回-1
j = LocateVex(G,v2);
/* while(i<0 || i > G.vexnum-1 || j<0 || j > G.vexnum-1)
{
cout << "找不到该弧所依附的顶点,请重新输入:" << endl;
cout << "请输入第" << k+1 << "条弧" << endl;
cin >> v1;
cout << "->";
cin >> v2;
i = LocateVex(G,v1); //如果v1存在,则返回v1在数组G.vertices[]中的位置,否则返回-1
j = LocateVex(G,v2);
} */
ArcNode *p;
p = (ArcNode *)malloc(sizeof(ArcNode)); //开辟内存空间
if(!p)
{
cout << "溢出" << endl;
return 0;
}
p->adjvex = j; //这里运用了单链表的逆序构建
p->info = NULL;
p->nextarc = G.vertices[i].firstarc; //头插法
G.vertices[i].firstarc = p;
if(IncInfo)
{
cout << "请输入单链表的其它信息:" << endl;
cin >> *(p->info);
} // if end
} // for end
return OK;
} // CreateDG() end
void DisplayDG(ALGraph &G)
{
cout << "邻接表:" << endl;
for(int i = 0; i < G.vexnum; ++i)
{
cout << G.vertices[i].data << ' ';
for(ArcNode *p = G.vertices[i].firstarc; p!=NULL; p=p->nextarc)
{
cout << p->adjvex << ' ';
}
cout << endl;
}
}
void DestroyDG(ALGraph &G)
{
ArcNode *q;
for(int i = 0; i < G.vexnum; ++i)
for (ArcNode *p = G.vertices[i].firstarc; p!=NULL; )
{
q = p;
p = p->nextarc;
free(q);
cout<<"销毁图!"<<endl;
}
}
void du(ALGraph G) {//计算入度与出度
int a[G.vexnum];
int b[G.vexnum];
for(int i=0;i<G.vexnum;i++){//初始化数组
a[i]=0;
b[i]=0;
}
for(int i=0;i<G.vexnum;i++){//计算出度
ArcNode *p=G.vertices[i].firstarc;
while(p!=NULL){
a[i]++;
p=p->nextarc;
}
cout<<G.vertices[i].data<<"的出度为:"<<a[i]<<endl;
}
for(int i=0;i<G.vexnum;i++){//计算入度
ArcNode *p=G.vertices[i].firstarc;
while(p!=NULL) {
int k=p->adjvex;
b[k]++;
p=p->nextarc;
}
}
for(int i=0;i<G.vexnum;i++){
cout<<G.vertices[i].data<<"的入度为:"<<b[i]<<endl;
}
}
int main()
{
ALGraph G;
CreateDG(G);
DisplayDG(G);
DestroyDG(G);
du(G);
return 0;
}
邻接表
最新推荐文章于 2025-07-20 10:33:35 发布
