开发者

C 2D linked list

So i have

struct node {
  开发者_StackOverflow int number;
   struct node *next;

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

I want to create a 2D linked list. How can i initialize something like this?

Thanks.


Something like this maybe ?

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;
}


First of all, you need to take deck outside node. What you have is valid C++ but not C.

You could write something like this:

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


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


using cartesian system, you can. (BTW, it's not yet working for me. Trying)

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