前面四个头文件都没问题,然后到了源文件出现SeqList矛盾,求大神解释

AKK188888881 2020-01-25 08:47:46
(第一部分是SeqList.h)
#include<stdio.h>

#define MaxSize 100

typedef char DataType;

typedef struct
{
DataType list[MaxSize];
int size;
}SeqList;

void ListInitiate(SeqList *L)
{
L->size = 0;
}

int ListLength(SeqList L)
{
return L.size;
}

int ListInsert(SeqList *L,int i, DataType x)
{
int j;
if(L->size>=MaxSize)
{
printf("数组已满无法插入!\n");
return 0;
}
else if(i<0||i>L->size)
{
printf("参数i不合法!\n");
return 0;
}
else
{
for(j=L->size;j>i;j--)
L->list[j]=L->list[j-1];
L->list[i]=x;
L->size++;
return 1;
}
}

int ListDelete(SeqList *L,int i,DataType *x)
{
int j;
if(L->size<=0)
{
printf("顺序表已空无数据可删!\n");
return 0;
}
else if(i<0||i>L->size-1)
{
printf("参数i不合法");
return 0;
}
else
{
*x=L->list[i];
for(j=i+1;j<=L->size-1;j++)L->list[j-1]=L->list[j];
L->size--;
return 1;
}

}

int ListGet(SeqList L,int i,DataType *x)
{
if(i<0||i>L.size-1)
{
printf("参数i不合法!\n");
return 0;
}
else
{
*x=L.list[i];
return 1;
}
}

(第二部分AdjMGraph.h)
#include<stdio.h>
#include<stdlib.h>

#include"SeqList.h"

#define MaxVertices 10
#define MaxWeight 10000

typedef struct
{
SeqList Vertices;
int edge[MaxVertices][MaxVertices];
int numOfEdges;
}AdjMGraph;

void Initiate(AdjMGraph *G,int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(i==j)G->edge[i][j]=0;
else G->edge[i][j]=MaxWeight;
}

G->numOfEdges=0;
ListInitiate(&G->Vertices);
}

void InsertVertex(AdjMGraph *G,DataType vertex)
{
ListInsert(&G->Vertices,G->Vertices.size,vertex);
}

void InsertEdge(AdjMGraph *G,int v1,int v2,int weight)
{
if(v1<0||v1>G->Vertices.size||v2<0||v2>G->Vertices.size)
{
printf("参数v1或v2越界出错!\n");
exit(1);
}

G->edge[v1][v2]=weight;
G->numOfEdges--;
}

void DeleteVerten(AdjMGraph *G,int v)
{
int n=ListLength(G->Vertices),i,j;
DataType x;

for(i=0;i<n;i++)
for(j=0;j<n;j++)
if((i=v)||(j=v)&&G->edge[i][j]>0&&G->edge[i][j]<MaxWeight)
G->numOfEdges--;
for(i=v;i<n;i++)
for(j=0;j<n;j++)
G->edge[i][j]=G->edge[i+1][j];
for(i=0;i<n;i++)
for(j=v;j<n;j++)
G->edge[i][j]=G->edge[i][j+1];
ListDelete(&G->Vertices,v,&x);
}

int GetFirstVex(AdjMGraph G,int v)
{
int col;

if(v<0||v>G.Vertices.size)
{
printf("参数v1越界出错!\n");
exit(1);
}

for(col=0;col<G.Vertices.size;col++)
if(G.edge[v][col]>0&&G.edge[v][col]<MaxWeight) return col;
return -1;
}

int GetNextVex(AdjMGraph G,int v1,int v2)
{
int col;

if(v1<0||v1>G.Vertices.size||v2<0||v2>G.Vertices.size)
{
printf("参数v1或v2越界出错!\n");
exit(1);
}

for(col=v2+1;col<G.Vertices.size;col++)
if(G.edge[v1][col]>0&&G.edge[v1][col]<MaxWeight) return col;
return -1;
}

(第三部分AdjMGraphCreate.h)
#include<stdio.h>
#include"AdjMGraph.h"

typedef struct
{
int row;
int col;
int weight;
}RowColWeight;

void CreatGraph(AdjMGraph *G,DataType V[],int n,RowColWeight E[],int e)
{
int i,k;
Initiate(G,n);
for(i=0;i<n;i++)
InsertVertex(G,V[i]);

for(k=0;k<e;k++)
InsertEdge(G,E[k].row,E[k].col,E[k].weight);
}

(第四部分Prim.h)
#include<stdio.h>
#include"AdjMGraph.h"

typedef char VerT;

typedef struct
{
VerT vertex;
int weight;
}MinSpanTree;

