maxiao


私信TA

用户名:651158933

访问量:10104

签 名:

加油写代码

等  级
排  名 1350
经  验 2853
参赛次数 0
文章发表 16
年  龄 0
在职情况 学生
学  校 燕山大学
专  业

  自我简介:

想得到什么,就去追求什么。

#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
    int id;
    int score;
    struct student *next;
}student;
student *input(int n)//输入函数
{
    student *head,*p1,*p2;
    head=0;
    for(int i=0;i<n;i++)
    {
        p1=(student*)malloc(sizeof(student));
        scanf("%d%d",&(p1->id),&(p1->score));
        if(head==0)
        {
            head=p1;
            p2=p1;
        }
        else
        {
            p2->next=p1;
            p2=p1;
        }
    }
    p2->next=0;
    return head;
}
void connect(student *head1,student *head2)//连接函数,将第二个链表的地址赋给第一个的结尾
{
    student *p;
    p=head1;
    while(p->next!=0)
    {
        p=p->next;
    }
    if(p->next==0)
    {
        p->next=head2;
    }
}
void sort(student *head,int n)//排序函数
{
    student *p1,*p2;
    p1=head;
    p2=head;
    int temp;
    for(int i=0;i<n;i++)//利用冒泡法将两个学号比较,如果后面的学号比较小,将两个学号进行交换
    {
        for(int j=i+1;j<n;j++)
        {   
            p2=p2->next;
            if(p1->id>p2->id)
            {
                temp=p1->id;
                p1->id=p2->id;
                p2->id=temp;
                temp=p1->score;//注意成绩也需要交换
                p1->score=p2->score;
                p2->score=temp;
            }
        }
        p1=p1->next;
        p2=p1;
    }
}
void output(student *h)//输出函数
{   
    student *p;//用来释放结点
    while(h!=0)
    {
        printf("%d %d\n",h->id,h->score);
        p=h;
        h=h->next;
        free(p);//释放结点
    }
}
int main()
{
    student *head1,*head2;
    int m,n;
    scanf("%d %d",&m,&n);
    head1=input(m);
    head2=input(n);
    connect(head1,head2);
    sort(head1,m+n);
    output(head1);
    return 0;
}

参考:https://www.zhihu.com/question/53645056先学习了这个程序

 

0.0分

12 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

void sort(student *head,int n)//排序函数
{
    student *p1,*p2,*l;
    p1=head;
    p2=head;
    int temp;
  
    for(int i=0;i<n-1;i++)//利用冒泡法将两个学号比较,如果后面的学号比较小,将两个学号进行交换
    {
    	int flag=0;
        for(int j=0;j<n-i-1;j++)
        {   
            p2=p2->next;
            if(p1->id>p2->id)
            {
                temp=p1->id;
                p1->id=p2->id;
                p2->id=temp;
                temp=p1->score;//注意成绩也需要交换
                p1->score=p2->score;
                p2->score=temp;
                flag=1;
            } 
            p1=p1->next;
           
        }
2022-10-24 19:56:38
typedef struct student
{
    int id;
    int score;
    struct student *next;
}student;
这么写 student变成student没啥用啊
2021-12-22 15:48:08
void connect(student *head1,student *head2)//连接函数,将第二个链表的地址赋给第一个的结尾
{
        student *p;
        p=head1;
        while(p!=0)
        {
                p=p->next;
        }
        if(p==0)
        {
                p=head2;
        }
}
31行的函数这么写为什么不对?麻烦问一下
2018-08-05 14:38:20
新手,有什么不足的请大神指出。
2018-05-22 11:17:00
  • «
  • 1
  • »