C语言将两个有序链表合并成一个有序链表

问题描述:输入两个有序链表,将它们合并成一个有序链表。

我们采用的方法,主要是创建一个新的链表,在创建时新加入的表元从原本的两个链表中选取,由于链表本身是有序的,故只需不断地将两个表元进行对比,较小者存入新链表,较大者下一轮再对比。关于链表的一些基本的函数,在上一篇博客中都有提到。

以下是完整的代码,其中mergeLists是主要的合并函数:

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

typedef struct Node {
    int value;
    struct Node* next;
} Node;

Node* createNode(int value) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    if (newNode == NULL) {
        printf("Memory allocation failed.\n");
        exit(1);
    }
    newNode->value = value;
    newNode->next = NULL;
    return newNode;
}

Node* createOriginalList() {
    Node* head = NULL;
    Node* tail = NULL;
    int n;

    printf("Enter the number of nodes in the linked list:");
    scanf("%d", &n);

    printf("Enter the values of the linked list nodes:\n");
    for (int i = 0; i < n; i++) {
        int value;
        printf("Value of Node %d:", i + 1);
        scanf("%d", &value);

        Node* newNode = createNode(value);
        if (NULL == head) {
            head = newNode;
            tail = newNode;
        }
        else {
            tail->next = newNode;
            tail = newNode;
        }
    }

    return head;
}
//合并两个有序链表
Node* mergeLists(Node* list1, Node* list2) {
    Node* result = NULL;//新链表的头尾指针
    Node* tail = NULL;

    Node* currentS1 = list1;
    Node* currentS2 = list2;

    //双指针分别遍历S1和S2,直到其中一个被遍历完,按从小到大的顺序添加节点到结果中
    while (currentS1 != NULL && currentS2 != NULL) {
        if (currentS1->value < currentS2->value) {
            Node* newNode = createNode(currentS1->value);
            if (result == NULL) {//插入到头表元的情况
                result = newNode;
                tail = newNode;
            }
            else {//直接从末尾插入
                tail->next = newNode;
                tail = newNode;
            }
            currentS1 = currentS1->next;//向后历遍
        }
        else if (currentS1->value > currentS2->value) {
            Node* newNode = createNode(currentS2->value);
            if (result == NULL) {
                result = newNode;
                tail = newNode;
            }
            else {
                tail->next = newNode;
                tail = newNode;
            }
            currentS2 = currentS2->next;
        }
        else {
            //两个节点值相等,都要插入
            Node* newNode1 = createNode(currentS1->value);
            if (result == NULL) {
                result = newNode1;
                tail = newNode1;
            }
            else {
                tail->next = newNode1;
                tail = newNode1;
            }
            Node* newNode2 = createNode(currentS2->value);
            tail->next = newNode2;
            tail = newNode2;
            currentS1 = currentS1->next;
            currentS2 = currentS2->next;
        }
    }
    while (currentS1 != NULL) {//链接链表1剩下的元素(如果有)
        Node* newNode = createNode(currentS1->value);
        if (result == NULL) {
            result = newNode;
            tail = newNode;
        }
        else {
            tail->next = newNode;
            tail = newNode;
        }
        currentS1 = currentS1->next;
    }
    while (currentS2 != NULL) {//链接链表2剩下的元素(如果有)
        Node* newNode = createNode(currentS2->value);
        if (result == NULL) {
            result = newNode;
            tail = newNode;
        }
        else {
            tail->next = newNode;
            tail = newNode;
        }
        currentS2 = currentS2->next;
    }

    return result;
}
void printList(Node* head) {
    if (head == NULL) {
        printf("链表为空。\n");
        return;
    }
    Node* current = head;
    while (current != NULL) {
        printf("%d ", current->value);
        current = current->next;
    }
    printf("\n");
}
void freeList(Node* head) {
    Node* current = head;
    Node* next;

    while (current != NULL) {
        next = current->next;
        free(current);
        current = next;
    }
}

int main() {
    printf("Create the first sorted linked list:\n");
    Node* list1 = createOriginalList();
    printf("Create the second sorted linked list:\n");
    Node* list2 = createOriginalList();
    Node* mergedList = mergeLists(list1, list2);
    printf("Merged sorted linked list: ");
    printList(mergedList);
    freeList(mergedList);
    freeList(list2);
    freeList(list1);
    return 0;
}

以下是输入的样例:

