链接的边缘列表

时间:2014-04-17 17:54:53

标签: c graph linked-list adjacency-list

我正在使用Adjacency列表来构建有向图。我有正确的顶点工作,但当涉及到使顶点有多个边缘时,我遇到了麻烦。我也不确定如何正确地遍历多个边缘以便打印邻接列表。我目前所拥有的是导致分段错误,但我想弄清楚如何正确地构建列表。我将包括main,头文件,测试数据和我收到的输出。如果需要,我可以发布其他两个函数。我觉得问题发生的代码部分就在我调用adjListLocate两次并且处理边缘之后。我试图创建一个新的边缘,让它指向旧的边缘,然后让顶点指向新的边缘,但这不会发生。

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

数据

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

示例输出

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

边缘测试是我在这里担心的。我希望它说边缘测试:X F D

1 个答案:

答案 0 :(得分:1)

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

上面的代码看起来有问题。因为*&是多余的,条件就是

 if(adjList[aloc].p)

然后它会始终提供true,因为您不会使用adjList[aloc].p初始化NULL,至少我不知道您在哪里做。而且当然是

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

只需打印f->q->v->c两次。