gpt4 book ai didi

c - xcode和vs不同的IDE有问题

转载 作者:行者123 更新时间:2023-11-30 16:38:09 24 4
gpt4 key购买 nike

我同时使用 Xcode 和 VS2017。但是下面的代码在 Xcode 中工作正常,并得到正确的输出。但是在 vs 中,当使用函数 Printlist() 时,它停止工作并进入循环:

Please input the number of vertexes:
5
Please input the number of edges:
6
Please input each vertex's imformation:
A
B
C
D
E
Please input the relationship between two vertexes:
A B
A D
B C
C D
B E
C E
The Adjacency Matrix you've entered is:
0 1 0 1 0
1 0 1 0 1
0 1 0 1 1
1 0 1 0 0
0 1 1 0 0
The converted adjacency list is:
(0)A->3 1
(1)B->4 2 0
(2)->4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4 3 4................

该程序是创建一个邻接矩阵并将其转换为列表。我在网上搜索了很长时间。但没有用。请帮助或尝试给出一些如何实现这一目标的想法。预先感谢:)

#include <stdio.h>
#include <stdlib.h>
#define vexnum 20
#define isLetter(a) ((((a)>='a')&&((a)<='z')) || (((a)>='A')&&((a)<='Z')))
int visited[vexnum];
typedef struct {
char vexs[vexnum];
int AdjMatrix[vexnum][vexnum];
int n, e; //VEXNUM and EDGENUM
}MGraph;
typedef struct ArcNode
{
int adjvex;
struct ArcNode *nextarc;
}ArcNode;
typedef struct VNode
{
char data;
ArcNode *firstarc;
}VNode, AdjList[vexnum];
int GetPosition(MGraph G, char ch)
{
int i;
for (i = 0; i<G.n; i++)
{
if (G.vexs[i] == ch)
return i;
}
return -1;
}
void CreatMGraph(MGraph *G, int n, int e)
{
int i, j, k;
int p = 0, q = 0;
char a, b;
printf("Please input each vertex's imformation:\n");
for (i = 0; i<n; i++)
{
do
{
G->vexs[i] = getchar();
} while (!isLetter(G->vexs[i]));
}
for (i = 0; i<n; i++)
{
for (j = 0; j<n; j++)
{
G->AdjMatrix[i][j] = 0;
}
}
printf("Please input the relationship between two vertexes:\n");
for (k = 0; k<G->e; k++)
{
do
{
a = getchar();
} while (!isLetter(a));
do
{
b = getchar();
} while (!isLetter(b));
p = GetPosition(*G, a);
q = GetPosition(*G, b);
G->AdjMatrix[p][q] = G->AdjMatrix[q][p] = 1;
}
}
void PrintAdjMatrix(MGraph G)
{
int i, j;
printf("The Adjacency Matrix you've entered is:\n");
for (i = 0; i<G.n; i++)
{
for (j = 0; j<G.n; j++)
{
printf("%d ", G.AdjMatrix[i][j]);
if (j == G.n - 1)
printf("\n");
}
}
}
void MatrixToList(MGraph G, AdjList *L)
{
int i, j;
ArcNode *p;
for (i = 0; i<G.n; i++)
{
L[i]->data = G.vexs[i];
L[i]->firstarc = NULL;
}
for (i = 0; i<G.n; i++)
{
for (j = 0; j<G.n; j++)
{
if (G.AdjMatrix[i][j] == 1)
{
if (L[i]->firstarc == NULL)
{
p = (ArcNode *)malloc(sizeof(ArcNode));
p->adjvex = j;
p->nextarc = NULL;
L[i]->firstarc = p;
}
else
{
p = (ArcNode *)malloc(sizeof(ArcNode));
p->adjvex = j;
p->nextarc = L[i]->firstarc;
L[i]->firstarc = p;
}
}
}
}
}
void Printlist(MGraph G, AdjList *L)
{

ArcNode *p;
int i;
for (i = 0; i<G.n; i++)
{
printf("(%d)%c->", i, L[i]->data);
p = (ArcNode *)malloc(sizeof(ArcNode));
p = L[i]->firstarc;
while (p)
{
printf("%d ", p->adjvex);
p = p->nextarc;
}
printf("\n");
}
}

int main()
{
MGraph G;
printf("Please input the number of vertexes:\n");
scanf("%d", &G.n);
printf("Please input the number of edges:\n");
scanf("%d", &G.e);
CreatMGraph(&G, G.n, G.e);
PrintAdjMatrix(G);
AdjList *L = NULL;
L = (AdjList *)malloc(sizeof(AdjList));
MatrixToList(G, L);
printf("The converted adjacency list is:\n");
Printlist(G, L);
return 0;
}

最佳答案

参数L的下标在函数 MatrixToList()Printlist()是错的。考虑参数声明AdjList *L :L是指向 AdjList 的指针,它又被定义为 vexnum 的数组struct VNode s,所以L是指向整个数组 struct VNode 的指针s。然后表达式为 L[i]->data不是i访问数组中的第一个节点,但 i数组的第一个数组(对于 i > 0 不存在)数组(并且 ->datadata 元素位于该 i 的第一个节点中)。要修复该错误,您可以将上述函数中的参数声明更改为 AdjList L以及 L[i]-> 的每个实例在这些函数中 L[i].并调用

    MatrixToList(G, *L);

    Printlist(G, *L);

关于c - xcode和vs不同的IDE有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47604803/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com