#include<stdio.h>
#include<limits.h>
#include<malloc.h>
#define MAX_VERTEX_NUM 20
typedef char VertexType;
typedef struct ArcBox
{
int tailvex,headvex;
struct ArcBox *hlink,*tlink;
} ArcBox;
typedef struct VexNode
{
VertexType data;
ArcBox *firstin,*firstout;
}VexNode;
typedef struct
{
int vexnum,arcnum;
VexNode xlist[MAX_VERTEX_NUM];
}OLGraph;
int locate(OLGraph *g,char ch)
{
int i=0;
for(i=0;i<g->vexnum;i++)
{
if(g->xlist[i].data==ch)
return i;
}
return -1;
}
void createDG(OLGraph *g)
{
int i,j,k;
char ch;
char p,q;
printf("请输入顶点数和弧数:\n");
scanf("%d%d",&(g->vexnum),&(g->arcnum));
//构造表头向量
printf("请输入各个顶点的信息\n");
for(i=0;i<g->vexnum;i++)
{
while(ch!='\n')
{
g->xlist[i].data=ch;
}
g->xlist[i].firstin=NULL;
g->xlist[i].firstin=NULL;
}
for(k=0;k<g->arcnum;k++)
{
//输入起点和终点
p=getchar();
while(p=='\n')
{
printf("不能为换行符,请重新输入");
p=getchar();
}
q=getchar();
while(q=='\n')
{
printf("不能为换行符,请重新输入");
p=getchar();
}
i=locate(g,p);
j=locate(g,q);
ArcBox *p=(ArcBox *)malloc(sizeof(ArcBox));;
p->tailvex=i;
p->headvex=j;
p->hlink=(g->xlist[j]).firstin;
p->tlink=(g->xlist[i]).firstout;
g->xlist[j].firstout=g->xlist[i].firstin=p;
printf("有向图创建成功\n");
}
}
int main()
{
OLGraph g;
createDG(&g);
return 0;
}有向图的十字链表存储
最新推荐文章于 2023-07-25 19:41:06 发布
2324

被折叠的 条评论
为什么被折叠?



