从图中创建邻接列表的两种实现

时间:2013-06-04 12:24:12

标签: c

对于下面的图形声明(我无法更改 - 赋值;还有宏,因为我不允许使用'。'和' - >'图形运算符)

#include <string.h>
#include <stdlib.h>
#define TAG(vp)   ((vp)->tag)
#define LABEL(vp) ((vp)->label)  
#define EDGE(vp)  ((vp)->edge)

typedef struct vertex 
{
    char tag;
    char *label;
    struct vertex *edge[1];
}
vertex, *vp;

和邻接列表的这个声明(对于这个和所有下一个代码,我可以做任何我想做的事)

typedef struct adjList
{
    vp node;
    struct adjList *next;
}
adjList;

我编写了两个版本的函数,它必须从图形创建邻接列表表示。他们都使用此功能创建一个列表。

adjList *createList (vp graph)
{
    int i;
    adjList *result, *ptr, *ptr2;
    if (graph)
    {
        result = malloc (sizeof (adjList));
        result->node = graph;
        ptr2 = result;
        for (i = 0; EDGE (graph)[i]; ++i)
        {
            ptr = malloc (sizeof (adjList));
            ptr->node = EDGE (graph)[i];
            ptr2->next = ptr;
            ptr2 = ptr;
        }
        ptr2->next = NULL;
    }
    return result;
}

第一个返回一个邻接列表数组(作为一个双指针),但这仅针对一个节点及其子节点(我还没有弄清楚如何使用这个工作函数返回完整的表示)

adjList **createLists (vp graph)
{
    int i;
    adjList **result;
    result = malloc (sizeof (adjList *));
    result[0] = createList (graph);
    for (i = 0; EDGE (graph)[i]; ++i)
    {
        result = realloc (result, sizeof (adjList *) * (i + 2));
        result[i + 1] = createList (EDGE (graph)[i]);
    }
    result = realloc (result, sizeof (adjList *) * (i + 2));
    result[i + 1] = NULL;
    return result;
}

第二个产生分段错误。基本上,它创建了一个空列表数组,但使用这个逻辑我会创建完整的表示。

void createListsHelper (adjList **result, vp graph, int *index) /* index stores an index in an array to create there next list */
{
    int i;
    if (graph)
    {
        result = realloc (result, sizeof (adjList *) * (*index + 2));
        result[*index] = createList (graph);
        for (i = 0; EDGE (graph)[i]; ++i)
        {
            ++(*index);
            createListsHelper (result, EDGE (graph)[i], index);
        }
    }
}

adjList **createLists (vp graph)
{
    adjList **result = malloc (sizeof (adjList *));
    int *index = malloc (sizeof (int));
    *index = 0;
    createListsHelper (result, graph, index);
    result[*index + 1] = NULL;
    return result;
}

如何更改其中一个以便它们可以正常工作。

提前致谢。

注意:我使用以下“main”来测试它们。

int main()
{
    int i;
    adjList **list, *ptr;
    vp test;
    test = malloc (sizeof (vertex) + 4 * sizeof (vp));
    LABEL (test) = malloc (sizeof (char) * 2);
    LABEL (test)[0] = 'a';
    LABEL (test)[1] = '\0';
    for (i = 0; i < 3; ++i)
    {
        EDGE (test)[i] = malloc (sizeof (vertex));
    }
    LABEL (EDGE (test)[0]) = malloc (sizeof (char) * 2);
    LABEL (EDGE (test)[0])[0] = 'b';
    LABEL (EDGE (test)[0])[1] = '\0';
    LABEL (EDGE (test)[1]) = malloc (sizeof (char) * 2);
    LABEL (EDGE (test)[1])[0] = 'c';
    LABEL (EDGE (test)[1])[1] = '\0';
    LABEL (EDGE (test)[2]) = malloc (sizeof (char) * 2);
    LABEL (EDGE (test)[2])[0] = 'd';
    LABEL (EDGE (test)[2])[1] = '\0';
    EDGE (test)[3] = NULL;
    EDGE (EDGE (test)[0])[0] = NULL;
    EDGE (EDGE (test)[1])[0] = NULL;
    EDGE (EDGE (test)[2])[0] = NULL;
    list = createLists2 (test);
    for (i = 0; list[i]; ++i)
    {
        for (ptr = list[i]; ptr; ptr = ptr->next)
        {
            printf ("%c ", LABEL (ptr->node)[0]);
        }
        printf ("\n");
    }
    return 0;
}

1 个答案:

答案 0 :(得分:2)

void createListsHelper (adjList **result, vp graph, int *index) /* index stores an index in an array to create there next list */
{
    int i;
    if (graph)
    {
        result = realloc (result, sizeof (adjList *) * (*index + 2));

由于result按值传递(每个参数都由C中的值传递),因此此修改不会影响调用函数中的result。这一行

createListsHelper (result, graph, index);
result[*index + 1] = NULL; /* <<< */

调用UB(因为result仍然指向1 int的数组,并且您正在访问第二个元素。)