2014-04-17 206 views
0

我正在使用邻接列表构造有向图。我有顶点工作正常,但是当涉及到使一个顶点有多个边缘时,我遇到了麻烦。我也不确定如何正确地通过多条边以达到打印邻接表的目的。我现在所拥有的是导致分段错误,但我想弄清楚如何正确地建立列表。我将包括主,头文件,测试数据和我收到的输出。如果需要,我可以发布其他两个函数。在我调用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

+0

here if(*&adjList [aloc] .p)'你期望'*&'会给你什么?后来那些'*&'是什么? – Dabo

+0

我认为我在写这些时试图用指针做点什么,我对C有点生疏。我只是删除了它们,它没有改变任何东西。他们最终取消对方,不是吗? – Fluxxxy

+1

它们互相取消,但会使您的代码非常不清晰,避免不必要的代码。另外关于“我想要它说边缘测试:XFD”,你打印两次:ff-> q-> v-> c':printf(“边缘测试:%c%c%c \ n”,f- > v-> c,f-> q-> v-> c,f-> q-> v-> c);'所以它看起来像它给你你所要求的 – Dabo

回答

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因为你不NULL初始化adjList[aloc].p,至少我没有看到你这样做。并且当然

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

简单地打印f->q->v->c两次。

+0

因此,当我最初将一个顶点放入adjList中时,我应该将其指针设置为NULL?恩。 adjList [size] .p = NULL; – Fluxxxy

+0

其实我现在看到你在创建adjList时使用calloc,所以很可能你的指针已经是NULL,但也许不是,因为不能保证'NULL'表示为'全位零值'。使用malloc,然后迭代所有元素并明确地用'NULL'设置'p' – Dabo