这里,对于更多的情况下,我们输入的链表不一定是有序的。这时再需要将它们合并为有序的链表,可以直接在输入创建原始链表的时候,直接创建成有序的链表。之后,可以像上述方法一样合并,或者(在没有新链表要求的时候)直接将有序的链表2插入链表1中,以下为完整代码(使用的方法是后者):

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef struct Node {
    int value;
    struct Node* next;
} Node;
Node* creatsortedlist()
{
    int n;
    printf("Enter the number of nodes in the linked list:");
    scanf("%d", & n);
    Node* head = NULL, * p, * new = NULL, * old = NULL;
    printf("Enter the values of the linked list nodes:\n");
    for (int i = 0; i < n; i++) {
        p = (Node*)malloc(sizeof(Node));
        if (p == NULL) {
            printf("Memory allocation failed.\n");
            exit(1);
        }
        printf("Value of Node %d:", i + 1);
        scanf("%d", &p->value);
        //对输入的每一个节点,判断其按值大小的插入位置
        new = head;
        while (new != NULL && p->value > new->value)
        {
            old = new;
            new = new->next;
        }
        //old记录插入位置之前的节点
        //new记录插入位置之后的节点
        if (new == head)
        {
            head = p;
        }
        else
            old->next = p;
        p->next = new;
    }
    return head;
}
Node* merged_sortedlink(Node* head1, Node* head2)
{
    Node* p, * new = NULL, * old = NULL, * p2 = head2;
    while (p2 != NULL)
    {
        //顺序将链表2中的每一项插入到链表1中
        p = (Node*)malloc(sizeof(Node));
        if (p == NULL) {
            printf("Memory allocation failed.\n");
            exit(1);
        }
        p->value = p2->value;
        new = head1;
        while (new != NULL && p->value > new->value)
        {
            old = new;
            new = new->next;
        }
        if (new == head1)
        {
            head1 = p;
        }
        else
            old->next = p;
        p->next = new;
        p2 = p2->next;
    }
    return head1;
}
void printList(Node* head) {
    if (head == NULL) {
        printf("链表为空。\n");
        return;
    }
    Node* prhead = head;
    while (prhead != NULL) {
        printf("%d ", prhead->value);
        prhead = prhead->next;
    }
    printf("\n");
}
void freeList(Node* head) {
    Node* current = head;
    Node* next;
    while (current != NULL) {
        next = current->next;
        free(current);
        current = next;
    }
}
int main()
{
    printf("Create the first sorted linked list:");
    Node* head1 = creatsortedlist();
    printf("Create the second sorted linked list:");
    Node* head2 = creatsortedlist();
    Node* head = merged_sortedlink(head1, head2);
    printf("Merged sorted linked list: ");
    printList(head);
    freeList(head1);
    freeList(head2);
	return 0;
}

以下是样例输入:

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当我们需要合并两个有序链表时,我们可以使用迭代的方式来解决这个问题。首先,我们需要定义一个新的链表头节点,并设置一个指针用于遍历两个链表。然后,我们比较两个指针所指向的节点的值,将较小的节点添加到新链表中,并将指针向后移动。重复这个过程,直到其中一个链表遍历完毕。最后,将剩余链表的节点直接添加到新链表的末尾。 下面是使用C语言实现这个算法的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 struct ListNode { int val; struct ListNode* next; }; // 创建新节点 struct ListNode* createNode(int val) { struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode)); newNode->val = val; newNode->next = NULL; return newNode; } // 合并两个有序链表 struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { // 创建一个新的链表头节点 struct ListNode* dummy = createNode(0); // 用于遍历新链表两个原始链表 struct ListNode* cur = dummy; while (l1 != NULL && l2 != NULL) { // 比较两个节点的值 if (l1->val <= l2->val) { cur->next = l1; l1 = l1->next; } else { cur->next = l2; l2 = l2->next; } cur = cur->next; } // 将剩余链表的节点直接添加到新链表的末尾 if (l1 != NULL) { cur->next = l1; } if (l2 != NULL) { cur->next = l2; } return dummy->next; } // 打印链表 void printList(struct ListNode* head) { struct ListNode* cur = head; while (cur != NULL) { printf("%d ", cur->val); cur = cur->next; } printf("\n"); } int main() { // 创建两个有序链表 struct ListNode* l1 = createNode(1); l1->next = createNode(3); l1->next->next = createNode(5); struct ListNode* l2 = createNode(2); l2->next = createNode(4); l2->next->next = createNode(6); // 合并两个有序链表 struct ListNode* mergedList = mergeTwoLists(l1, l2); // 打印合并后的链表 printf("Merged List: "); printList(mergedList); return 0; } ``` 运行上述代码,输出结果为: ``` Merged List: 1 2 3 4 5 6 ``` 这是将两个有序链表合并一个有序链表的基本思路和实现方法。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值