声明并初始化链表头节点的动态数组

Declare and Initialize dynamic array of head nodes of linked lists

我想知道如何声明、分配和初始化 Node 数组为 null。

typedef struct Nodes_Of_List { 
        int data; 
        struct Nodes_Of_List *next;
} Node;

//declare array of head nodes
Node *table;

//ALOCATE memory for "x" x is provided at runtime, number of head nodes
table = malloc(sizeof(Node) * x); 

//Initialize elements to Null value
?? //How to do it 

请求链表头节点动态数组初始化为Null的说明。目的是做链表数组,做哈希表。

据我了解,您想声明一个头节点数组。之后,你想将它们初始化为 NULL:

//declare array of head nodes statically
Node * table[x];      // x value provided at runtime
// Or dynamically
Node ** table = (Node **) malloc(sizeof(Node *) * x);
// Or use calloc directly wich will initialize your pointers to 0
Node ** table = (Node **) calloc(x, sizeof(Node *));

// table is an array of pointers. Each pointer references a linked list.
// Now you have just to put NULL value in each element of table
int i;
for(i = 0; i < x; i++) 
    table[i] = 0;