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