Question

I am having to make a directed graph that does a breath first search, depth first search, and topological sort. My professor wants us to use an adjacency list to construct the graph and wants us to use C even though we have been using Java the whole year so I am a bit rusty when it comes to C. I am getting a segmentation fault when compiling what I have so far and I can't figure out why. I have tried putting print statements to find where the code is failing but nothing is being printed. Could you help locate where the problem is? I will include my code and the sample data I am working with.

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[]);

myfile

A  B
B  C
E  X
C  D
Was it helpful?

Solution

You probably have to allocate VERTEX *adjList[26]; before using

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top