C语言中线性表的合并&有序表的合并以及案例分析和实现

文章介绍了如何在C语言中合并两个线性列表,包括无序合并和有序合并的示例代码,展示了合并过程和函数实现细节。
摘要由CSDN通过智能技术生成

**线性列表合并:**

 

案例分析:

要合并两个线性列表,我们考虑以下情况:

 

1. 两个列表都是空的:在这种情况下,合并的列表也将是空的。

2. 一个列表是空的:如果其中一个列表是空的,则合并的列表将是非空列表本身。

3. 两个列表都是非空的:在这种情况下,我们同时遍历两个列表,将两个列表中的元素附加到合并的列表中,直到我们到达任一列表的末尾。之后,我们将非空列表中的其余元素附加到合并列表中。

 

实现:

'''c

 

#include <stdio.h>

#include <stdlib.h>

 

struct Node {

    int data;

    struct Node* next;

};

 

struct Node* mergeLinearLists(struct Node* list1, struct Node* list2) {

    if (list1 == NULL)

        return list2;

    if (list2 == NULL)

        return list1;

 

    struct Node* mergedList = NULL;

    struct Node* current = NULL;

 

    // Append elements from list1 to merged list

    if (list1) {

        mergedList = list1;

        current = list1;

        list1 = list1->next;

    }

 

    // Append elements from list2 to merged list

    while (list1 && list2) {

        current->next = list2;

        current = current->next;

        list2 = list2->next;

    }

 

    // Append remaining elements from list1 or list2

    if (list1)

        current->next = list1;

    else

        current->next = list2;

 

    return mergedList;

}

 

void displayList(struct Node* head) {

    struct Node* current = head;

    while (current != NULL) {

        printf("%d ", current->data);

        current = current->next;

    }

    printf("\n");

}

 

int main() {

    struct Node* list1 = NULL;

    struct Node* list2 = NULL;

 

    // Create list1

    list1 = (struct Node*)malloc(sizeof(struct Node));

    list1->data = 1;

    list1->next = (struct Node*)malloc(sizeof(struct Node));

    list1->next->data = 2;

    list1->next->next = (struct Node*)malloc(sizeof(struct Node));

    list1->next->next->data = 3;

    list1->next->next->next = NULL;

 

    // Create list2

    list2 = (struct Node*)malloc(sizeof(struct Node));

    list2->data = 4;

    list2->next = (struct Node*)malloc(sizeof(struct Node));

    list2->next->data = 5;

    list2->next->next = NULL;

 

    printf("List 1: ");

    displayList(list1);

 

    printf("List 2: ");

    displayList(list2);

 

    struct Node* mergedList = mergeLinearLists(list1, list2);

 

    printf("Merged List: ");

    displayList(mergedList);

 

    return 0;

}

 

'''

 

在此实现中,我们定义了一个“节点”结构,该结构表示列表中的节点。'mergeLinearLists' 函数将两个链表('list1' 和 'list2')作为输入,并返回一个合并的列表。它处理前面讨论的三种情况,并相应地合并列表。“displayList”函数用于打印列表的元素。

 

在“main”函数中,我们创建了两个列表(“list1”和“list2”),其中包含一些示例数据。然后,我们调用“mergeLinearLists”函数来合并列表并将结果分配给“mergedList”。最后,我们显示原始列表和合并列表以验证合并过程。

 

**有序列表合并:**

 

案例分析:

为了合并两个有序列表,我们假设列表已经按升序排序。我们考虑以下情况:

 

1. 两个列表都是空的:在这种情况下,合并的列表也将是空的。

2. 一个列表是空的:如果其中一个列表是空的,则合并的列表将是非空列表本身。

3. 两个列表都是非空的:在这种情况下,我们同时遍历两个列表,比较每个位置的元素。我们将较小的元素追加到合并的列表中,并移动到相应列表中的下一个元素。到达任一列表的末尾后,我们将非空列表中的剩余元素附加到合并列表中。

实现:

'''c

 

#include <stdio.h>

#include <stdlib.h>

 

struct Node {

    int data;

    struct Node* next;

};

 

struct Node* mergeOrderedLists(struct Node* list1, struct Node* list2) {

    if (list1 == NULL)

        return list2;

    if (list2 == NULL)

        return list1;

 

    struct Node* mergedList = NULL;

    struct Node* current = NULL;

 

    // Determine the merged list's head

    if (list1->data <= list2->data) {

        mergedList = list1;

        list1 = list1->next;

    }

    else {

        mergedList = list2;

        list2 = list2->next;

    }

 

    current = mergedList;

 

    // Merge the lists

    while (list1 && list2) {

        if (list1->data <= list2->data) {

            current->next = list1;

            list1 = list1->next;

        }

        else {

            current->next = list2;

            list2 = list2->next;

        }

        current = current->next;

    }

 

    // Append remaining elements from list1 or list2

    if (list1)

        current->next = list1;

    else

        current->next = list2;

 

    return mergedList;

}

 

void displayList(struct Node* head) {

    struct Node* current = head;

    while (current != NULL) {

        printf("%d ", current->data);

        current = current->next;

    }

    printf("\n");

}

 

int main() {

    struct Node* list1 = NULL;

    struct Node* list2 = NULL;

 

    // Create list1

    list1 = (struct Node*)malloc(sizeof(struct Node));

    list1->data = 1;

    list1->next = (struct Node*)malloc(sizeof(struct Node));

    list1->next->data = 3;

    list1->next->next = (struct Node*)malloc(sizeof(struct Node));

    list1->next->next->data = 5;

    list1->next->next->next = NULL;

 

    // Create list2

    list2 = (struct Node*)malloc(sizeof(struct Node));

    list2->data = 2;

    list2->next = (struct Node*)malloc(sizeof(struct Node));

    list2->next->data = 4;

    list2->next->next = NULL;

 

    printf("List 1: ");

    displayList(list1);

 

    printf("List 2: ");

    displayList(list2);

 

    struct Node* mergedList = mergeOrderedLists(list1, list2);

 

    printf("Merged List: ");

    displayList(mergedList);

 

    return 0;

}

 

'''

 

在此实现中,“mergeOrderedLists”函数将两个有序列表(“list1”和“list2”)合并为一个有序列表。它首先根据输入列表的第一个元素的比较来确定合并列表的头部。然后,它循环访问两个列表,比较每个位置的元素,并将较小的元素追加到合并的列表中。最后,它将非空列表中的任何剩余元素追加到合并的列表中。

 

“displayList”函数用于打印列表的元素。

 

在 'main' 函数中,我们创建了两个有序列表('list1' 和 'list2'),其中包含一些示例数据。然后,我们调用“mergeOrderedLists”函数来合并列表并将结果分配给“mergedList”。最后,我们显示原始列表和合并列表以验证合并过程。

  • 31
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BlurryFace36549

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值