0

だから私が欲しいのは、ノードを与えられた関数を実行して、それを最後の場所に置くことです。問題は、すべてのリスト構造で機能する関数を実行したいということです。どうすればいいですか?これまでのところ、ノードを最後に配置する関数を作成することができましたが、リストのタイプは 1 つだけでした。コードは次のとおりです。

void putList(PESPECIALIDADE *p){
    int i = 1;
    p->proximo = NULL;
     ptr;
    if(cabecaEspecialidade == NULL){
        p->id=1;
        cabecaEspecialidade = p;
    }
    else{
        for(ptr=cabecaEspecialidade; ptr->proximo != NULL; ptr = ptr->proximo){
            i++;
        }
        p->id=i+1;
        ptr->proximo = p;
        p->proximo=NULL;            
    }
}
4

1 に答える 1

0

If you want to have many lists of different contents, a common way is to have a common node structure as base for all lists.

struct base_node
{
    struct base_node *next;
};

Then you have that structure first in all other structures used for your lists:

struct some_node_data
{
    struct base_node node;  /* Note: not a pointer */

    /* All other data... */
};

(Note: Having the base node structure as a member in the new structure, it's kind of like C++ inheritance.)

Now you can cast a pointer to the above structure as a pointer to the base_node structure, and use that.

void add_tail(struct base_node **head, struct base_node *node)
{
    /* Add `node` at the end of the list pointed to by `*head` */
}

/* ... */

struct some_node_data *some_data_head = NULL;

struct some_node_data *some_node = malloc(sizeof struct some_node_data);
add_tail((struct base_node *) &some_data_head, (struct base_node *) some_node);
于 2013-06-23T17:46:44.837 に答える