-1

基本上,数组应该在用户输入时初始化。如果input = 3,则表示该数组可以分别在索引中存储一个链表0,1,2(总共 3 个链表)

int input = 3;
list* array[n]//not allowed as n is not constant, also not in heap so can't pass it across functions
list* array[] = (list*) malloc(sizeof(list)*input)//compiler error

准备面试......所以你可以说家庭作业!

4

3 回答 3

1

链表数组可以是头节点数组(假设是标准链表实现)或指向列表的指针数组。无论哪种情况,您似乎面临的问题是如何动态分配数组。

在堆上动态分配数组的通用语法是

type * array = calloc(number_of_elements, sizeof(type))

因此,纠正代码中的编译错误将是

int input = 3;
list ** array = calloc(input, sizeof(list*));

这是你想要的?

于 2012-04-19T10:35:01.730 回答
0

它应该是list* array = malloc(sizeof(list) * input)malloc返回新分配的内存位置的基地址。您可以将其用作数组,即您可以访问array[0]..array[input - 1].

于 2012-04-18T06:58:35.923 回答
0

(单)链表通常是带有指向下一个结构的指针的结构。这种模式使得从该列表中添加、删除和插入内容变得容易,并且仍然保持整个数据使用的灵活性并在运行时进行管理。

在您的情况下,链表将如下所示

struct List
{
 // contents of the list
 List* Pointer_to_next_list;
};

然后,您可以添加用于跟踪每个列表的功能。我建议阅读Wikipedia::Linked List以了解其工作原理。

于 2012-04-18T10:05:47.130 回答