文档介绍:邻接表:
#define max_vertex_num 20 //最大顶点数
struct ode
{
int adjvex;
struct ode *nextarc;
infotype info;// 和弧有关的其它信息
};
typedef struct ode *arcptr;
typedef struct vexnode
{
vextype vexdata;// 和顶点有关的信息
arcptr firstarc;
}adjlist[max_vertex_num+1];
void setadjlist(adjlist graph) //根据所读入的边,建立图的邻接表graph
{
int v1,v2,i;
for(i=1;i<=max_vertex_num;i++)
graph[i].firstarc=NULL;
arcptr p=NULL,q=NULL;
scanf("%d%d",&v1,&v2); //读入第一条边(v1,v2)
while (v1!=0) //边的结束标志v1=0
{
q=(arcptr)malloc(sizeof(ode));
q->adjvex=v2;q->nextarc=NULL;
if (graph[v1].firstarc==NULL)
graph[v1].firstarc=q;
else
{
p=graph[v1].firstarc;
while (p->nextarc!=NULL)
p=p->nextarc;
p->nextarc=q;
}
scanf("%d%d",&v1,&v2);//读下一条边
}
} // end of setadjlist
十字链表形式:
struct arctype
{
int tailvex,headvex;
arctype *hlink,*tlink;
};
typedef struct arctype *arclink;
typedef struct vnode
{
vertex data;
arclink firstin,firstout;
}ortholist[max_vertex_num+1];
void crt_ortho(ortholist ga)//建立有向图的十字链表存储结构
{
int n,e,k,i,j;
arclink p,q;
scanf("%d%d",&n,&e);//输入顶点和弧的数目
getchar();
for (i=1;i<=n;i++)
{
scanf("%c",&ga[i].data);//输入顶点信息的函数
ga[i].firstin=NULL; ga[i].firstout=NULL; //指针初始化
}
for (k=1;k<=e;k++)
{
scanf("%d%d",&i,&j);//输入弧的信息,i是弧尾顶点的编号,j是弧头顶点的编号
q=(arclink)malloc(sizeof(arclink));
q->headvex=j;
q->tailvex=i;
q->hlink=q->tlink=NULL;
if(NULL==ga[j].firstout)
ga[j].firstout=q;
else
{
p=ga[j].firstout;
while(p->tlink)
p=p->tlink;
p->tlink=q;
}
if(NULL==ga[i].firstin)
ga[i].firstin=q;
else
{
p=ga[i].firstin;
while(p->hlink)
p=p->hlink;
p->hlink=q;
} //将弧结点分别插入到两个链表中
}
}//end of crt-ortho
边集数组:
struct edge //定义边集数组的元素类型
{
int fromvex;//边的起点域
int endvex;//边的终点域
int weight;//边的权值域,对应无权图可省去此域
};
typedef struct edge edgeset[max_arc_num+1];//定义edgeset为边集数组类型
void createdgeset(vextype gv[],edgeset ge,int n,int e)
{ //通过从键盘上输入的n个顶点信息和e条边的信息
//建立顶点数组gv和边集数组ge
int