C++ 如何将链接列表传递给函数

C++ 如何将链接列表传递给函数,c++,C++,我想创建两个链表并按排序将它们合并。我试图将它们合并到一个名为“merge(link1,link2)”的函数中,但我不知道如何将链表引用给该函数。 #include<iostream> #include<malloc.h> using namespace std; struct node { int info; node *next; }; node *start1=NULL; no

我想创建两个链表并按排序将它们合并。我试图将它们合并到一个名为“merge(link1,link2)”的函数中,但我不知道如何将链表引用给该函数。

 #include<iostream>
    #include<malloc.h>
    using namespace std;
    struct node
    {
        int info;
        node *next;
    };
    node *start1=NULL;
    node *start2=NULL;
    void link1(int x)
    {
        node *temp=(node *)malloc(sizeof(node));
        node *t;
        temp->info=x;
        temp->next=NULL;
        if(start1==NULL) 
        start1=temp;
        else
        {
            t=start1;
            while(t->next!=0)
            {
                t=t->next;

            }       
            t->next=temp;
        }

    }
    void link2(int x)
    {
        node *temp=(node *)malloc(sizeof(node));
        node *t;
        temp->info=x;
        temp->next=NULL;
        if(start2==NULL) 
        start2=temp;
        else
        {
            t=start2;
            while(t->next!=0)
            {
                t=t->next;

            }       
            t->next=temp;
        }
    }
    void merge(link1,link2)
    {
        //merge function
    }
    int main()
    {
        link1(1);
        link1(2);
        disp1();
        cout<<"\n";
        link2(3);
        link2(4);
        disp2();

        merge(link 1,link2); //this is problem how to give reference of inked lists here.
    }
#包括
#包括
使用名称空间std;
结构节点
{
国际信息;
节点*下一步;
};
node*start1=NULL;
node*start2=NULL;
无效链接1(int x)
{
node*temp=(node*)malloc(sizeof(node));
节点*t;
温度->信息=x;
temp->next=NULL;
if(start1==NULL)
start1=温度;
其他的
{
t=start1;
while(t->next!=0)
{
t=t->next;
}       
t->next=温度;
}
}
无效链接2(int x)
{
node*temp=(node*)malloc(sizeof(node));
节点*t;
温度->信息=x;
temp->next=NULL;
if(start2==NULL)
start2=温度;
其他的
{
t=start2;
while(t->next!=0)
{
t=t->next;
}       
t->next=温度;
}
}
无效合并(链接1、链接2)
{
//合并函数
}
int main()
{
链接1(1);
链接1(2);
disp1();

cout3->4.

开始编码之前,您至少需要学习编程基础知识。例如,代码(函数)和数据之间的差异,以及数据和它们的类型之间的差异

 #include<iostream>
    #include<malloc.h>
    using namespace std;
    struct node
    {
        int info;
        node *next;
    };
    node *start1=NULL;
    node *start2=NULL;
    void link1(int x)
    {
        node *temp=(node *)malloc(sizeof(node));
        node *t;
        temp->info=x;
        temp->next=NULL;
        if(start1==NULL) 
        start1=temp;
        else
        {
            t=start1;
            while(t->next!=0)
            {
                t=t->next;

            }       
            t->next=temp;
        }

    }
    void link2(int x)
    {
        node *temp=(node *)malloc(sizeof(node));
        node *t;
        temp->info=x;
        temp->next=NULL;
        if(start2==NULL) 
        start2=temp;
        else
        {
            t=start2;
            while(t->next!=0)
            {
                t=t->next;

            }       
            t->next=temp;
        }
    }
    void merge(link1,link2)
    {
        //merge function
    }
    int main()
    {
        link1(1);
        link1(2);
        disp1();
        cout<<"\n";
        link2(3);
        link2(4);
        disp2();

        merge(link 1,link2); //this is problem how to give reference of inked lists here.
    }
在您的示例中,
node*
是一种数据类型。
link1
link2
是函数,即创建数据的代码。
start1
start2
是数据。您需要编写另一个函数来描述其输入和输出数据类型。然后传递实际数据,如下所示:

    node *merge(node *link1, node *link2)
    {
        ...
    }
    int main()
    {
        ...
        start3 = merge(start1, start2);
    }
还有一些评论:

不要在C++中使用<代码> Mulc C/代码>,使用<代码>新< /COD> .
  • 不要使用
    new
    ,除非您必须使用标准容器,如果标准容器不够,请使用标准容器或增压容器

  • 不要使用
    名称空间std;

  • 因为这是C++,你不能只使用<代码> STD::合并< /COD>?如果你不能,那么你最好做的就是查看合并的接口并理解它(编写一些简单的代码来确保你做)。,然后重新实现它的基础。迭代器是您应该努力争取的接口,而不是裸指针。在引擎盖下,迭代器通常可以只是一个指针,但合并算法不应该关心这一点。要创建输出列表,您可以使用插入迭代器,将合并列表的事实抽象出来。Abstracti通过迭代器删除列表可能是一个很好的开始。作为起点,
    start1
    start2
    声明属于
    merge
    的参数列表。就这么简单!
    Node*merge(Node*left,Node*right)
    。既然语言已经有了
    std::list
    std::forward\u list
    ,为什么还要重新发明轮子并构建自己的链表呢?另一方面,你甚至可能不想要链表,因为它是一种性能糟糕得可怕的数据结构(尤其是在现代CPU上)。您可能实际上想要一个
    std::vector
    节点*temp=(节点*)malloc(sizeof(节点));
    -不。永远不要(除非您真的知道自己在做什么)请在C++程序中使用<代码> MalOC ,并且请停止使用C样式的Casts。请停止使用<代码> NUL>代码>,使用<代码> NulLPTR < /Code >。如果这是代码审查,我只会用表空间STD来抓取表面.. <代码>;通常,也不好。你的评论1. 2。3。只是鼓励另一个货物崇拜。e在你的答案中更加具体,以及它们与OP.@πάντα的关系ῥεῖ, 您可以对C++的任何一本书发表相同的评论,它们通常包含相同的建议。