void Prim(AdjMGraph G,MinSpanTree closeVertex[])
{
VerT x;
int n=G.Vertices.size,minCost;
int *lowCost=(int *)malloc(sizeof(int)*n);
int i,j,k;

for(i=1;i<n;i++)
lowCost[i]=G.edge[0][i];

ListGet(G.Vertices,0,&x);
closeVertex[0].vertex=x;
lowCost[0]=-1;

for(i=1;i<n;i++)
{
minCost=MaxWeight;
for(j=1;j<n;j++)
{
if(lowCost[j]<minCost&&lowCost[j]>0)
{
minCost=lowCost[j];
k=j;
}
}


ListGet(G.Vertices,k,&x);
closeVertex[i].vertex=x;
closeVertex[i].weight=minCost;
lowCost[k]=-1;
for(j=1;j<n;j++)
{
if(G.edge[k][j]<lowCost[j])
lowCost[j]=G.edge[k][j];
}
}
}

(第五部分测试普利姆算法.c)
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

typedef char DataType;
typedef char VerT;

#define MaxSize 100
#define MaxVertices 10
#define MaxWeight 10000
#define N 7

#include"AdjMGraph.h"
#include"AdjMGraphCreate.h"
#include"Prim.h"

void main(void)
{
AdjMGraph g;
char a[]={'A','B','C','D','E','F','G'};
RowColWeight rcw[]={{0,1,50},{1,0,50},{0,2,66},{2,0,60},{1,3,65},{3,1,65},{1,4,40},{4,1,40},{2,3,52},{3,2,52},{2,6,45},{6,2,45},{3,4,50},{4,3,50},{3,5,30},{5,3,30},{3,6,42},{6,3,42},{4,5,70},{5,4,70}};
int n=7,e=20,i;
MinSpanTree closeVertex[7];

CreatGraph(&g,a,n,rcw,e);
Prim(g,closeVertex);
printf("初始结点=%c\n",closeVertex[0].vertex);
for(i=1;i<n;i++)
printf("结点=%c 边的权值=%d\n",closeVertex[i].vertex,closeVertex[i].weight);
}
...全文
192 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
GKatHere 2020-01-26
  • 打赏
  • 举报
回复
没做好吧。 在每个.h的开头,马上加一个 #pragma once (我的是c++) 你你这此文件实测,加上过后没问题,能生成
AKK188888881 2020-01-25
  • 打赏
  • 举报
回复
现在加一个测试迪克迪特拉斯函数也是 (第六部分Dijkstra.h) #include<stdio.h> #include"AdjMGraph.h" void Dijkstra(AdjMGraph G,int v0,int distance[],int path[]) { int n=G.Vertices.size; int *s=(int *)malloc(sizeof(int)*n); int minDis,i,j,u; for(i=0;i<n;i++) { distance[i]=G.edge[v0][i]; s[i]=0; if(i!=v0&&distance[i]<MaxWeight)path[i]=v0; else path[i]=-1; } s[v0]=1; for(i=1;i<n;i++) { minDis=MaxWeight; for(j=0;j<n;j++) if(s[j]==0&&distance[j]<minDis) { u=j; minDis=distance[j]; } if(minDis==MaxWeight)return; s[u]=1; for(j=0;j<n;j++) if(s[j]==0&&G.edge[u][j]<MaxWeight&&distance[u]+G.edge[u][j]<distance[j]) { distance[j]=distance[u]+G.edge[u][j]; path[j]=u; } } } (第七部分 测试迪克斯特拉函数.c) #include<stdio.h> #include<stdlib.h> #include"AdjMGraph.h" #include"AdjMGraphCreate.h" #include"Dijkstra.h" void main(void) { AdjMGraph g; char a[]={'A','B','C','D','E','F'}; RowColWeight rcw[]={{0,2,5},{0,3,30},{1,0,2},{1,4,8},{2,1,15},{2,5,7},{4,3,4},{5,3,10},{5,4,18}}; int i,n=6,e=9; int distance[6],path[6]; GreatGraph(&g,a,n,rcw,e); Dijkstra(g,0,distance,path); printf("从结点%c到其他各节点的最短距离为:\n",g.Vertices.list[0]); for(i=0;i<n;i++) printf("到结点%c的最短距离为%d\:n",g.Vertices.list[i],distance[i]); printf("从结点%c到其他各节点最短路径的前一结点为:\n",g.Vertices.list[0]); for(i=0;i<n;i++) if(path[i]!=-1) printf("到结点%c的前一结点为%c:\n",g.Vertices.list[i],g.Vertices.list[path[i]]); }
AKK188888881 2020-01-25
  • 打赏
  • 举报
回复
不是,防包含会更混。。。错误更多
GKatHere 2020-01-25
  • 打赏
  • 举报
回复
#pragma once 或者 #define .....
AKK188888881 2020-01-25
  • 打赏
  • 举报
回复
以上是编译器结果,求大神解答

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