c - C 函数中的动态结构数组

标签 c arrays adjacency-list

我正在尝试创建邻接列表来表示文件“input.txt”中的边列表中的图形。我知道指针如何工作的基础知识,但我对由单链表组成的动态结构数组有问题。

#include <stdio.h>
#include <stdlib.h>

struct list {
    int vertex;
    struct list *next;
};

void create_list(int v, struct list* **array);

int main()
{
    int i, v = 5;
    struct list *ptr, **array = (struct list **)malloc(sizeof(struct list *) * v);
    for (i = 0; i < v; i++)
        array[i] = NULL;
    array[i] = NULL;

    create_list(v, &array);

    for(i = 0; i < v; i++) {
        ptr = array[i];
        printf("%d: ", i);
        while(ptr != NULL) {
            printf(" ->%d", ptr->vertex);
            ptr = ptr->next;
        }
        printf("\n");
    }
    return 0;
}

void create_list(int v, struct list* **array)
{
    int m, n;
    struct list *ptr, *tmp;
    FILE *luki;
    luki = fopen("input.txt", "r");
    while(fscanf(luki, "%d %d\n", &m, &n) == 2) {
        tmp = (struct lista*)malloc(sizeof(struct list));
        tmp->vertex = n;
        tmp->next = NULL;
        if (*array[m] == NULL)      //Here my program crashes when m changes from 0 to 1
            *array[m] = tmp;
        else {
            ptr = array[m];
            while(ptr->next != NULL)
                ptr = ptr->next;
            ptr->next = tmp;
        }
    }
    fclose(luki);
}

你能帮我弄清楚它应该是什么样子吗?

此外,起初我在不使用指向数组的指针的情况下创建了该函数:

void create_list(int v, struct list **array)

create_list(v, array);

调试时效果非常好(我正在使用 CodeBlocks):

0: 4 ->3 ->1
1: 2
2: 3
3:
4:

但是在正常运行程序时我得到了这个:

0:
1:
2:
3:
4:

如果将数组传递给函数create_list是错误的,为什么调试时的输出是正确的?

最佳答案

(1) void create_list(int v, struct list **array); ... create_list(v, 数组);

#include <stdio.h>
#include <stdlib.h>

struct list {
    int vertex;
    struct list *next;
};

void create_list(int v, struct list **array);

int main(void) {
    int i, v = 5;
    struct list *ptr, **array = (struct list **)malloc(sizeof(struct list *) * v);
    for (i = 0; i < v; i++)
        array[i] = NULL;

    create_list(v, array);

    for(i = 0; i < v; i++) {
        ptr = array[i];
        printf("%d: ", i);
        while(ptr != NULL) {
            printf("%d", ptr->vertex);
            ptr = ptr->next;
            if(ptr)
                printf(" -> ");
        }
        printf("\n");
    }
    return 0;
}

void create_list(int v, struct list **array){
    int m, n;
    struct list *ptr, *tmp;
    FILE *luki;
    luki = fopen("input.txt", "r");
    while(fscanf(luki, "%d %d\n", &m, &n) == 2) {
        tmp = (struct list*)malloc(sizeof(struct list));
        tmp->vertex = n;
        tmp->next = NULL;
        if (array[m] == NULL)
            array[m] = tmp;
        else {
            ptr = array[m];
            while(ptr->next != NULL)
                ptr = ptr->next;
            ptr->next = tmp;
        }
    }
    fclose(luki);
}
<小时/>

(2) void create_list(int v, struct list ***array); ... create_list(v, &array);

void create_list(int v, struct list ***array){
    int m, n;
    struct list *ptr, *tmp;
    FILE *luki;
    luki = fopen("input.txt", "r");
    while(fscanf(luki, "%d %d\n", &m, &n) == 2) {
        tmp = (struct list*)malloc(sizeof(struct list));
        tmp->vertex = n;
        tmp->next = NULL;
        if ((*array)[m] == NULL)
            (*array)[m] = tmp;
        else {
            ptr = (*array)[m];
            while(ptr->next != NULL)
                ptr = ptr->next;
            ptr->next = tmp;
        }
    }
    fclose(luki);
}

关于c - C 函数中的动态结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37194992/

相关文章:

c - 如何从串口读取数据并将其写入文件

java - 如何更改数组中的最后一次迭代?

ruby-on-rails - 在 Rails 关系上使用 Ruby 的 select 方法并更新它

php - 递归地从多维数组中删除特定键

c - 字符串复制函数

CUDA:车道之间的 __shfl delta 可以不同吗?

c - 函数宏问题

algorithm - 将网格转换为加权邻接列表

php - 如何使用 PHP 和 MySQL 将父子(邻接)表转换为嵌套集?

c# - 从对列表创建邻接列表类型结构