c - 用C语言建立邻接表

标签 c graph segmentation-fault adjacency-list

我必须制作一个有向图,它可以进行呼吸优先搜索、深度优先搜索和拓扑排序。我的教授希望我们使用邻接表来构建图,并希望我们使用 C,尽管我们一整年都在使用 Java,所以我对 C 有点生疏。在编译什么时出现段错误到目前为止我还无法弄清楚为什么。我尝试放置打印语句来查找代码失败的位置,但没有打印任何内容。能帮忙定位一下问题出在哪里吗?我将包含我的代码和我正在使用的示例数据。

main.c

   //Main Function

    #include "my.h"

    int main (int argc, char* argv[])
    {

    int y = 0;
    int x = 0;
    VERTEX *adjList[26];
    FILE* p;
    char *fp;
    char a;
    char b;
    int check1 = 0;
    int check2 = 0;
    int size = 0;

    int searchsuccess = 0;
    //Statements

    printf("Awdada");

    p = fopen( argv[1], "r");

    printf("aweada");

    while(fscanf(p, "%c %c",&a,&b)!= EOF)  
    {
    check1 = adjListSearch(a,size,adjList);
        if(check1==1)
    {
        (*adjList[size]).c = a;
        size = size +1;
    }   

    check2 = adjListSearch(b,size,adjList);
    if(check2==1)
    {
        (*adjList[size]).c = b;
        size = size +1;
    }
    }
    //End While




    return 0;
    }
    //End main

adjListSearch.c

#include "my.h"

int adjListSearch (char a, int size, VERTEX* adjList[])
{

int i;

if(size == 0)
{
return 1;
}

for(i = 0; i<size; i++)
{
    if((* adjList[i]).c = a)
    {
    return 0;
    }
}
return 1;

}

my.h

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

/* Forward declaration */
struct EDGETAG;

typedef struct
{
    char c;
    bool isVisited;
    struct EDGETAG* p;
} VERTEX;


typedef struct EDGETAG
{
    VERTEX* v;
    struct EDGETAG* q;
} EDGE;


int main (int argc, char* argv[]);

int adjListSearch (char a, int size, VERTEX* adjList[]);

我的文件

A  B
B  C
E  X
C  D

最佳答案

您可能必须分配 VERTEX *adjList[26];使用前

关于c - 用C语言建立邻接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23121261/

相关文章:

c++ - 为什么 const char *foo = "Hello";编译但不编译 const int *foo = 5;?

c - opengl渲染到纹理只看到一个黑色区域

调用msync有必要吗?

条件跳转或移动取决于 C 中未初始化的值

c - C 中的基本数组用法?

c - 尝试访问二进制文件时出现段错误

python - 通过 Python 中的 c 函数传递和返回 double 组

graph - 最短路径更快 - SPFA 算法?

c++ - 如何找到图形增广路径

algorithm - 寻找最小的顶点子集 - 回溯?