#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 人评分
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; }
typedef struct student { int id; int score; struct student *next; }student; 这么写 student变成student没啥用啊
void connect(student *head1,student *head2)//连接函数,将第二个链表的地址赋给第一个的结尾 { student *p; p=head1; while(p!=0) { p=p->next; } if(p==0) { p=head2; } } 31行的函数这么写为什么不对?麻烦问一下
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:521 |
C语言训练-排序问题<2> (C++代码)浏览:882 |
C语言程序设计教程(第三版)课后习题11.1 (C语言代码)浏览:679 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:677 |
WU-图形输出 (C++代码)浏览:802 |
WU-判定字符位置 (C++代码)浏览:1400 |
2004年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:502 |
C语言程序设计教程(第三版)课后习题9.4 (C语言代码)浏览:665 |
1642题解浏览:712 |
简单的a+b (C语言代码)浏览:415 |
uq_93509428540 2022-10-24 19:57:03 |
冒泡这样写的