SCAU:18064 链表的有序合并

18064 链表的有序合并

时间限制:1000MS  代码长度限制:10KB
提交次数:0 通过次数:0

题型: 填空题   语言: G++;GCC;VC

Description

已知有两个链表a和b,结点类型相同,均包括一个int类型的数据。编程把两个链表合并成一个,结点按升序排列。


#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct DATA)

struct DATA
{
     long num;
     struct DATA *next;
};

struct DATA *create(int n)
{
     struct DATA *head=NULL,*p1=NULL,*p2=NULL;
     int i;
     for(i=1;i<=n;i++)
     {  p1=(struct DATA *)malloc(LEN);
        scanf("%ld",&p1->num);
        p1->next=NULL;
        if(i==1) head=p1;
        else p2->next=p1;
        p2=p1;
      }
      return(head);
}

struct DATA *merge(struct DATA *head, struct DATA *head2)
{
    _______________________
    return head;
}

struct DATA *insert(struct DATA *head, struct DATA *d)
{
    _______________________
    return head;
}

struct DATA *sort(struct DATA *head) 

    _______________________
    return head;


void print(struct DATA *head)
{
    struct DATA *p;
    p=head;
    while(p!=NULL)
    {
        printf("%ld",p->num);
        p=p->next;
        printf("\n");
    }
}

main()
{
    struct DATA *head, *head2;
    int n;
    long del_num;
    scanf("%d",&n);
    head=create(n);
    scanf("%d",&n);
    head2=create(n);
    head = merge(head, head2);
    head = sort(head);
    print(head);
}
 

输入格式

第一行一个数n,表示第一个列表的数据个数
每二行为n个数
第三行为一个数m
第四行为m个数

输出格式

输出合并后的有序的数据,一行一个数

输入样例

2
4 8
3
9 1 5

输出样例

1
4
5
8
9

#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct DATA)

struct DATA
{
     long num;
     struct DATA *next;
};

struct DATA *create(int n)
{
     struct DATA *head=NULL,*p1=NULL,*p2=NULL;
     int i;
     for(i=1;i<=n;i++)
     {  p1=(struct DATA *)malloc(LEN);
        scanf("%ld",&p1->num);
        p1->next=NULL;
        if(i==1) head=p1;
        else p2->next=p1;
        p2=p1;
      }
      return(head);
}

struct DATA *merge(struct DATA *head, struct DATA *head2)
{
    struct DATA *p1;
    p1=head;
    while(p1->next!=NULL)
    {
        p1=p1->next;
    }
    p1->next=head2;
    return head;
}

struct DATA *insert(struct DATA *head, struct DATA *d)
{
    struct DATA *p1,*p2,*p3;
    p1=head;
    p2=d;
    if(head==NULL)
    {
        head=p2;
        p2->next=NULL;
    }
    else
    {
        while((p1->num < p2->num) && (p1->next!=NULL))
        {
            p3=p1;
            p1=p1->next;
        }
        if(p1->num >= p2->num)
        {
            if(head==p1)
                head=p2;
            else
                p3->next=p2;
            p2->next=p1;
        }
        else
        {
            p1->next=p2;
            p2->next=NULL;
        }
    }
    return head;
}

struct DATA *sort(struct DATA *head) 
{ 
    struct DATA *p1=head,*p2=head;
    p1=p1->next;
    p2->next=NULL;
    p2=p1;
    p1=p1->next;
    p2->next=NULL;
    while(p1!=NULL)
    {
        head=insert(head,p2);
        p2=p1;
        p1=p1->next;
        p2->next=NULL;
    }
    head=insert(head,p2);
    return head;
} 

void print(struct DATA *head)
{
    struct DATA *p;
    p=head;
    while(p!=NULL)
    {
        printf("%ld",p->num);
        p=p->next;
        printf("\n");
    }
}

main()
{
    struct DATA *head, *head2;
    int n;
    long del_num;
    scanf("%d",&n);
    head=create(n);
    scanf("%d",&n);
    head2=create(n);
    head = merge(head, head2);
    head = sort(head);
    print(head);
}

  • 42
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zero_019

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

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

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

打赏作者

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

抵扣说明:

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

余额充值