简体   繁体   中英

How to merge two sorted linked lists in C?

I am trying to merge two sorted linked lists but not getting the desired O/P. I think there is some problem with address allocation but i am not sure. Can anybody suggest why i'm not getting any o/p ?

struct Node* SortedMerge(struct Node* a, struct Node* b)
{
    struct Node *head;
    struct Node  **tail=&head;

    while(1)
    {
        if(a==NULL)
        {
           *tail=b;
            break;
        }

        if(b==NULL)
        {
            *tail=a;
            break;
        }

        if(a->data<=b->data)
        {
            *tail=a;
            a=a->next;
            (*tail)->next=NULL;
        }
        else
        {
            *tail=b;
            b=b->next;
            (*tail)->next=NULL;
        }

        (*tail)=(*tail)->next;
    }
    return head;

}
(*tail)=(*tail)->next;

Replace this with:-

tail = &( (*tail)->next );

I hope it will work.

You should consider using an append function to simplify your code. Here is an example:

#include <stdio.h>
#include <stdlib.h>

struct List {
    struct Node *head;
    struct Node *tail;
};

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

void append(struct List *list, struct Node *node) {
    struct Node *nodeCopy = malloc(sizeof(struct Node));
    nodeCopy->data = node->data;
    nodeCopy->next = NULL;

    if (list->head == NULL) { // Empty list
        list->head = nodeCopy;
        list->tail = nodeCopy;
    } else {
        list->tail->next = nodeCopy;
        list->tail = nodeCopy;
    }
}

void print(struct List *list) {
    for (struct Node *node = list->head; node != NULL; node = node->next) {
        printf("%d,", node->data);
    }
    printf("\n");
}

struct List* SortedMerge(struct Node* a, struct Node* b)
{
    struct List *list = malloc(sizeof(struct List));
    list->head = NULL;
    list->tail = NULL;

    while(1)
    {
        if(a==NULL && b==NULL)
        {
            break;
        }

        if(a==NULL && b!=NULL)
        {
            append(list, b);
            b = b->next;
        }

        else if(b==NULL && a!=NULL)
        {
            append(list, a);
            a = a->next;
        }

        else if(a->data<=b->data)
        {
            append(list, a);
            a = a->next;
        }
        else
        {
            append(list, b);
            b = b->next;
        }
    }

    return list;
}

int main() {
    struct List listA = { NULL, NULL };
    struct List listB = { NULL, NULL };

    struct Node node1 = { 1, NULL };
    struct Node node2 = { 2, NULL };
    struct Node node3 = { 3, NULL };
    struct Node node4 = { 4, NULL };
    struct Node node5 = { 5, NULL };

    append(&listA, &node1);
    append(&listA, &node4);
    print(&listA);

    append(&listB, &node2);
    append(&listB, &node3);
    append(&listB, &node5);
    print(&listB);

    struct List *mergedList1 = SortedMerge(listA.head, listB.head);
    print(mergedList1);

    struct List *mergedList2 = SortedMerge(listB.head, listA.head);
    print(mergedList2);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM