1099 [填空题]链表合并

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

题型: 填空题   语言: GCC

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 *headstruct 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
#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",&amp;p1-&gt;num);
        scanf("%d",&amp;p1-&gt;score);
        p1-&gt;next=NULL;
        if(i==1) head=p1;
        else p2->next=p1;
        p2=p1;
      }
      return(head);
}

struct student *merge(struct student *head, struct student *head2)
{
    struct student *s,*p;
    s=head2;
    p=head;
    while(p->next!=NULL)//这里循环结构,不要混淆if和while,功能完全不一样,一个条件一个循环
    {
        p=p->next;
    }
    p->next=s;
    return(head);
}


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",&amp;n);
    head=create(n);
    print(head);
    scanf("%d",&amp;n);
    head2=create(n);
    print(head2);
    head = merge(head, head2);
    print(head);
}

 

原文地址:https://blog.csdn.net/zero_019/article/details/134806918

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_44674.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注