-4

I want to make two linked lists and want to merge them in sorted order.I tried to take them into a function named "merge(link1,link2)",but i don't know that how to give the reference of linked lists to the function.

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

Output: expected output after merge function is 1->2->3->4.

sam
  • 203
  • 1
  • 3
  • 15
  • 1
    Since this is C++, can’t you just use `std::merge`? And if you can’t, then about the best you can do is to look at merge’s interface and understand it (write some simple code to make sure you do), then re-implement the basics of it. Iterators are the interface you should strive for, not bare pointers. Under the hood, an iterator can often be just a pointer – but the merge algorithm shouldn’t care about that. To create the output list, you can use an insertion iterator that abstracts away the fact that you’re merging lists. Abstracting the lists away via iterators may be a good start. – Kuba hasn't forgotten Monica Apr 13 '19 at 14:02
  • As a starting point, the `start1` and `start2` declarations belong in the argument list of `merge`. As simple as that! `Node *merge(Node* left, Node* right)`. – Kuba hasn't forgotten Monica Apr 13 '19 at 14:04
  • Why are you reinventing the wheel and building your own linked list, when the language already has `std::list` and `std::forward_list`? On another note; you probably don't even *want* a linked list, since it's a horribly badly performing datastructure (especially on modern CPUs). You probably *actually* want a `std::vector`. – Jesper Juhl Apr 13 '19 at 14:08
  • `node *temp=(node *)malloc(sizeof(node));` - No. Don't ever (unless you *really* know what you are doing) use `malloc` in a C++ program. And also *stop* with the C-style casts, please. And stop using `NULL` please, use `nullptr`. And if this was a code review, I'd only have scratched the surface.. – Jesper Juhl Apr 13 '19 at 14:10
  • `using namespace std;` is also usually a bad idea. – Jesper Juhl Apr 13 '19 at 14:18

1 Answers1

0

Before you start coding you need to learn at least the programming basics. E.g. the difference between code (functions) and data. And between data and their types.

In your example node* is a data type. link1 and link2 are functions, i.e. code creating the data. And start1 and start2 are data. You need to write another function describing its input and output data types. And than pass actual data, like this:

    node *merge(node *link1, node *link2)
    {
        ...
    }
    int main()
    {
        ...
        start3 = merge(start1, start2);
    }

And some remarks:

  1. Don't use malloc in C++, use new.
  2. Don't use new either unless you absolutely have to, use standard containers or boost containers if the standard containers are not sufficient.
  3. Don't use using namespace std; Why is "using namespace std" considered bad practice?
aparpara
  • 2,171
  • 8
  • 23
  • 1
    Your remarks _1. 2. 3._ are just encouraging ***another*** _cargo cult_. Be more specific in your answers and how they relate to the OP. – πάντα ῥεῖ Apr 13 '19 at 14:26
  • @πάνταῥεῖ, you can address the same comment to any book on C++. They usually contain the same recommendations. – aparpara Apr 13 '19 at 14:32