N—E—E 2021-10-24 23:17 采纳率: 59.5%

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

``````
#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;

//每个节点不作表的头节点
int Weight;
};

//每个vertex作为邻接表的头节点时
struct VNode{
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)
{
NewNode0->Weight = E->Weight;
//下面开始插入，插到表头后面
NewNode0->Next = G->NodeList[E->V1].First;
G->NodeList[E->V1].First = NewNode0;
//若为无向图，还要执行以下代码
NewNode1->Weight = E->Weight;

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));
for (int i = 0;i < G->Ne;i++)
{
scanf("%d %d %d",&E->V1,&E->V2,&E->Weight);
InsertEdge(G,E);
}

}
/*以下是可选的数据录入*/
//for (int i = 0;i < G->Nv;i++)
//{
//    scanf("%s",G->NodeList[i].data[i]);
//}
return G;

}

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

system("pause");
return 0;
}

``````

• 写回答

#### 2条回答默认 最新

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

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

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

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

#### 悬赏问题

• ¥15 BP神经网络控制倒立摆
• ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
• ¥15 html5+css和javascript有人可以帮吗？图片要怎么插入代码里面啊
• ¥30 Unity接入微信SDK 无法开启摄像头
• ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写