问题描述】建立一个10结点的单向链表,每个结点包括编号、姓名、年龄、性别。采用插入排序法对其进行排序,按学号从小到大排序。
【输入形式】10行数据,每行数据4个元素,用空格隔开,分别为姓名,编号,性别,年龄。
【输出形式】22行数据,前11行为排序前结果,后11行为排序后结果。每11行中第一行为输出提示语句,后面10行每行包含四个数据,分别为编号,年龄,姓名,性别。具体输出形式见样例输出
【样例输入】
AAA 8002101 male 15
BBB 8002105 male 16
CCC 8002106 male 17
DDD 8002107 male 18
EEE 8002108 male 19
FFF 8002109 male 20
GGG 8002100 male 21
HHH 8002103 male 22
III 8002102 male 23
JJJ 8002104 male 24【样例输出】
Before ordering(id|age|name|sex)
(8002101|15|AAA|male)
(8002105|16|BBB|male)
(8002106|17|CCC|male)
(8002107|18|DDD|male)
(8002108|19|EEE|male)
(8002109|20|FFF|male)
(8002100|21|GGG|male)
(8002103|22|HHH|male)
(8002102|23|III|male)
(8002104|24|JJJ|male)
After ordering(id|age|name|sex)
(8002100|21|GGG|male)
(8002101|15|AAA|male)
(8002102|23|III|male)
(8002103|22|HHH|male)
(8002104|24|JJJ|male)
(8002105|16|BBB|male)
(8002106|17|CCC|male)
(8002107|18|DDD|male)
(8002108|19|EEE|male)
(8002109|20|FFF|male)
https://www.cnblogs.com/yuanshuang/p/5479242.html
sort+cmp他不香吗
#include<iostream>
using namespace std;
int main() {
//建立一个10名学生信息的单向链表,每个结点包括学号、姓名、性别、年龄,采用插入排序按学号对其从小到大排列。
struct student
{
int sno;
char name[20];
char sex[3];
int age;
struct student* next;
}stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10;
stu1 = { 5 ,"s05" ,"男" ,18 };
stu2 = { 3 ,"s03","男", 18 };
stu3 = { 2 ,"s02", "男", 18 };
stu4 = { 4,"s04","男", 18 };
stu5 = { 6,"s06", "男", 18 };
stu6 = { 1 ,"s01" ,"男", 18 };
stu7 = { 8 ,"s08" ,"男", 19 };
stu8 = { 9, "s09", "男", 18 };
stu9 = { 10, "s10", "男", 18 };
stu10 = { 7 ,"s07" ,"男", 18 };
struct student* s[10] = { &stu1,&stu2,&stu3,&stu4,&stu5,&stu6,&stu7,&stu8,&stu9,&stu10 };
struct student* head = s[0];
struct student* stu;
struct student* sture = NULL;
head->next = NULL;
for (int i = 1; i < 10; i++) {
stu = head;
while (s[i]->sno > stu->sno && stu->next != NULL) {
sture = stu;
stu = stu->next;
}
if (s[i]->sno <= stu->sno) {
if (stu == head) {
head = s[i];
head->next = stu;
}
else {
sture->next = s[i];
s[i]->next = stu;
}
}
else
{
stu->next = s[i];
s[i]->next = NULL;
}
}
stu = head;
cout << "*****************排序结果*****************" << endl;
cout << "学号" << "\t" << "姓名" << "\t" << "性别" << "\t" << "年龄" << "\t" << endl;
for (int n = 0; n < 10; n++) {
cout << stu->sno<<"\t" << stu->name << "\t" << stu->sex << "\t" << stu->age << "\t" << endl;
stu = stu->next;
}
return 0;
}