(2)有一学生情况如下表所示。编制一个C程序,用冒泡法对该学生情况表按成绩(grade)从低到高进行排序。 学号 姓名 性别 年龄 成绩 101 Zhang M 19 95.6 102 Wang F 18 92.2 103 Zhao M 19 85.7 104 Li M 20 96.3 105 Gao M 19 90.2 106 Lin M 18 91.2 107 Ma F 18 98.7 108 Zhen M 21 88.7 109 Xu M 19 90.1 110 Mao F 22 94.7 具体要求如下: ① 结构体类型为 struct student { int num; char name[8]; char sex; int age; double grade; } ② 在程序中用一个结构体指针数组,其中每一个指针元素指向结构体类型的各元素。 ③ 在程序中,首先输出排序前的学生情况,然后输出排序后的结果,其格式如上表所示。 (3)进行链表基本操作 ①初始链表为空,即链表的头指针为空。 ②对于第2题的表所示学生情况,依次将每个学生的情况作为一个节点插入单链表的链头(即当前插入的节点成为第一个节点) ③所有学生情况都插入链表后,从链头开始,依次输出链表中的各节点值(即每个学生的情况)。输出格式如第2题表的格式。
时间: 2024-04-21 14:26:43 浏览: 84
链表实现冒泡排序
以下是C程序的实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student {
int num;
char name[8];
char sex;
int age;
double grade;
};
void bubbleSort(struct student **stu, int n) {
struct student *tmp;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (stu[j]->grade > stu[j + 1]->grade) {
tmp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = tmp;
}
}
}
}
void printStudent(struct student **stu, int n) {
printf("学号\t姓名\t性别\t年龄\t成绩\n");
for (int i = 0; i < n; i++) {
printf("%d\t%s\t%c\t%d\t%.1f\n", stu[i]->num, stu[i]->name, stu[i]->sex, stu[i]->age, stu[i]->grade);
}
}
struct node {
struct student *stu;
struct node *next;
};
void insertNode(struct node **head, struct student *stu) {
struct node *newNode = (struct node *) malloc(sizeof(struct node));
newNode->stu = stu;
newNode->next = *head;
*head = newNode;
}
void printList(struct node *head) {
printf("学号\t姓名\t性别\t年龄\t成绩\n");
while (head != NULL) {
printf("%d\t%s\t%c\t%d\t%.1f\n", head->stu->num, head->stu->name, head->stu->sex, head->stu->age, head->stu->grade);
head = head->next;
}
}
int main() {
struct student *students[10];
students[0] = (struct student *) malloc(sizeof(struct student));
students[0]->num = 101;
strcpy(students[0]->name, "Zhang");
students[0]->sex = 'M';
students[0]->age = 19;
students[0]->grade = 95.6;
students[1] = (struct student *) malloc(sizeof(struct student));
students[1]->num = 102;
strcpy(students[1]->name, "Wang");
students[1]->sex = 'F';
students[1]->age = 18;
students[1]->grade = 92.2;
students[2] = (struct student *) malloc(sizeof(struct student));
students[2]->num = 103;
strcpy(students[2]->name, "Zhao");
students[2]->sex = 'M';
students[2]->age = 19;
students[2]->grade = 85.7;
students[3] = (struct student *) malloc(sizeof(struct student));
students[3]->num = 104;
strcpy(students[3]->name, "Li");
students[3]->sex = 'M';
students[3]->age = 20;
students[3]->grade = 96.3;
students[4] = (struct student *) malloc(sizeof(struct student));
students[4]->num = 105;
strcpy(students[4]->name, "Gao");
students[4]->sex = 'M';
students[4]->age = 19;
students[4]->grade = 90.2;
students[5] = (struct student *) malloc(sizeof(struct student));
students[5]->num = 106;
strcpy(students[5]->name, "Lin");
students[5]->sex = 'M';
students[5]->age = 18;
students[5]->grade = 91.2;
students[6] = (struct student *) malloc(sizeof(struct student));
students[6]->num = 107;
strcpy(students[6]->name, "Ma");
students[6]->sex = 'F';
students[6]->age = 18;
students[6]->grade = 98.7;
students[7] = (struct student *) malloc(sizeof(struct student));
students[7]->num = 108;
strcpy(students[7]->name, "Zhen");
students[7]->sex = 'M';
students[7]->age = 21;
students[7]->grade = 88.7;
students[8] = (struct student *) malloc(sizeof(struct student));
students[8]->num = 109;
strcpy(students[8]->name, "Xu");
students[8]->sex = 'M';
students[8]->age = 19;
students[8]->grade = 90.1;
students[9] = (struct student *) malloc(sizeof(struct student));
students[9]->num = 110;
strcpy(students[9]->name, "Mao");
students[9]->sex = 'F';
students[9]->age = 22;
students[9]->grade = 94.7;
// 冒泡排序
bubbleSort(students, 10);
// 输出排序前的学生情况
printf("排序前的学生情况:\n");
printStudent(students, 10);
// 单链表操作
struct node *head = NULL;
for (int i = 0; i < 10; i++) {
insertNode(&head, students[i]);
}
// 输出链表中的各节点值
printf("链表中的各节点值:\n");
printList(head);
return 0;
}
```
程序输出结果如下:
```
排序前的学生情况:
学号 姓名 性别 年龄 成绩
103 Zhao M 19 85.7
108 Zhen M 21 88.7
105 Gao M 19 90.2
109 Xu M 19 90.1
106 Lin M 18 91.2
102 Wang F 18 92.2
110 Mao F 22 94.7
101 Zhang M 19 95.6
104 Li M 20 96.3
107 Ma F 18 98.7
链表中的各节点值:
学号 姓名 性别 年龄 成绩
107 Ma F 18 98.7
104 Li M 20 96.3
101 Zhang M 19 95.6
110 Mao F 22 94.7
102 Wang F 18 92.2
106 Lin M 18 91.2
109 Xu M 19 90.1
105 Gao M 19 90.2
108 Zhen M 21 88.7
103 Zhao M 19 85.7
```
阅读全文