图的邻接矩阵储存代码(还有点问题,for循环不太对劲

//图的邻接矩阵储存 
#include<stdio.h>
#include<stdlib.h>
#define maxsize 20 
typedef struct{
    //图的顶点和边的个数 
    int vtex,e;
    //图的顶点的值 
    char dd[maxsize];
    //图的边的顶点 
    int mtx[10][10];
}Mgraph;
 
//求给定顶点在数组中的位置
int locate(Mgraph *g,char v){
    int i;
    for(i=0;i<g->vtex;i++){
        if(v==g->dd[i]);
        return i;
    }
    return -1;

//初始化无向无权图 
Mgraph creatUDG(){
    Mgraph *g=( Mgraph *)malloc(sizeof(Mgraph));
    
     printf("请输入图的顶点个数:");
     scanf("%d",&g->vtex);
     printf("请输入图的边的个数:");
     scanf("%d",&g->e);
      
    int i,j;
    for(i=0;i<g->vtex;i++){
        for(j=0;j<g->e;j++){
        g->mtx[i][j]=0;    
        }
    }    
    //给边 
    int m,n;
    for(i=0;i<g->e;i++){
        printf("请输入边的顶点:");
        char v1,v2;
        scanf("%c%c",&v1,&v2);
        m=locate(g,v1);
        n=locate(g,v2);
        g->mtx[m][n]=g->mtx[n][m]=1;
    }    
}
 
//初始化有向无权图
Mgraph creatDF(){
    Mgraph *g=( Mgraph *)malloc(sizeof(Mgraph));
    
     printf("请输入图的顶点个数:");
     scanf("%d",&g->vtex);
     printf("请输入图的边的个数:");
     scanf("%d",&g->e);
      
    int i,j;
    for(i=0;i<g->vtex;i++){
        for(j=0;j<g->e;j++){
        g->mtx[i][j]=0;    
        }
    }    
    
    //给边 
    int m,n;
    for(i=0;i<g->e;i++){
        printf("请输入边的顶点:");
        char v1,v2;
        scanf("%c%c",&v1,&v2);
        m=locate(g,v1);
        n=locate(g,v2);
        g->mtx[m][n]=1;
    }    

//初始化无向有权图
Mgraph creatUG(){
    Mgraph *g=( Mgraph *)malloc(sizeof(Mgraph));
    
     printf("请输入图的顶点个数:");
     scanf("%d",&g->vtex);
     printf("请输入图的边的个数:");
     scanf("%d",&g->e);
      
    int i,j;
    for(i=0;i<g->vtex;i++){
        for(j=0;j<g->e;j++){
        g->mtx[i][j]=0;    
        }
    }    
    //给边 
    int m,n;
    for(i=0;i<g->e;i++){
        printf("请输入边的起点和终点以及每条边的权重:");
        char v1,v2;
        int w;
        scanf("%c%c%d",&v1,&v2,&w);
        m=locate(g,v1);
        n=locate(g,v2);
        g->mtx[m][n]=g->mtx[n][m]=1;
        
    }    
}
 
 
//初始化有向有权图  
Mgraph creatDN(){
    Mgraph *g=( Mgraph *)malloc(sizeof(Mgraph));
    
     printf("请输入图的顶点个数:");
     scanf("%d",&g->vtex);
     printf("请输入图的边的个数:");
     scanf("%d",&g->e);
      
    int i,j;
    for(i=0;i<g->vtex;i++){
        for(j=0;j<g->e;j++){
        g->mtx[i][j]=0;    
        }
    }    
    
    //给边 
    int m,n;
    for(i=0;i<g->e;i++){
        printf("请输入边的起点和终点以及每条边的权重:");
        char v1,v2;
        int w;
        scanf("%c%c%d",&v1,&v2,&w);
        m=locate(g,v1);
        n=locate(g,v2);
        g->mtx[m][n]=1;
    }    

 
 
int main(){
    int a;
    printf("请选择要创建的图的类型(0为无向无权图,1为有向无权图,2为无向有权图,3为有向有权图)"); 
     scanf("%d",&a);
     switch(a)
    {
         
         case 0:
             creatUDG();
             break;
         case 1:
             creatDF();
             break;
        case 2:
             creatUG();
             break;     
        case 3:
             creatDN();
             break;     
    }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值