SCAU程序设计在线实训平台_实验_高级语言程序设计_实验11_链表操作_堂前习题

1099 [填空题]链表的合并

Description

下面程序创建两个链表,然后将第二个链表合并到第一个链表未尾,但合并部分的代码未完成,请你完成这部分代码。

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

struct student
{
     long num;
     int score;
     struct student *next;
};

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

struct student *merge(struct student *head, struct student *head2)
{ 
_______________________
}


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

main()
{
    struct student *head, *head2;
    int n;
    long del_num;
    scanf("%d",&n); 
    head=create(n);
    print(head);
    scanf("%d",&n); 
    head2=create(n);
    print(head2);
    head = merge(head, head2);    
    print(head);
}
输入样例

2 (the 1st linked list, 2 students)
1 (code of no.1 studentof the 1st linked list)
98 (score of no.1 student of the 1st linked list)
7 (code of no.2 student of the 1st linked list)
99 (score of no.2 student of the 1st linked list)
1 (the 2nd linked list, 1 student)
5 (code of no.1 student of the 2nd linked list)
87 (score of no.1 student of the 2nd linked list)

输出样例
   1      98
   7      99
   5      87
   1      98
   7      99
   5      87
代码实现
struct student *merge(struct student *head, struct student *head2)
{
    struct student *p1=head;
    while(p1->next!=NULL)p1=p1->next;
    p1->next=head2;
    return head;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值