当前位置:网站首页>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;
}
边栏推荐
- 英语句式参考
- Sibling components carry out value transfer (there is a sequence displayed)
- sample_rate(采樣率),sample(采樣),duration(時長)是什麼關系
- 开户注册股票炒股安全吗?有没有风险的?靠谱吗?
- MATLAB中print函数使用
- ConvMAE(2022-05)
- Numerical calculation method chapter8 Numerical solutions of ordinary differential equations
- Huaxia Fund: sharing of practical achievements of digital transformation in the fund industry
- Copy the linked list with random pointer in the "Li Kou brush question plan"
- Memory management chapter of Kobayashi coding
猜你喜欢

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

瞅一瞅JUC提供的限流工具Semaphore

FCN: Fully Convolutional Networks for Semantic Segmentation

About Estimation with Cross-Validation

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

node_ Exporter memory usage is not displayed

使用Jmeter虚拟化table失败

@Extension、@SPI注解原理

pytorch yolov5 训练自定义数据

LeetCode 6109. Number of people who know the secret
随机推荐
Huaxia Fund: sharing of practical achievements of digital transformation in the fund industry
Is it safe for Apple mobile phone to speculate in stocks? Is it a fraud to get new debts?
Electron installation problems
Fix vulnerability - mysql, ES
爱因斯坦求和einsum
JDBC reads a large amount of data, resulting in memory overflow
JVM third talk -- JVM performance tuning practice and high-frequency interview question record
Cronab log: how to record the output of my cron script
Generate XML schema from class
第十一届中国云计算标准和应用大会 | 云计算国家标准及白皮书系列发布 华云数据全面参与编制
彻底理解为什么网络 I/O 会被阻塞?
[paddlepaddle] paddedetection face recognition custom data set
sample_rate(采样率),sample(采样),duration(时长)是什么关系
Gimp 2.10 tutorial "suggestions collection"
About Estimation with Cross-Validation
Sibling components carry out value transfer (there is a sequence displayed)
Privacy computing helps secure data circulation and sharing
瀚升优品app翰林优商系统开发功能介绍
写作写作写作写作
音视频包的pts,dts,duration的由来.