合并两个有序链表(递归和非递归)

合并两个有序链表

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

typedef char linktype;
typedef struct linklist{
    linktype data;
    struct linklist *next;
}linklist;

void linklistinit(linklist **head)
{
    if(head == NULL)
    {
        return;
    }
    *head = NULL;
}

void linklistprint(linklist *head)
{
    if(head == NULL)
    {
        return;
    }
    linklist *cur = head;
    while(cur != NULL)
    {
        printf("%c",cur->data);
        cur = cur->next;
    }
    printf("\n");
}

linklist * creat(linktype value)
{
    linklist *newnode = (linklist*)malloc(sizeof(linklist));
    if(newnode == NULL)
    {
        return NULL;
    }
    newnode->data = value;
    newnode->next = NULL;
    return newnode;
}

void linklist_pushback(linklist **head,linktype value)
{
    if(head == NULL)
    {
        return;
    }
    if(*head == NULL)
    {
        *head = creat(value);
        return;
    }
    linklist *new = creat(value);
    linklist *cur = *head;
    while(cur ->next != NULL)
    {
        cur = cur->next;
    }
    cur->next = new;
}

linklist *merge1(linklist *head1,linklist *head2)
{
    linklist *newhead = NULL;
    if(head1 == NULL)
    {
        return head2;
    }
    if(head2 == NULL)
    {
        return head1;
    }
    else
    {
        if((head1->data) < (head2->data))
        {
            newhead = head1;
            newhead->next = merge1(head1->next,head2);
        }
        else
        {
            newhead = head2;
            newhead->next = merge1(head1,head2->next);
        }
        return newhead;

    }
}
linklist *merge2(linklist *head1,linklist *head2)
{
    linklist *newtail = NULL;
    linklist *newhead = NULL;
    if(head1 == NULL)
    {
        return head2;
    }
    if(head2 == NULL)
    {
        return head1;
    }
    if(head1->data <= head2->data)
    {
        newhead = head1;
        head1 = head1->next;
    }
    else
    {
        newhead = head2;
        head2 = head2->next;
    }
    newtail = newhead;
    while(head1 && head2)
    {
        if(head1->data < head2->data)
        {
            newtail->next = head1;
            head1 = head1->next;
        }
        else
        {
            newtail->next = head2;
            head2 = head2->next;
        }
        newtail = newtail->next;
    }
    if(head1 == NULL)
    {
        newtail->next = head2;
    }
    else
    {
        newtail->next = head1;
    }
        return newhead;
}
int main()
{
    linklist *head1;
    linklist *head2;
    linklistinit(&head1);
    linklistinit(&head2);
    linklist_pushback(&head1,'1');
    linklist_pushback(&head1,'3');
    linklist_pushback(&head1,'5');
    linklist_pushback(&head1,'7');
    linklist_pushback(&head2,'2');
    linklist_pushback(&head2,'4');
    linklist_pushback(&head2,'6');
    linklist_pushback(&head2,'8');
    linklist *head = merge1(head1,head2);
    linklistprint(head);
    linklistprint(head1);
    linklist *head3;
    linklist *head4;
    linklistinit(&head3);
    linklistinit(&head4);
    linklist_pushback(&head3,'1');
    linklist_pushback(&head3,'3');
    linklist_pushback(&head3,'5');
    linklist_pushback(&head3,'7');
    linklist_pushback(&head4,'2');
    linklist_pushback(&head4,'4');
    linklist_pushback(&head4,'6');
    linklist_pushback(&head4,'8');
    linklist *head5 = merge2(head3,head4);
    linklistprint(head5);
    return 0;
}

结果

这里写图片描述

©️2020 CSDN 皮肤主题: 精致技术 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值