当前位置:网站首页>7-2 保持链表有序
7-2 保持链表有序
2022-07-05 18:22:00 【mxrone】
对于输入的若干学生的信息,按学号顺序从小到大建立有序链表,最后遍历链表,并按顺序输出学生信息。
输入格式:
首先输入一个正整数T,表示测试数据的组数,然后是T组测试数据。每组测试数据首先输入一个正整数n(1≤n≤100),表示学生的个数。然后输入n行信息,分别是学生的学号和姓名,其中,学号是8位的正整数(保证各不相同),姓名是长度不超过10且不含空格的字符串。
输出格式:
对于每组测试,按顺序输出学生信息,学号和姓名之间留一个空格(参看输出样例)。
输入样哩:
1
3
20080108 Zhangsan
20070328 Lisi
20070333 Wangwu
输出样例:
20070328 Lisi
20070333 Wangwu
20080108 Zhangsan#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct stu{
int num;
char name[100];
struct stu *next;
};
int main(){
int n=0,m,i=0,j=0;
struct stu *head=NULL,*p,*tail,*q,*index,temp;
scanf("%d",&n);
while(i<n){
scanf("%d",&m);
for(j=0;j<m;j++){
p=(struct stu *)malloc(sizeof(struct stu ));
scanf("%d %s\n",&p->num,p->name);
if(head==NULL) head=tail=p;
else{
tail->next=p;
}
tail=p;
}
tail->next=NULL;
i++;
}
for(p=head;p!=NULL;p=p->next){
for(q=p->next;q!=NULL;q=q->next){
if(q->num<p->num){
temp.num=p->num;
strcpy(temp.name,p->name);
p->num=q->num;
strcpy(p->name,q->name);
q->num=temp.num;
strcpy(q->name,temp.name);
}
}
}
for(p=head;p!=NULL;p=p->next){
printf("%d %s\n",p->num,p->name);
}
return 0;
}
边栏推荐
- Introduction to VC programming on "suggestions collection"
- Nacos distributed transactions Seata * * install JDK on Linux, mysql5.7 start Nacos configure ideal call interface coordination (nanny level detail tutorial)
- Logical words in Articles
- Insufficient picture data? I made a free image enhancement software
- FCN: Fully Convolutional Networks for Semantic Segmentation
- Access the database and use redis as the cache of MySQL (a combination of redis and MySQL)
- About statistical power
- How can cluster deployment solve the needs of massive video access and large concurrency?
- Clickhouse (03) how to install and deploy Clickhouse
- Electron installation problems
猜你喜欢

Introduction to the development function of Hanlin Youshang system of Hansheng Youpin app

Vulnhub's darkhole_ two
![含重复元素取不重复子集[如何取子集?如何去重?]](/img/b2/d019c3f0b85a6c0d334a092fa6c23c.png)
含重复元素取不重复子集[如何取子集?如何去重?]

模拟百囚徒问题

Use JMeter to record scripts and debug

瀚升优品app翰林优商系统开发功能介绍

LeetCode 6111. 螺旋矩阵 IV

Insufficient picture data? I made a free image enhancement software

案例分享|金融业数据运营运维一体化建设

Le cours d'apprentissage de la machine 2022 de l'équipe Wunda arrive.
随机推荐
websocket 工具的使用
Gimp 2.10 tutorial "suggestions collection"
buuctf-pwn write-ups (9)
彻底理解为什么网络 I/O 会被阻塞?
Fix vulnerability - mysql, ES
写作写作写作写作
英语句式参考
第十一届中国云计算标准和应用大会 | 云计算国家标准及白皮书系列发布 华云数据全面参与编制
生词生词生词生词[2]
Thoroughly understand why network i/o is blocked?
[use electron to develop desktop on youqilin]
怎么自动安装pythn三方库
LeetCode 6111. 螺旋矩阵 IV
Sophon kg upgrade 3.1: break down barriers between data and liberate enterprise productivity
Generate classes from XML schema
Find the first k small element select_ k
图像分类,看我就够啦!
LeetCode 6109. Number of people who know the secret
Use JMeter to record scripts and debug
[PM2 details]