N—E—E 2021-10-24 23:17 采纳率: 59.5%
浏览 76
已结题

这个图的邻接表实现为啥出错?

运行之后输了一组数据就直接segmentaton fault了


#include <stdio.h>
#include <stdlib.h>
#define MaxVerNum 100

typedef int Vertex;  //每个vertex的序号
typedef char DataType;

//边
struct ENode{
    Vertex V1,V2;
    int Weight;
};
typedef struct ENode *Edge;

//每个节点不作表的头节点
typedef struct AdjNode *PtrToAdjNode;
struct AdjNode{
    Vertex VAdj;
    PtrToAdjNode Next;
    int Weight;
};

//每个vertex作为邻接表的头节点时
struct VNode{
    PtrToAdjNode First;
    DataType data[MaxVerNum][10];  //可选:存储的数据
};
typedef struct VNode List;

//邻接表实现
struct LGragh{
    int Nv;
    int Ne;
    List NodeList[MaxVerNum];
};
typedef struct LGragh *Gragh;

Gragh Initialize(int VerNum);
void InsertEdge(Gragh G,Edge E);
Gragh BuildGragh();
#include "Gragh-link.h"

Gragh Initialize(int VerNum){
    Gragh G = (Gragh)malloc(sizeof(struct LGragh));
    G->Nv = VerNum;
    G->Ne = 0;
    for (int i = 0;i < G->Nv-1;i++)
    {
        G->NodeList[i].First = NULL;
    }
    return G;
}
void InsertEdge(Gragh G,Edge E)
{
    PtrToAdjNode NewNode0 = (PtrToAdjNode)sizeof(struct AdjNode);
    NewNode0->Weight = E->Weight;
    NewNode0->VAdj = E->V2;
    //下面开始插入,插到表头后面
    NewNode0->Next = G->NodeList[E->V1].First;
    G->NodeList[E->V1].First = NewNode0;
    //若为无向图,还要执行以下代码
    PtrToAdjNode NewNode1 = (PtrToAdjNode)sizeof(struct AdjNode);
    NewNode1->Weight = E->Weight;
    NewNode1->VAdj = E->V1;

    NewNode1->Next = G->NodeList[E->V2].First;
    G->NodeList[E->V2].First = NewNode1;

}
Gragh BuildGragh(){
    int Nv,Ne;
    Edge E;
    do
    {
        printf("Please enter a number less than 100:");
        scanf("%d",&Nv);
    } while (Nv >= 100);
    Gragh G = Initialize(Nv);

    printf("Input the number of edges:");
    scanf("%d",&Ne);
    G->Ne = Ne;
    if (G->Ne != 0)
    {
        E = (Edge)malloc(sizeof(struct ENode));
        printf("please add edges for your gragh.format:Vec1 Vec2 Weight.\n");
        for (int i = 0;i < G->Ne;i++)
        {
            scanf("%d %d %d",&E->V1,&E->V2,&E->Weight);
            InsertEdge(G,E);
        }
        
    }
    /*以下是可选的数据录入*/
    //printf("Please add data for each vertex.");
    //for (int i = 0;i < G->Nv;i++)
    //{
    //    scanf("%s",G->NodeList[i].data[i]);
    //} 
    return G;    
    
    
}

int main()
{
    Gragh G = BuildGragh();
    PtrToAdjNode Neighbour;
    int i = 0;
    for (;i < G->Nv;i++)
        Neighbour = G->NodeList[i].First;
        printf("---------%d----------\n",i);
        while (Neighbour)
        {
            printf("%-3d%-3d\n",Neighbour->VAdj,Neighbour->Weight);
            Neighbour = Neighbour->Next;
        }

    system("pause");
    return 0;
}

img

  • 写回答

2条回答 默认 最新

  • 广大菜鸟 2021-10-24 23:24
    关注

    爆红的那句上面应该是缺了malloc

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月8日
  • 已采纳回答 10月31日
  • 创建了问题 10月24日

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算