• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Jesus Program
博客园    首页    新随笔    联系   管理    订阅  订阅
图的邻接表

 

Code
#include<stdio.h>
#include
<stdlib.h>
#define NULL 0
#define MAX 20
typedef 
struct Anode
{
 
int adj;
 
struct Anode *next;
}Anode;
typedef 
struct Vnode
{
 
int data;
 Anode 
*first;
}Vnode,Adj[MAX];
typedef 
struct
{
 Adj vertices;
 
int vexnum,arcnum;
 
}Graph;
int LocateVex(Graph *G,int v)
{
 
int n=0;
 
while(G->vertices[n].data!=v)
 {
  n
++;
 }
 
return n;
}
void CreateGraph(Graph *G)
{
 
int i,j,m;
 
int v1,v2;
 Anode 
*a1,*a2;
 printf(
"input num of vex and arc:\n");
 scanf(
"%d,%d",&G->vexnum,&G->arcnum);
 printf(
"input value of vex:\n");
 
for(i=0;i<G->vexnum;i++)
 {
  scanf(
"%d",&G->vertices[i].data);
  G
->vertices[i].first=NULL;
 }
 printf(
"input v1 and v2:\n");
 
for(m=0;m<G->arcnum;m++)
 {
  scanf(
"%d,%d",&v1,&v2);
  i
=LocateVex(G,v1);
  j
=LocateVex(G,v2);
 
  a1
=(Anode *)malloc(sizeof(Anode));
 
if(!a1)
  printf(
"ERROR!\n");
    
if(!G->vertices[i].first)
 {
  G
->vertices[i].first=a1;
  a1
->adj=j;
  a1
->next=NULL;
  a2
=a1;
 }
 
else
 {
  a2
->next=a1;
  a1
->adj=j;
  a1
->next=NULL;;
  a2
=a1;
 }
 }
   
}
void Out_Graph(Graph *G)
{
 Anode 
*a1;
 
int i;
 printf(
"output:\n");
 
for(i=0;i<G->vexnum;i++)
 {
  printf(
"%d",G->vertices[i].data);
  printf(
"\t");
  a1
=G->vertices[i].first;
     
while(a1)
  {
   printf(
"%d\t",a1->adj);
   a1
=a1->next;
  }
  printf(
"\n");
 }
}
int main()
{
 Graph G;
 CreateGraph(
&G);
 Out_Graph(
&G);
 
return 0;
}

 

posted on 2009-05-12 18:48  Jesus Program  阅读(350)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3