学生表学生信息包含n个数据按照学号从小到大排序结构体
时间: 2024-02-15 12:01:52 浏览: 35
以下是一个示例代码,包含一个学生信息的结构体和排序函数:
```c++
#include <iostream>
#include <algorithm>
using namespace std;
struct Student{
int id; // 学号
string name; // 姓名
int age; // 年龄
char gender; // 性别
};
bool cmp(Student a, Student b){ // 按照学号从小到大排序
return a.id < b.id;
}
int main(){
int n;
cin >> n; // 输入学生数量
Student stu[n];
for(int i=0; i<n; i++){
cin >> stu[i].id >> stu[i].name >> stu[i].age >> stu[i].gender; // 输入学生信息
}
sort(stu, stu+n, cmp); // 排序
for(int i=0; i<n; i++){
cout << stu[i].id << " " << stu[i].name << " " << stu[i].age << " " << stu[i].gender << endl; // 输出排序后的学生信息
}
return 0;
}
```
这里使用的是sort函数进行排序,其中cmp函数定义了排序规则。
相关问题
建立一个依学生的学号从小到大有序排列的链表。链表的结点中包括学号、姓名和年龄,最后输出该链表。
以下是一个 C++ 的链表实现,可以实现按照学号从小到大有序排列的功能。
```cpp
#include <iostream>
#include <string>
using namespace std;
// 学生结构体
struct Student {
int id;
string name;
int age;
Student *next;
};
// 插入学生节点,按照学号从小到大排序
Student* insert(Student *head, int id, string name, int age) {
Student *cur = head;
Student *pre = NULL;
// 找到第一个学号大于新学生学号的节点
while (cur != NULL && cur->id < id) {
pre = cur;
cur = cur->next;
}
// 插入新学生节点
Student *new_student = new Student;
new_student->id = id;
new_student->name = name;
new_student->age = age;
new_student->next = cur;
if (pre == NULL) {
head = new_student;
} else {
pre->next = new_student;
}
return head;
}
// 输出链表
void print(Student *head) {
Student *cur = head;
while (cur != NULL) {
cout << "学号:" << cur->id << ",姓名:" << cur->name << ",年龄:" << cur->age << endl;
cur = cur->next;
}
}
int main() {
// 初始化链表头
Student *head = NULL;
// 插入学生节点,按照学号从小到大排序
head = insert(head, 10001, "张三", 20);
head = insert(head, 10003, "李四", 22);
head = insert(head, 10002, "王五", 21);
// 输出链表
print(head);
return 0;
}
```
输出结果:
```
学号:10001,姓名:张三,年龄:20
学号:10002,姓名:王五,年龄:21
学号:10003,姓名:李四,年龄:22
```
建立一个5名学生信息的单向链表,每个结点包括学号、姓名、性别、年龄,采用插入排序方式,按学号对其从小到大排列
以下是一个简单的实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义链表节点结构体
typedef struct student {
int id;
char name[20];
char sex[10];
int age;
struct student* next;
} Student;
// 定义链表头指针
Student* head = NULL;
// 插入节点函数(按学号从小到大排序)
void insertNode(int id, char name[], char sex[], int age) {
Student* newNode = (Student*)malloc(sizeof(Student));
newNode->id = id;
strcpy(newNode->name, name);
strcpy(newNode->sex, sex);
newNode->age = age;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
} else {
if (head->id > id) {
newNode->next = head;
head = newNode;
} else {
Student* current = head;
while (current->next != NULL && current->next->id < id) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
}
// 输出链表函数
void printList() {
Student* current = head;
while (current != NULL) {
printf("id: %d, name: %s, sex: %s, age: %d\n", current->id, current->name, current->sex, current->age);
current = current->next;
}
}
int main() {
// 插入节点并排序
insertNode(2, "Tom", "male", 20);
insertNode(1, "Jerry", "female", 21);
insertNode(4, "Lucy", "female", 19);
insertNode(3, "Bob", "male", 22);
insertNode(5, "Alice", "female", 18);
// 输出链表
printList();
return 0;
}
```
这个程序会创建一个包含5名学生信息的单向链表,每个节点包括学号、姓名、性别和年龄。它会采用插入排序的方式,按学号从小到大排列。最后,它会输出整个链表的内容。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)