创建邻接表(AdjacencyList)
来源:互联网 发布:百度电脑软件管家 编辑:程序博客网 时间:2024/05/19 12:25
#include<stdio.h>
/*----------------------------邻接表的存储结构---------------------------*/
#define MAX_VERTEX_NUM 20
typedef int VRType;
typedef int VertexType;
struct ArcNode {
int adjvex;
struct ArcNode *nextarc;
VRType weight;
};
struct VexNode {
VertexType data;
struct ArcNode *firstarc;
};
typedef struct {
struct VexNode adjlist[MAX_VERTEX_NUM];
int vexnum,arcnum;
}ALGraph;
/*============================================================================*/
int LocateVex(ALGraph *G,VertexType v)
{
int i;
for(i = 0;i < G->vexnum;i ++)
if(v == (G->adjlist[i]).data )
return i;
}
int CreateUDG(ALGraph *G)
{
int i,j,k,kind=1;
VRType weight=0;
VertexType v1,v2;
struct ArcNode *p;
printf(" 1-Create DG/n 2-Create UDG/n CHOICE :");
scanf("%d",&kind);
printf("/n/n The Graph's number of Vertex :"); scanf("%d",&(G->vexnum));
printf(" The Graph's number of Arcnum :"); scanf("%d",&(G->arcnum));
printf("/n");
for(i=0;i<G->vexnum;i++) {
printf(" The Graph's %d Vertex's NAME:",i+1); scanf("%d",&(G->adjlist[i].data));
G->adjlist[i].firstarc = NULL;
}
for(k=0;k<G->arcnum;k++)
{
printf("/n The %d Arc ./n",k+1);
printf(" The tail vertex:");do{ scanf("%d",&v1);if((v1<=0)||(v1>G->vexnum))printf(" ERROR/n The tail vertex:"); }while((v1<=0)||(v1>G->vexnum));
printf(" The head vertex:");do{ scanf("%d",&v2);if((v2<=0)||(v2>G->vexnum))printf(" ERROR/n The head vertex:"); }while((v2<=0)||(v2>G->vexnum));
/*printf(" The arc weight:");do{ scanf("%d",&weight); if(weight < 0) printf(" ERROR/n The arc weight:"); }while(weight < 0);*/
i = LocateVex(G,v1); j = LocateVex(G,v2);
p = (struct ArcNode *)malloc(sizeof(struct ArcNode));
p->adjvex = j;
p->nextarc = G->adjlist[i].firstarc;
p->weight = weight;
G->adjlist[i].firstarc = p;
if(2 == kind){
p = (struct ArcNode *)malloc(sizeof(struct ArcNode));
p->adjvex = i;
p->nextarc = G->adjlist[j].firstarc;
p->weight = weight;
G->adjlist[j].firstarc = p;
}
}
return 0;
}
void Prin_ALGraph(ALGraph *G)
{
int k;
struct ArcNode *p;
printf("/n/n The Adjacenacy Vertex =>/n/n");
for(k = 0;k <G->vexnum;k ++)
{
p = G->adjlist[k].firstarc;
printf(" /tVertex %d :",k+1);
while( p != NULL ){ printf(" (%d,%d) ",G->adjlist[k].data,G->adjlist[p->adjvex].data); p = p->nextarc; }
printf("/n");
}
}
/*------------------------------------------------------------------------------*/
int main()
{
ALGraph G;
CreateUDG(&G);
Prin_ALGraph(&G);
getch();
}
- 创建邻接表(AdjacencyList)
- 邻接表创建图
- 邻接表创建图
- 邻接表的创建
- 邻接表创建图
- 邻接表的创建
- 【数据结构】邻接表创建_CreateALGraph
- 图的创建(邻接表)
- 邻接链表创建图
- 邻接链表的创建和输出(发布)
- 邻接表的创建与输出(C语言)
- 邻接表的定义与创建
- 图的邻接表创建输出
- 图的邻接表的创建
- 图的邻接表创建以及遍历
- 02邻接表创建_CreateALGraph.c
- 邻接表的创建与深搜
- C语言里面邻接表的创建
- 自动升级程序
- 关于压缩和解压缩
- JavaScript字符串函数大全
- June five,2010,PM
- Vmware安装Redhat Linux5.5遇到的奇怪问题
- 创建邻接表(AdjacencyList)
- flash
- 星期六
- hibernate的Dao作为单例时需要注意的问题
- 修改mplayer,使其支持在线无缝播放多个flv片段
- 算命
- Ubuntu 7.10配置SSH服务
- 邻接矩阵(Adjacency Matrix)
- Struts1.x 跨站脚本(XSS)漏洞的解决