C 2D链表

时间:2010-10-04 04:29:05

标签: c linked-list

所以我有

struct node {
   int number;
   struct node *next;

 struct deck{
    int number;
    struct deck *next;
 };
};

我想创建一个2D链表。我怎样才能初始化这样的东西?

感谢。

3 个答案:

答案 0 :(得分:3)

这样的事可能吗?

struct deck{
    int number;
    struct deck *next;
}
struct node {
   int number;
   struct node *next;
   struct deck *decks;
};

struct node *current_node, *new_node;
struct deck *current_deck, *new_deck;

current_node = (struct node *) malloc (sizeof(struct node));

for (i=0; i<number_of_nodes-1;i++) {
    current_deck = (struct deck* ) malloc (sizeof(struct deck));
    current_node->decks = current_deck;
    for (j=0; j<number_of_decks_in_node_i-1; j++) {
        new_deck = (struct deck* ) malloc (sizeof(struct deck));
        current_deck->next = new_deck;
        current_deck = new_deck;
    }
    new_node = (struct node *) malloc (sizeof(struct node));
    current_node->next = new_node;
    current_node = new_node;
}

答案 1 :(得分:2)

首先,您需要在节点之外的 deck 。你拥有的是有效的C ++而不是C。

你可以这样写:

struct node {
   int number;
   struct node *next;
};


struct deck{
   struct node* nodes;
   struct deck* next;
};

答案 2 :(得分:1)

使用笛卡尔系统,你可以。 (顺便说一下,它还没有为我工作。试着)

struct node(){
    char data[10];
    node *x_link;
    node *y_link;
}