加预定义的办法试过了但是还是报错,不知道是不是程序有错
#include "LGragh.h"
#include "MGragh.h"
#include <stddef.h>
int dist[MaxVerNum];
MVertex parent[MaxVerNum];
void Prim(MGragh G);
MVertex FindMin(MGragh G);
void PrintPrimTree(LGragh G);
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MaxVertexNum 100
#define Infinity 3000
typedef int MVertex;
typedef char DataType;
struct ENode {
MVertex V1, V2;
int Weight;
};
typedef struct ENode* MEdge;
struct GNode {
MVertex Gmat[MaxVertexNum][MaxVertexNum];
DataType Data[MaxVertexNum][10]; //可选:顶点存储的数据
int Nv; //顶点数
int Ne; //边数
};
typedef struct GNode* MGragh;
MGragh Init_M(int VertexNum);
void InsertEdge_M(MGragh G, MEdge E);
MGragh BuildGragh_M();
#include <stdio.h>
#include <stdlib.h>
#define MaxVerNum 100
typedef int LVertex; //每个vertex的序号
typedef char DataType;
//边
struct LENode {
LVertex V1, V2;
int Weight;
};
typedef struct LENode* LEdge;
//每个节点不作表的头节点
typedef struct AdjNode* PtrToAdjNode;
struct AdjNode {
LVertex VAdj;
PtrToAdjNode Next;
int Weight;
};
//每个vertex作为邻接表的头节点时
struct LVNode {
PtrToAdjNode First;
DataType data[MaxVerNum][10]; //可选:存储的数据
};
typedef struct LVNode List;
//邻接表实现
struct LinkGragh {
int Nv;
int Ne;
List NodeList[MaxVerNum];
};
typedef struct LinkGragh* LGragh;
LGragh Init_L(int VerNum);
void InsertEdge_L(LGragh G, LEdge E);
LGragh BuildGragh_L();
void PrintPrimTree(LGragh G);
#include "Prim.h"
void Prim(MGragh G) {
MVertex V, W;
for (V = 0; V < G->Nv; V++)
{
dist[V] = G->Gmat[0][V];
parent[V] = 0; //?
}
dist[0] = 0;
parent[0] = -1;
LGragh MST = BuildGragh_L(G->Nv);
LEdge E = (LEdge)malloc(sizeof(struct LENode));
while (1)
{
V = FindMin(G);
if (V == -1)
break;
E->V1 = parent[V];
E->V2 = V;
E->Weight = dist[V];
InsertEdge_L(MST, E);
dist[V] = 0;
for (W = 0; W < G->Nv; W++)
{
if (!dist[W] && G->Gmat[V][W] < Infinity)
{
dist[W] = G->Gmat[V][W];
parent[W] = V;
}
}
}
PrintPrimTree(G);
}
MVertex FindMin(MGragh G) {
MVertex V, MinV;
int MinD = Infinity;
for (V = 0; V < G->Nv; V++)
{
if (!dist[V] && dist[V] < MinD)
{
MinD = dist[V];
MinV = V;
}
}
if (MinD != Infinity)
return MinV;
return -1;
}
void PrintPrimTree(LGragh G) {
LVertex V;
PtrToAdjNode n0;
for (V = 0; V < G->Nv; V++)
{
n0 = G->NodeList[V].First;
while (n0)
{
printf("%-3d", n0->VAdj);
n0 = n0->Next;
}
printf("\n");
}
}
#include "MGragh.h"
MGragh Init_M(int VertexNum) {
MGragh G = (MGragh)malloc(sizeof(struct GNode));
G->Nv = VertexNum;
G->Ne = 0;
for (int i = 0; i < VertexNum; i++)
{
for (int j = 0; j < VertexNum; j++)
G->Gmat[i][j] = Infinity; //自己到自己也是infinity
}
return G;
}
void InsertEdge_M(MGragh G, MEdge E) {
G->Gmat[E->V1][E->V2] = E->Weight;
G->Gmat[E->V2][E->V1] = E->Weight; //可选:无向图加上这一句
}
MGragh BuildGragh_M() {
int Nv, Ne;
int v1, v2, weight;
MEdge E;
do
{
printf("Please enter a number less than 100:");
scanf("%d", &Nv);
} while (Nv >= 100);
MGragh G = Initialize(Nv);
printf("Input the number of edges:");
scanf("%d", &Ne);
G->Ne = Ne;
if (G->Ne != 0)
{
E = (MEdge)malloc(sizeof(struct ENode));
printf("please add edges for your MGragh.format:Vec1 Vec2 Weight.\n");
for (int i = 0; i < G->Ne; i++)
{
scanf("%d %d %d", &v1, &v2, &weight);
E->V1 = v1;
E->V2 = v2;
E->Weight = weight;
InsertEdge(G, E);
}
}
/*以下是可选的数据录入*/
//printf("Please add data for each vertex.");
//for (int i = 0;i < G->Nv;i++)
//{
// scanf("%s",G->Data[i]);
//}
return G;
}
#include "LGragh.h"
LGragh Init_L(int VerNum) {
LGragh G = (LGragh)malloc(sizeof(struct LinkGragh));
G->Nv = VerNum;
G->Ne = 0;
for (int i = 0; i < G->Nv - 1; i++)
{
G->NodeList[i].First = NULL;
}
return G;
}
void InsertEdge_L(LGragh G, LEdge E)
{
PtrToAdjNode NewNode0 = (PtrToAdjNode)malloc(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)malloc(sizeof(struct AdjNode));
NewNode1->Weight = E->Weight;
NewNode1->VAdj = E->V1;
NewNode1->Next = G->NodeList[E->V2].First;
G->NodeList[E->V2].First = NewNode1;
}
LGragh BuildGragh_L() {
int Nv, Ne;
LEdge E;
do
{
printf("Please enter a number less than 100:");
scanf("%d", &Nv);
} while (Nv >= 100);
LGragh G = Init_L(Nv);
printf("Input the number of edges:");
scanf("%d", &Ne);
G->Ne = Ne;
if (G->Ne != 0)
{
E = (LEdge)malloc(sizeof(struct LENode));
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_L(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;
}
void PrintPrimTree(LGragh G) {
PtrToAdjNode tmp;
for (int i = 0; i < G->Nv; i++)
{
printf("------%d------", i);
if (G->NodeList[i].First)
{
tmp = G->NodeList[i].First;
while (tmp)
printf("顶点%d与顶点%d相连,边权重是%d", i, tmp->VAdj, tmp->Weight);
}
}
}
#include "Prim.h"
int main()
{
MGragh G = BuildGragh_M();
Prim(G);
return 0;
}
这个是警告,可以不用理 要处理也可以,直接int error=scanf 如果scanf的返回值是已经成功赋值的变量个数,如果异常返回0,表示输入不匹配,-1表示输入流结束,你可以直接int error=然后对error分情况讨论 处理,或不处理
在程序开头加上下面这一行代码就行啦
#define _CRT_SECURE_NO_WARNINGS 1
不会依然会报警告,这在vs上是正常的,不影响程序运行
```