两个有序链表序列的合并 -C语言实现

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

typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
    ElementType Data;
    PtrToNode   Next;
};
typedef PtrToNode List;

List Read(int a[],int n); /* 细节在此不表 */
void Print( List L ); /* 细节在此不表;空链表将输出NULL */

List Merge( List L1, List L2 );

int main()
{
    List L1, L2, L;
    int l1[5] = {2,4,6,8,10};
    int l2[5] = {2,4,6,8,10};
    
    L1 = Read(l1,5);
    L2 = Read(l2,5);
    L = Merge(L1, L2);
    Print(L);
    Print(L1);
    Print(L2);
    return 0;
}
void Print( List L )
{
    List p = L->Next;
    if(!p){
        printf("NULL\n");
    }
    else{
        while (p) {
            printf("%d ",p->Data);
            p = p->Next;
        }
        printf("\n");
    }
}

List Read(int a[],int n)
{
    List head = (List)malloc(sizeof(List));
    head->Data = n;
    head->Next = NULL;
    int i;
    List p,last;
    last = head;
    
    for (i = 0; i < n; i++) {
        p = (List)malloc(sizeof(List));
        p->Data = a[i];
        p->Next = NULL;
        last->Next = p;
        last = p;
    }
    return head;
}

List Merge( List L1, List L2 )
{
    List head,tail;
    head = (List)malloc(sizeof(struct Node));
    tail = head;
    head->Next = NULL;
    head->Data = L1->Data + L2->Data;
    List p,q;
    p = L1->Next;
    q = L2->Next;
   
    
    while (p && q)
    {
        if(p->Data  > q->Data)
        {
            tail->Next = q;
            tail = q;
            q = q->Next;
        }
        else if(p->Data  < q->Data)
        {
            tail->Next = p;
            tail = p;
            p = p->Next;
        }
        else
        {
            tail->Next = p;
            tail = p;
            p = p->Next;
            tail->Next = q;
            tail = q;
            q = q->Next;
            
        }
    }
    while (p)
    {
        tail->Next = p;
        tail = p;
        p = p->Next;
    }
    while (q)
    {
        tail->Next = q;
        tail = q;
        q = q->Next;
    }
    L1->Next = NULL;
    L2->Next = NULL;
    return head;
}

 

在C语言中,合并两个有序链表可以采用迭代或者递归的方式实现。这里是一个简单的迭代方法的示例: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 typedef struct ListNode { int val; struct ListNode* next; } ListNode; // 合并两个有序链表 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { // 创建一个新的头节点,并初始化为NULL ListNode* dummy = (ListNode*)malloc(sizeof(ListNode)); dummy->next = NULL; ListNode* current = dummy; // 当前指针 while (l1 != NULL && l2 != NULL) { // 只要任一链表非空 if (l1->val <= l2->val) { // 将较小的值节点插入当前节点 current->next = l1; l1 = l1->next; } else { current->next = l2; l2 = l2->next; } current = current->next; // 移动到下一个位置 } // 如果其中一个链表已经遍历完,则将另一个链表剩余部分添加到结果链表尾部 if (l1 != NULL) { current->next = l1; } else { current->next = l2; } return dummy->next; // 返回新的头节点 } // 测试函数 void printList(ListNode* head) { ListNode* temp = head; while (temp != NULL) { printf("%d ", temp->val); temp = temp->next; } printf("\n"); } int main() { // 创建两个示例链表 ListNode* list1 = (ListNode*)malloc(sizeof(ListNode)); list1->val = 1; list1->next = (ListNode*)malloc(sizeof(ListNode)); list1->next->val = 2; list1->next->next = NULL; ListNode* list2 = (ListNode*)malloc(sizeof(ListNode)); list2->val = 3; list2->next = (ListNode*)malloc(sizeof(ListNode)); list2->next->val = 4; list2->next->next = NULL; // 合并链表 ListNode* merged = mergeTwoLists(list1, list2); // 打印合并后的链表 printList(merged); return 0; } ``` 在这个例子中,`mergeTwoLists` 函数接收两个有序链表的头节点作为参数,返回合并后的有序链表头节点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值