i am trying to merg two linked list.
error occurs when i run the program it dead.
i believe it is because the function i defined to merge these two list has problems
would someone helpe me to fix it
or some hint would be nice
thanks

#include <stdio.h> 
#include<malloc.h> 
typedef struct listNode{ 
        int* dataPtr; 
        struct listNode* next; 
}ListNode; 
 
typedef struct list{ 
        ListNode* head; 
        ListNode* tail; 
}List; 
 
void initList(List *lst){ 
        lst->head = NULL; 
        lst->tail = NULL; 
} 
void insertToEnd(List *lst, int *data){ 
        ListNode *node; 
        node = (ListNode*)malloc(sizeof(ListNode)); 
        node->dataPtr = data; 
        node->next = NULL; 
        if(lst->head == NULL){ 
                lst->head = node; 
        } 
        else{ 
                lst->tail->next = node; 
        } 
        lst->tail = node; 
} 
void add(List *lst, ListNode* node){ 
        if(lst->head == NULL){ 
                lst->head = node; 
        } 
        else{ 
                lst->tail->next = node; 
        } 
        lst->tail = node; 
} 
List getNewList(){ 
        List newList; 
        newList.head = NULL; 
        newList.tail = NULL; 
        return newList; 
} 
void printList(List *lst){ 
        ListNode *node; 
        int *i; 
        node = lst->head; 
        while(node){ 
                i = (node->dataPtr); 
                printf("%d ", *i); 
                node = node->next; 
        } 
        printf("\n"); 
} 
         
 
List merge2(List lst1, List lst2){ 
        ListNode *ptr1; 
        ListNode *ptr2; 
        ListNode *temp; 
        int *val1; 
        int *val2; 
 
        ptr1 = lst1.head; 
        ptr2 = lst2.head; 
        val1 = ptr1->dataPtr; 
        val2 = ptr2->dataPtr; 
 
        //swap ptr1 and ptr2 if ptr2 is smaller than ptr1 
        if(*val1 > *val2){ 
                temp = lst2.head; 
                ptr2 = ptr2->next; 
                temp->next = lst1.head; 
                lst1.head = temp;  
        } 
        else { 
                ptr1 = ptr1->next; 
        } 
 
        while(ptr1 && ptr2){ 
                val1 = ptr1->dataPtr; 
                val2 = ptr2->dataPtr; 
                 
                if(*val1 > *val2){ 
                       temp->next = ptr2;
                          temp = ptr2;
                          ptr2 = ptr2-> next;

                } 
                else { 
                        ptr1 = ptr1->next; 
                } 
                if(ptr1 == NULL)
                {    while(ptr2 != NULL)
                              temp->next=ptr2;
         //add the rest of List2 to your current list
} 
else if( ptr2 == NULL)
{
     while(ptr1 != NULL)
     temp->next=ptr1;
          //  add the rest of List1 to your current list.
}        

        } 
        return lst1;     
} 

        
          

 
int main(){ 
        List lst1; 
        List lst2; 
        List mrg; 
        int i; 
        int nums1[4] = {2,3,9,10}; 
        int nums2[3] = {0,2,6,}; 
        initList(&lst1); 
        initList(&lst2); 
        for(i = 0 ;i < 4; i++){ 
                insertToEnd(&lst1, (nums1 + i)); 
        } 
        for(i = 0 ;i < 3; i++){ 
                insertToEnd(&lst2, (nums2 + i)); 
        } 
        mrg = merge2(lst1, lst2); 
        printList(&mrg); 
        return 0;
}

It would be best to post as a thread instead of a snippet. Hopefully a moderator can change your post type for you this time.

In addition, the code as posted looks pretty much C rather than classic C++, and may best be posted to the C board.

It's been a while since I've written in C but I'm pretty sure this:

typedef struct list
{
ListNode* head;
ListNode* tail;
}List;

declares a user defined type called list and declares an instance of that type called List. If that's is correct, then you can't use List as a return type, you would have to use list.

Decide if you are going to write in C or C++ and review the declaration syntax to see if my memory serves correct or not. If you want to write in C and my memory is correct regarding user defined types, then make the appropriate adjusctments before proceeding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.