有向图的十字链表存储

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值