如何连接结构节点(链表)?

how to connect a struct nodes(linked lists)?

i want to make a new node that is a copy of Node_1 connected to Node_2 , the problem is that i need to choose elemenets in each node that accept a specific condition thhat i insert in the connection function . for example if i have two nodes that i want to connect to each other (the second one at the end of the first one) , but i want to chose the elements in each node that are for example odd ! (for example : first linked list has the following elements(1 2 3 ) , and the second linked list has the following elements (4 5 6) then i want to have a new linked list >that has the following elements : (1 3 5) now my main problem is that i need to work with pointers to functions because each time i want to give the function different conditions .

i wrote this function with the assumption that i have a ConditionFunction, but actually i an kinda stuck on how to make a ConditionFunction in the main function that can actually do what i want :/ (for example linke only the odd numbers)

i wrote this function to connect the two linked lists :

// the struct: 
typedef struct node_t* Node;
struct node_t {
Element element;
Node next;
};

// ConditionNode a pointer to a function that has condition
// CopyNode a pointer to a function that copy's the node

Node concatLists(Node Node_1,Node Node_2,ConditionNode ConditionFunction,CopyNode copyFunction,void* condition){
    Node currentNode=NULL;
    Node* New_Node=NULL;
    Node head=NULL;
    while(Node_1!=NULL){
        if(ConditionFunction(Node_1->element,condition)==0){  
            Node_Result=create_node(&New_Node);
            if(head==NULL){
                head=New_Node;
                currentNode=New_Node;
            }
            currentNode->next=New_Node;
            currentNode=New_Node;
            Node_1=GetNextNode(Node_1);
        }
        else{
            Node_1=GetNextNode(Node_1);
        }
    }


    while(Node_2!=NULL){
        if(CmpFunction(Node_2->element,condition)!=0){
            if(head==NULL){
                head=New_Node;
                currentNode=New_Node;
            }
            currentNode->next=New_Node;
            currentNode=New_Node;
            Node_2=GetNextNode(Node_2);
        } else {
            Node_1=GetNextNode(Node_1);
        }
    }
    return head;
}


Node_Result create_node(Node* CreatedNode)  {
Node newNode = malloc(sizeof(*newNode));
if(!newNode) {
   return MEM_PROBLEM;
}
newNode->element =0;
newNode->next = NULL;
*CreatedNode=newNode;
return NODE_SUCCESS;
}



Node GetNextNode(Node node){
    if(node==NULL){
    return NULL;
    }
    return node->next;
}

i wrote an example but i think it is wrong :\

int main(){
    int array_1[3]={1,2,3};
    int array_2[4]={4,5,6,7};

    Node head_1=createAllNode(array_1,3);
    Node head_2=createAllNode(array_2,4);
    int num=2;
    Node oddhead=concatLists(head_1,head_2,&copyInt,&checkIfOdd,&num);
    printIntElements(oddhead);

    return 0;
}





static Node createAllNode(int* array,int len){
    Node head;
    Node_Result result=create_node(&head);
    if(result!=NODE_SUCCESS){
        return NULL;
    }
    Node new_node=NULL;
    int j=0;
    while(len){
        /*int *num=malloc(sizeof(*num));
        if(num==NULL){
            return NULL;
        } */
        int element=array[j];
        head->element=*(int *)element;

        if(j != len-1){
            result=create_node(&new_node);
        }
        if(Node_Result!=NODE_SUCCESS){
                    return NULL;
        }
        head->next=new_node;
        head=new_node;
        new_node=GetNextNode(new_node);
        j++;
        len--;
        }
    return head;
}


static void* copyInt(void* num){
    int* newInt=malloc(sizeof(*newInt));
    *newInt=*(int*)num;
    return newInt;
}

/*
static bool PrimaryIntNode(void*num1,void* num2){

}
*/

static void printIntElements(Node head){
    while(head!=NULL){
        printf("%d",(int*) head->element);
        head=GetNextNode(head);
    }
}


static bool checkIfOdd(Element num1,int num2){
    int num=*(int *)num1;
    if(num<0){
        num *=-1;
    }
    return num % num2 != 0;

}

and i call the coonect list function like this in the main function : Node oddhead=concatLists(head_1,head_2,&copyNode,&checkifIdd,&num);

can anyone just show me a correct example oh how actually use a function like this in main !! because i get all kinda of errors in eclipse ..

我不会使用你的定义,因为它们令人困惑。我将仅演示如何通过选择满足某些条件的节点来连接两个列表。您可以使用提供的演示程序作为您自己的列表实现的基础。

#include <stdio.h>
#include <stdlib.h>

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

void insert( struct node **head, const int a[], size_t n )
{
    if ( *head != NULL ) head = &( *head )->next;

    for ( size_t i = 0; i < n; i++ )
    {
        struct node *tmp = malloc( sizeof( struct node ) );
        tmp->data = a[i];
        tmp->next = *head;
        *head = tmp;
        head = &( *head )->next;
    }        
}    


struct node * concatLists( struct node *head1, struct node *head2, int cmp( struct node * ) )
{
    struct node *head = NULL;
    struct node **current = &head;

    for ( ; head1 != NULL; head1 = head1->next )
    {
        if ( cmp( head1 ) )
        {
            *current = malloc( sizeof( struct node ) );
            ( *current )->data = head1->data;
            ( *current )->next = NULL;
            current = &( *current )->next;
        }
    }

    for ( ; head2 != NULL; head2 = head2->next )
    {
        if ( cmp( head2 ) )
        {
            *current = malloc( sizeof( struct node ) );
            ( *current )->data = head2->data;
            ( *current )->next = NULL;
            current = &( *current )->next;
        }
    }

    return head;
}

void output( struct node *head )
{
    for ( ; head != NULL; head= head->next )
    {
        printf( "%d ", head->data );
    }
}

int odd( struct node *n )
{
    return n->data % 2;
}

int main( void ) 
{
    struct node *head1 = NULL;
    struct node *head2 = NULL;
    int a1[] = { 1, 2, 3 };
    int a2[] = { 4, 5, 6 };
    const size_t N1 = sizeof( a1 ) / sizeof( *a1 );
    const size_t N2 = sizeof( a2 ) / sizeof( *a2 );

    insert( &head1, a1, N1 );
    insert( &head2, a2, N2 );

    struct node *head3 = concatLists( head1, head2, odd );

    output( head1 );
    putchar( '\n' );
    output( head2 );
    putchar( '\n' );
    output( head3 );
    putchar( '\n' );

    return 0;
}

程序输出为

1 2 3 
4 5 6 
1 3 5