C 数据结构实验之链表四:有序链表的归并 SDUT

Time Limit: 1000 ms Memory Limit: 65536 KiB


Problem Description

分别输入两个有序的整数序列(分别包含M和N个数据),建立两个有序的单链表,将这两个有序单链表合并成为一个大的有序单链表,并依次输出合并后的单链表数据。


Input

第一行输入M与N的值;
第二行依次输入M个有序的整数;
第三行依次输入N个有序的整数。


Output

输出合并后的单链表所包含的M+N个有序的整数。


Sample Input

6 5
1 23 26 45 66 99
14 21 28 50 100


Sample Output

1 14 21 23 26 28 45 50 66 99 100


Hint

不得使用数组!


Source


先定义两个链表,在定义第三个链表,并对前两个链表从第一个开始比较,那一个小,边连接在第三个链表上;

代码一:因为在链表的题目中代码较前面的题目长很多,所以采用函数可以使代码看起来更加清晰明朗,能灵活运用函数定义对以一个程序员来说也是非常重要的,

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int a;
    struct node *next;
};
void display(struct node *head);//输出函数;
struct node *found(struct node *head,int n);//建立函数;
struct node*f(struct node *head1,struct node *head2);//归并函数;
//事先声明三个函数;
int main()//主函数;
{
    struct node *head1,*head2,*head3;
    head1 = (struct node *)malloc(sizeof(struct node));
    head1 -> next = NULL;
    head2 = (struct node *)malloc(sizeof(struct node));
    head2 -> next = NULL;
    int n,m;
    scanf("%d%d",&m,&n);
    head1 = found(head1,m);
    head2 = found(head2,n);
    head3 = f(head1,head2);
    display(head3);
    return 0;
}

void display(struct node *head)
//链表输出函数
{
    struct node *p;
    p = head -> next;
    while(p)
    {
        printf("%d%c",p -> a,p->next?' ':'\n');
        //用双条件语句,也可用两个单条件语句;
         p = p -> next;
    }
}
struct node *found(struct node *head,int n)
//链表建立函数;
{
    struct node *q,*p = head;
    int i;
    getchar();
    for(i=0; i<n; i++)
    {
        q = (struct node*)malloc(sizeof(struct node));
        scanf("%d",&q -> a);
        q -> next = NULL;
        p -> next = q ;
        p = q;

    }
    return(head);
};

struct node*f(struct node *head1,struct node *head2)
//链表归并函数;
{
    struct node *head3,*p,*pi,*q;
    head3 = (struct node*)malloc(sizeof(struct node));
    head3 -> next = NULL;
    q = head3;
    p = head1 -> next;
    pi = head2 -> next;
    free(head1);
    free(head2);
    while(p&&pi)
    /*只要有一个指针为零便结束,因为只要将还没有结束的那个链表剩下的连上就行,
    并且如果p和pi有一个为空下面的判断语句便无法执行,并出现错误*/
    {
        if( p -> a < pi -> a)
        {
            q -> next  = p;
            q = p;
            p = p -> next;
        }
        else
        {
            q -> next = pi;
            q = pi;
            pi = pi -> next;

        }
    }
    if(p)//判断哪个链表还有剩下的;
        q -> next  = p;
    else if(pi)
        q -> next = pi;
    return(head3);

};


代码二:

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int a;
    struct node *next;
};
int main()
{
    int m,n,i,a1;
    struct node *head1,*head2,*head3,*q,*p,*i1;
    scanf("%d%d",&m,&n);
    head1 = (struct node *)malloc(sizeof(struct node));
    head1 -> next = NULL;
    q = head1;
    for(i=0; i<m; i++)
    {
        scanf("%d",&a1);
        p = (struct node *)malloc(sizeof(struct node));
        p -> next = NULL;
        p -> a  = a1;
        q -> next = p;
        q = p;

    }  //定义第一个链表;
    head2 = (struct node *)malloc(sizeof(struct node));
    head2 -> next = NULL;
    q = head2;
    for(i=0; i<n; i++)
    {
        scanf("%d",&a1);
        p = (struct node *)malloc(sizeof(struct node));
        p -> next = NULL;
        p -> a  = a1;
        q -> next = p;
        q = p;

    }   //定义第二个链表;
    head3 = (struct node *)malloc(sizeof(struct node));
    head3 -> next = NULL;
    i1 = head1 -> next;
    q = head2 -> next;
    p = head3;
    free(head1);
    free(head2);
    for(i=0;i<n+m;i++)
    {
      if(i1&&q)
      {
         if(i1 -> a < q -> a)
        {
            p -> next = i1;
            p = i1;
            i1 = i1 -> next;
        }
        else
        {

            p -> next= q;
            p = q;
            q = q -> next;
        }
      }
      else
      {
          if(i1)
            p -> next =i1;
          if(q)
            p -> next = q;
      }
    }
    p = head3 -> next;
     while(p) 
     {
         if(p -> next)
         printf("%d ",p -> a);
         else printf("%d\n",p -> a);
         p = p -> next;

     }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

碧羽o(* ̄▽ ̄*)ブ回雪

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

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

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

打赏作者

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

抵扣说明:

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

余额充值