有没有办法为不同的结构编写单个函数( addnode )?我有这种情况:
typedef struct linkedlist_a *ptr_a;
typedef struct linkedlist_a
{
/* content */
ptr_a next;
} listA;
typedef struct linkedlist_b *ptr_b;
typedef struct linkedlist_b
{
/* content */
ptr_b next;
} listB;
listA *listA_addnode( listA *head, listA *node )
{
listA *temp = head;
if( temp == NULL )
{
temp = node;
}
else if( temp -> next == NULL )
{
temp -> next = node;
}
else
{
while( temp -> next ) temp = temp -> next;
temp -> next = node;
}
return head;
}
listB *listB_addnode( listB *head, listB *node )
{
listB *temp = head;
if( temp == NULL )
{
temp = node;
}
else if( temp -> next == NULL )
{
temp -> next = node;
}
else
{
while( temp -> next ) temp = temp -> next;
temp -> next = node;
}
return head;
}
如果有两个结构对我来说可以写两个函数,但如果我有超过 2 个,我该怎么办?