有向图的十字链表

 #include <stdio.h>
#include <stdlib.h>

#define MAX_VERTEX_NUM 20
//弧结点
//弧结点结构
//[tailvex | headvex | G.xlist[x].firstIn | G.xlist[x].firstOut]


typedef struct ArcBox{
 int tailvex,headvex;
 struct ArcBox *hlink,*tlink;
 //InfoType *info;
}ArcBox;
//头结点
typedef char VertexType;
typedef struct{
 VertexType data;
 int   weight;   //权
 ArcBox *firstIn,*firstOut;
}VexNode;

typedef struct{
 VexNode xlist[MAX_VERTEX_NUM];
 int  vexnum,arcnum;
}OLGraph;

void CreateOLGraph(OLGraph &G);
int  LocateVex(OLGraph G,char u);
void main()
{
 OLGraph G;
 CreateOLGraph(G);
}

void CreateOLGraph(OLGraph &G)
{
 printf("Input vexnum,arcnum:/n");
 scanf("%d,%d",&G.vexnum,&G.arcnum);

 for(int ix=0; ix<G.vexnum; ++ix)
 {
  int index=ix+1;
  printf("Input the %dth data and weight/n",index);
  fflush(stdin);
  scanf("%c,%d",&G.xlist[ix].data,&G.xlist[ix].weight);
  G.xlist[ix].firstIn  = NULL;
  G.xlist[ix].firstOut = NULL;
 }
 for(int arc=0; arc<G.arcnum; ++arc)
 {
  char v1,v2;
  printf("Input <v1,v2>/n");
  fflush(stdin);
  scanf("%c,%c",&v1,&v2);

  int i = LocateVex(G,v1);
  int j = LocateVex(G,v2);

  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].firstIn  = p;
  G.xlist[i].firstOut = p;

 }
}

int  LocateVex(OLGraph G,char u)
{
 int ix;
 for(ix=0; ix<G.vexnum; ++ix)
 {
  if(u == G.xlist[ix].data) return ix;
 }
 
 if(ix == G.vexnum) 
 {
  printf("Input error!");
  exit(-1);
 }

 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值