Question

I am using an Adjacency list to construct a directed graph. I have the vertexs working properly but when it comes to making a vertex have more than one edge I am having trouble. I am also unsure of how to correctly travese through multiple edges for the purpose of printing the adjacency list. What I have at the moment is causing a segmentation fault but I would like to figure out how to correctly build the list first. I will include the main, header file, test data, and the output I am receiving.I can post the other two functions if requested. The section of code that I feel the problem is occurring in is right after I call adjListLocate twice and I am dealing with edges. I am trying to make a new edge and have it point to the old one and then have the vertex point to the new edges but that isn't happening.

main.c

#include "my.h"

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



VERTEX *adjList;
adjList  = (VERTEX*)calloc(26, sizeof(VERTEX));
//adjList = malloc(sizeof(VERTEX)*26);
FILE* p;
char a;
char b;
int check1 = 0;
int check2 = 0;
int size = 0;
int i;
int aloc = 0;
int bloc = 0;
//Statements





if (argc != 2)
{
    fprintf(stderr, "Usage: %s file\n", argv[0]);
    return 1;
}

if ((p = fopen(argv[1], "r")) == 0)
{
    fprintf(stderr, "Failed to open file %s for reading \n", argv[1]);
    return 1;
}




while(fscanf(p," %c %c", &a, &b) == 2)  
{
printf("a: %c b: %c\n",a,b);

    check1 = adjListSearch(a,size,adjList);

    if(check1==1)
    {
        printf("Adding a = %c\n", a);
        adjList[size++].c = a;  
    }   
    check2 = adjListSearch(b,size,adjList);
    if(check2==1)
    {
        printf("Adding b = %c\n", b);
        adjList[size++].c = b;
    }

    aloc = adjListLocate(a,size,adjList);
    bloc = adjListLocate(b,size,adjList);

EDGE* e = (EDGE*)malloc(sizeof(EDGE));
e->v = &adjList[bloc];
if(*&adjList[aloc].p)
    {
    EDGE* f = (EDGE*)malloc(sizeof(EDGE));
    f->v = &adjList[bloc];
    f->q = (*&adjList[aloc]).p;
    (*&adjList[aloc]).p = f;

    printf("Edge Test: %c %c %c\n", f->v->c, f->q->v->c, f->q->v->c);
    }
else
{
(*&adjList[aloc]).p = e;
e->q = NULL;
}



}
//End While

printf("Size: %d\n", size);

for(i=0;i<size;i++)
{
printf(" %c", adjList[i].c);
    if(adjList[i].p)
    {
    VERTEX* z = (VERTEX*)malloc(sizeof(VERTEX));
    EDGE* temp = (EDGE*)malloc(sizeof(EDGE));
    z = &adjList[i];
    //printf("Edge: %c\n", z->p->v->c);
    temp = z->p;
        while(temp->v->c)
        {
        printf("test\n");   
        printf("Edge: %c\n", temp->v->c);
        temp = temp->q;
            printf("test2\n");

        }
    }
printf("\n");
}



fclose(p);


return 0;
}
//End main

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

int adjListLocate (char a, int size, VERTEX* adjList);

data

A B
C C
E X
C D
C F
C X

sample output

a: A b: B
empty
Adding a = A
not found B
Adding b = B
a: B b: C
found B
not found C
Adding b = C
a: E b: X
not found E
Adding a = E
not found X
Adding b = X
a: C b: D
found C
not found D
Adding b = D
a: C b: F
found C
not found F
Adding b = F
Edge Test: F D D
a: C b: X
found C
found X
Edge Test: X F F
Size: 7
 Atest
Edge: B
test2
Segmentation fault

The edge test is what I am worried about here. I would want it to say Edge Test: X F D

Was it helpful?

Solution

if(*&adjList[aloc].p)
{
      EDGE* f = (EDGE*)malloc(sizeof(EDGE));
      f->v = &adjList[bloc];
      f->q = (*&adjList[aloc]).p;
      (*&adjList[aloc]).p = f;

      printf("Edge Test: %c %c %c\n", f->v->c, f->q->v->c, f->q->v->c);
}

The above piece of code looks to me problematic. as *& is redundant, and the condition is simply

 if(adjList[aloc].p)

then it will always give true as you don't initialize adjList[aloc].p with NULL, at least i don't see where you do it. And ofcourse the

 printf("Edge Test: %c %c %c\n", f->v->c, f->q->v->c, f->q->v->c);

simply prints f->q->v->c twice.

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