0

I tried putting dummy statements to find out the problem. It seems the problem occurs at the end of the merge() function at p->next=list2; but I do not know what the problem is.

#include <iostream>
using namespace std;

int value, count, value2, count2,choice;

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

node *list = nullptr;
node *list2 = nullptr;
node * p;
node * q;
node * r;

void insertFront()
{
    cout << "ENTER A VALUE=";
    cin >> value;

    if (list == nullptr)
    {
        p = new node();
        p->data = value;
        p->next = nullptr;
        list = p;
    }
    else
    {
        p = new node();
        p->data = value;
        p->next = list;
        list = p;
    }
}

void insertFront2()
{
    cout << "ENTER A VALUE=";
    cin >> value;

    if (list2 == nullptr)
    {
        r = new node();
        r->data = value;
        r->next = nullptr;
        list2 = r;
    }
    else
    {
        r = new node();
        r->data = value;
        r->next = list2;
        list2 = r;
    }
}

void delFront()
{
    if (list == nullptr)
    {
        cout << "LIST IS ALREADY EMPTY";
    }
    else
    {
        p = list;
        list = p->next;
        delete(p);
    }
}

void display(int choice)
{
    int select=choice;

    if (select == 1)
    {
        p = list;
    }
    if (select == 2)
    {
        p = list2;
    }
    while (p != nullptr)
    {
        cout << p->data << " ";
        p = p->next;
    }

    cout << endl;
}

void mergeLists(node *list,node *list2)
{
    if (list==nullptr and list2==nullptr){
        cout<<"Both lists are empty";
    }

    else if(list==nullptr && list2!=nullptr){
        display(2);
    }

    else if(list2==nullptr && list!=nullptr){
        display(1);
    }
    if(list!=nullptr && list2!=nullptr){
        p=list;
        while(p!=nullptr){
            p=p->next;
        }

        p->next=list2;

        display(1);
    }
}

int main()
{
    int choice;
    cout << "1) Insert at front " << endl;
    cout << "2) Delete at front" << endl;
    cout << "7) Merge two lists" << endl;
    cout << "9) Display" << endl << endl;
    while (choice != 99)
    {
        cout << "Your choice:";
        cin >> choice;

        switch (choice)
        {
            case 1:
                {
                    int sel;
                    cout << "Enter the list to which you want to enter:\n 1 or 2\nYour choice:";
                    cin >> sel;
                    if (sel == 1)
                    {
                        insertFront();
                    }
                    else if (sel == 2)
                    {
                        insertFront2();
                    }

                    break;
                }

            case 2:
                {
                    delFront();
                    break;
                }

            case 7:
                {
                    mergeLists(list,list2);

                    break;
                }

            case 9:
                {
                    int sel;
                    cout<<"Select a list to display: 1/2"<<endl;
                    cin>>sel;
                    display(sel);
                    break;
                }

            case 99:
                {
                    cout << "PROGRAM TERMINATED :)";
                    break;
                }
        }
    }

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    Did run your code in a **debugger** to see where that error occurs, then run it again with a breakpoint near that failure so you can step carefully ahead and watch what happens leading up to that point? – tadman Apr 06 '23 at 18:20
  • i dont know how to use a debugger – Mufazzal Gujjar Apr 06 '23 at 18:24
  • 2
    Time to learn. You'll be glad you did – Paul Sanders Apr 06 '23 at 18:36
  • 1
    [What is a debugger?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – PaulMcKenzie Apr 06 '23 at 18:36
  • Debuggers differe a bit from toolchain to toolchain, but the way you use it is always the same. Here's [how to use the debugger in Visual Studio](https://learn.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2022). Other GUI IDE debuggers ill be extremely similar, they just move the buttons around a bit and use different hotkeys. – user4581301 Apr 06 '23 at 18:40
  • 1
    General gist is you put a breakpoint before the bug you want to kill (if you don't know where that is, start at the beginning of `main`) and start stepping line-by-line. Pay attention to what the program does and when you see something unexpected, like the program storing the wrong value or taking the wrong path, stop and investigate closer. Restart the program and get a better look at what lead up to the unexpected behaviour, because the bug is almost certainly somewhere nearby. – user4581301 Apr 06 '23 at 18:40

2 Answers2

0

Inside merge(), you are looping through list until p becomes nullptr, thus p is nullptr when you try to access p->next after the loop.

You need to fix your loop to stop when it reaches the last node, not when it goes past the last node, eg:

void mergeLists(node *list, node *list2)
{
    if (!list && !list2){
        cout << "Both lists are empty";
    }

    else if (!list && list2){
        display(2);
    }

    else if (!list2 && list){
        display(1);
    }

    else {
        p = list;
        while (p->next){
            p = p->next;
        }

        p->next = list2;

        display(1);
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Answering these kinds of questions just encourages people to post more of them... – CoffeeTableEspresso Apr 06 '23 at 18:37
  • 2
    @CoffeeTableEspresso this is a Q&A site, we are here to answer questions, including beginner questions like this. Not everyone has access to *good-quality* programming education. – Remy Lebeau Apr 06 '23 at 18:44
0

For starters if you want to append the second list list2 to the first list list then after the calling the function merge the list list2 should be empty.

Within the function merge after this while loop

    while(p!=nullptr){
        p=p->next;
    }

the pointer p is a null pointer. So using the null pointer to access memory in this statement

    p->next=list2;

results in undefined behavior.

And this if statement

if(list!=nullptr && list2!=nullptr){

is redundant.

Using your approach the function can be defined the following way

void mergeLists(node *list,node *list2)
{
    if ( list == nullptr and list2 == nullptr )
    {
        cout<<"Both lists are empty";
    }
    else if ( list == nullptr && list2 != nullptr )
    {
        
        list = list2;
        list2 = nullptr; 
        display(1);
    }
    else if ( list2 == nullptr && list != nullptr )
    {
        display(1);
    }
    else
    {
        p = list;

        while ( p->next != nullptr )
        {
            p = p->next;
        }

        p->next = list2;
        list2 = nullptr;

        display(1);
    }
}

Pay attention to that it is a bad idea to make functions depend on global variables. For example if you will want to use one more list you will need to rewrite your functions.

Also you have too much redundant code as for example in the functions insertFront and insertFront2. They can be defined much simpler. For example

void insertFront()
{
    cout << "ENTER A VALUE = ";
    cin >> value;

    list = new node { value, list };
} 

and

void insertFront2()
{
    cout << "ENTER A VALUE = ";
    cin >> value;

    list2 = new node { value, list2 };
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335