当前位置:   article > 正文

OJ 链表的合并_8580 合并链表 时间限制:1000ms 代码长度限制:10kb 提交次数:3724 通过次数:2

8580 合并链表 时间限制:1000ms 代码长度限制:10kb 提交次数:3724 通过次数:2077

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;//如果只建立一个链表 头指针指向p1
        else p2->next=p1;
        p2=p1;
    }
    return(head);
}

struct student *merge(struct student *head, struct student *head2)
{
   struct student *p1;
   p1=head;
   while (p1->next!=NULL)
    p1=p1->next;
   p1->next=head2;
   return (head);
   
   /* struct student *p1;//定义结构体指针变量p1
    p1=head;//p1指向head
    while(p1->next!=NULL)p1=p1->next;//使p1指向head链表的最后一个元素
    p1->next=head2;//p1的next指向head2头指针
    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",&n);
    head=create(n);
    print(head);
    scanf("%d",&n);
    head2=create(n);
    print(head2);
    head = merge(head, head2);
    print(head);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/761733
推荐阅读
相关标签
  

闽ICP备14008679号