Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何连接结构节点(链表)?_C_Struct_Linked List_Concatenation_Singly Linked List - Fatal编程技术网

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

C 如何连接结构节点(链表)?,c,struct,linked-list,concatenation,singly-linked-list,C,Struct,Linked List,Concatenation,Singly Linked List,我想创建一个新节点,它是连接到节点2的节点1的副本 ,问题是我需要在每个 接受我在连接函数中插入的特定条件 . 例如,如果我有两个要相互连接的节点 (第一个结尾处的第二个),但我想选择 每个节点中的元素,例如奇数!(例如:第一 链表包含以下元素(1、2、3)和第二个链接 列表有以下元素(4 5 6),然后我想有一个新的链表>,它有以下元素:(1 3 5) 现在我的主要问题是我需要使用指向 函数,因为每次我都想赋予不同的函数 条件 我编写这个函数时假设我有一个 ConditionFunction,

我想创建一个新节点,它是连接到节点2的节点1的副本 ,问题是我需要在每个 接受我在连接函数中插入的特定条件 . 例如,如果我有两个要相互连接的节点 (第一个结尾处的第二个),但我想选择 每个节点中的元素,例如奇数!(例如:第一 链表包含以下元素(1、2、3)和第二个链接 列表有以下元素(4 5 6),然后我想有一个新的链表>,它有以下元素:(1 3 5) 现在我的主要问题是我需要使用指向 函数,因为每次我都想赋予不同的函数 条件

我编写这个函数时假设我有一个 ConditionFunction,但实际上我有点执着于如何制作 主函数中的ConditionFunction,它实际上可以执行 想要:/(例如,仅链接奇数)

我编写此函数是为了连接两个链表:

我写了一个例子,但我认为它是错误的:\

intmain(){
int数组_1[3]={1,2,3};
int数组_2[4]={4,5,6,7};
节点头_1=createAllNode(数组_1,3);
节点头_2=createAllNode(数组_2,4);
int num=2;
节点oddhead=concatlist(head_1、head_2、©Int、&checkIfOdd、&num);
印刷元素(奇头);
返回0;
}
静态节点createAllNode(int*数组,int len){
节点头;
节点\结果=创建\节点(&head);
如果(结果!=节点成功){
返回NULL;
}
Node new_Node=NULL;
int j=0;
while(len){
/*int*num=malloc(sizeof(*num));
如果(num==NULL){
返回NULL;
} */
int元素=数组[j];
head->element=*(int*)元素;
如果(j!=len-1){
结果=创建\u节点(&新建\u节点);
}
if(Node\u Result!=Node\u SUCCESS){
返回NULL;
}
head->next=新建_节点;
head=新的_节点;
new_node=GetNextNode(new_node);
j++;
蓝--;
}
回流头;
}
静态void*copyInt(void*num){
int*newInt=malloc(sizeof(*newInt));
*newInt=*(int*)num;
返回newInt;
}
/*
静态布尔主节点(void*num1,void*num2){
}
*/
静态void打印元素(节点头){
while(head!=NULL){
printf(“%d”,(int*)头->元素);
head=GetNextNode(head);
}
}
静态布尔校验奇数(元素num1,整数num2){
int num=*(int*)num1;

if(num我将不使用您的定义,因为它们容易混淆。我将演示如何通过选择满足某些条件的节点来连接两个列表。您可以使用演示程序作为自己列表实现的基础

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

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

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

首先,永远不要使用typedef指针。其次,不要在类型中使用
\t
后缀。它与POSIX冲突。如果一次只需要一个筛选器,可以将
struct node\t*filter
添加到
struct node\t
中,而不是复制数据,只需创建一个新的筛选列表。@kayan我不明白如何检查t奇怪的是,@kayan和什么是ConditionFunction、copyFunction和condition?似乎您只需要一个参数函数来检查条件。我可以在这里插入我在maukn函数中写的内容,这样您就可以更好地理解我了。首先,所有从不使用typedef指针。其次,不要在您的类型中使用
\t
后缀。它在conf中lict与POSIX。如果您一次只需要一个筛选器,您可以将
struct node\u t*filter
添加到
struct node\u t
中,而不是复制数据,只需创建一个新的筛选列表即可。@kayan我不明白如何检查异常情况。@kayan以及什么是条件函数、复制函数和条件?似乎您只需要打开检查条件的e参数函数。我要在这里插入我在maukn函数中写的内容,这样你就能更好地理解我谢谢你!这就是我要找的..一个我可以做类似事情的例子!你帮了我很多:)我想问一下,我是否希望结构中的数据为void*,我想在这个concatLists函数中使用一个copy函数,它将在第一个和第二个节点中复制void*元素。我该如何做呢?也许:\@kayan事实上,你需要一个具有void*类型参数的函数来创建一个节点。你可以问一个根据这项任务提出一个问题。非常感谢!这就是我要找的..一个我如何能做这样事情的例子!你帮了我很多:)我想问一下,我是否希望结构中的数据为void*,我想在这个concatLists函数中使用一个copy函数,它将在第一个和第二个节点中复制void*元素。我该如何做呢?也许:\@kayan事实上,你需要一个具有void*类型参数的函数来创建一个节点。你可以问一个根据这项任务提出一个问题。
#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