不同的IDE xcode和vs有什么不对

时间:2017-12-02 05:06:48

标签: c visual-studio

我正在使用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;
}

1 个答案:

答案 0 :(得分:0)

函数LMatrixToList()中参数Printlist()的下标是错误的。考虑参数声明AdjList *LL是指向AdjList的指针,该指针又被定义为vexnum struct VNode s的数组,因此{{1 }}是指向整个L数组的指针。然后使用表达式struct VNode而不访问数组中的第L[i]->data个节点,而是访问数组的第i个数组(i> 0不存在) (以及第i个数组的第一个节点中的->data个元素data)。要解决该错误,您可以将上述函数中的参数声明更改为i,并将这些函数中的AdjList L的每个实例更改为L[i]->并调用

L[i].

    MatrixToList(G, *L);