元亨利贞t 2019-05-20 23:17 采纳率: 0%
浏览 387

数据结构图(用C语言)当中为什么邻接表用结构体变量报错,用邻接矩阵不报错?

我写的一个邻接表代码

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 10
struct ArcNode
{    int adjvex; 
     ArcNode *next;
};
struct VertexNode 
{
      int vertex;
      ArcNode *firstedge;
};
int visited[MAXSIZE];
typedef struct Graph
{    
      VertexNode adjlist[MAXSIZE];   
       int vertexNum, arcNum;       
}ALGraph ;
\\初始化邻接表
void InitALGraph(ALGraph g, int a[ ], int n, int e)
{   
    g.vertexNum=n; g.arcNum=e; 
    int i,j;
    for (j=0; j<MAXSIZE; j++) 
        g.adjlist[j].firstedge=NULL;
    for (i=0; i<g.vertexNum; i++)   
    //输入顶点信息,初始化边表
    {
       g.adjlist[i].vertex=a[i];

    } 
    int k;
    for (k=0; k<g.arcNum; k++)   
     //输入边的信息存储在边表中
     {
         int j;
         scanf("%d%d",&i,&j);  

        ArcNode* s=(ArcNode*)malloc(sizeof(ArcNode)); s->adjvex=j;              
         s->next=g.adjlist[i].firstedge;    
         g.adjlist[i].firstedge=s;
     }
}

\\深度优先遍历
void DFSTraverse(ALGraph g , int v)
{        
    printf("%d\n",g.adjlist[v].vertex);       visited[v]=1;
    ArcNode * p=g.adjlist[v].firstedge;    
    int i;
    for(i=0;i<g.vertexNum;i++){
        while(p){
            if(p->adjvex==i&&visited[i]==0)
                DFSTraverse(g,i);
            else p=p->next;
        }
    }
}

int main(){
    ALGraph g;
    int a[10]={1,2,3,4};
    InitALGraph(g,a, 4, 4);
     DFSTraverse(g , 0);
}

编译没问题,运行说The variable 'g' is being used without being inititalized
但是这个邻接矩阵代码

#include<stdio.h>
#include<stdlib.h>
#define MAX_VER_NUM 50
int visited[MAX_VER_NUM]={0};
typedef char VertexType;
VertexType Q[MAX_VER_NUM];
typedef enum
{
    DG,UDG
}GraphType;
typedef struct
{
    VertexType vexs[MAX_VER_NUM];        //顶点向量
    int arcs[MAX_VER_NUM][MAX_VER_NUM];  //邻接矩阵
    int vexnum,arcnum;                   //图的当前顶点数和弧数
    GraphType type;                      //图的种类标志
}MGraph; 

//1. 根据名称得到指定顶点在顶点集合中的下标
//vex 顶点
//return 如果找到,则返回下标,否则,返回0
int getIndexOfVexs(char vex,MGraph *MG)
{
    int i;
    for(i=1;i<=MG->vexnum;i++)
    {
        if(MG->vexs[i]==vex)
        {
            return i;
        }
    }
    return 0;
}

//2. 创建邻接矩阵
void create_MG(MGraph *MG)
{
    int i,j,k;
    int v1,v2,type;
    char c1,c2;
    printf("Please input graph type DG(1) or UDG(0):");
    scanf("%d",&type);
    if(type==1)
    {
        MG->type=DG;
    }
    else if(type==0)
    {
        MG->type=UDG;
    }
    else
    {
        printf("Please input correct graph type DG(1) or UDG(0)!");
        return;
    }
    printf("Please input vexnum:");
    scanf("%d",&MG->vexnum);
    printf("Please input arcnum:");
    scanf("%d",&MG->arcnum);
    getchar();
    for(i=1;i<=MG->vexnum;i++)
    {
        printf("Please input %dth vex(char):",i);
        scanf("%c",&MG->vexs[i]);
        getchar();
    }
    //初始化邻接矩阵
    for(i=1;i<=MG->vexnum;i++)
    {
        for (j=1;j<=MG->vexnum;j++)
        {
            MG->arcs[i][j]=0;
        }
    }
    //输入边的信息,建立邻接矩阵
    for(k=1;k<=MG->arcnum;k++)
    {
        printf("Please input %dth arc v1(char) v2(char):",k);
        scanf("%c %c",&c1,&c2);
        v1=getIndexOfVexs(c1,MG);
        v2=getIndexOfVexs(c2,MG);
        if(MG->type==DG)
        {
            MG->arcs[v1][v2]=1;
        }
        else
        {
            MG->arcs[v1][v2]=MG->arcs[v2][v1]=1;
        }
        getchar();
    }
}



//3. 打印邻接矩阵和顶点信息
void print_MG(MGraph MG)
{
    int i,j;
    if(MG.type==DG)
    {
        printf("Graph type: Direct graph\n");
    }
    else
    {
        printf("Graph type: Undirect graph\n");
    }
    printf("Graph vertex number: %d\n",MG.vexnum);
    printf("Graph arc number: %d\n",MG.arcnum);
    printf("Vertex set:");
    for(i=1;i<=MG.vexnum;i++)
    {
        printf("%c",MG.vexs[i]);
    }
    printf("\nAdjacency Matrix:\n");
    for(i=1;i<=MG.vexnum;i++)
    {
        for(j=1;j<=MG.vexnum;j++)
        {
            printf("%d",MG.arcs[i][j]);
        }
        printf("\n");
    }
}

//4.  从顶点V出发深度优先遍历图MG
void DFSMG(MGraph MG, VertexType V)
{
    printf("%c",V);
    int v,j;
    v=getIndexOfVexs(V,&MG);
    visited[v]=1;
     for (j=1; j<=MG.vexnum; j++)
         if (MG.arcs[v][j]==1 && visited[j]==0)
           DFSMG(MG, MG.vexs[j]);

}

 //5. 从顶点V出发广度优先遍历图MG
void BFSMG(MGraph MG, VertexType V)
{
    int v,j;
    int front=0, rear=0;   //假设采用顺序队列且不会发生溢出
    printf("%c",V); v=getIndexOfVexs(V,&MG); visited[v]=1;  Q[++rear]=V;
    while (front!=rear)
     {
         VertexType c=Q[++front];  
         v=getIndexOfVexs(c,&MG);
         for (j=1; j<=MG.vexnum; j++)
            if (MG.arcs[v][j]==1 && visited[j]==0 ) {
               printf("%c",MG.vexs[j]); visited[j]=1; Q[++rear]=MG.vexs[j];
            }
      }

}

//主函数
int main(void)
{
    MGraph MG;
    create_MG(&MG);
    print_MG(MG);
    //printf("深度优先遍历序列为:\n");
    //DFSMG(MG,'A');
    //visited[MAX_VER_NUM]={0};
    printf("\n广度优先遍历序列为:\n");
    BFSMG(MG,'A');
    return 0;
}


上面的邻接矩阵代码用的也是结构体变量,为什么又可以执行,是因为邻接表图的结构体中有指针吗,但是以下代码

#include<stdio.h>
struct MyStruct{
    int a;
    int *b;
    MyStruct *c;
};

int main(){
    MyStruct ms;
    ms.a=5;
    printf("%d\n",ms.a);
    return 0;
}

结构体里也有指针,为什么可以执行?

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-05-21 09:25
    关注
    The variable 'g' is being used without being inititalized
    这不是错误,而是VC++的安全警告
    项目->属性->配置属性->C/C++->生成代码->基本运行时检测
    勾掉对未初始化变量运行时的检查
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料