学生表抽象成一个队列,每个学生(包括姓名、学号、成绩)作为队列中的一个元素,该队列采用顺序存储结构,对该队列进行初始化、输出、入队和出队基本操作,
时间: 2024-05-29 10:12:40 浏览: 15
并实现对队列中成绩的排序操作。
1. 初始化队列
定义一个结构体作为队列中的元素,包括姓名、学号、成绩三个成员变量。同时定义一个数组,用于存储队列中的元素,以及定义队列头和队列尾指针,初始化为-1。
struct Student {
char name[20];
int id;
int score;
};
#define MAX_SIZE 100
Student queue[MAX_SIZE];
int front = -1;
int rear = -1;
2. 输出队列
遍历队列中的元素,输出其姓名、学号和成绩。
void printQueue() {
if (front == -1) {
printf("Queue is empty!\n");
return;
}
printf("Queue:\n");
for (int i = front; i <= rear; i++) {
printf("%s %d %d\n", queue[i].name, queue[i].id, queue[i].score);
}
}
3. 入队操作
将一个学生元素插入队列尾部,如果队列已满则提示“Queue is full!”。
void enqueue(Student student) {
if (rear == MAX_SIZE - 1) {
printf("Queue is full!\n");
return;
}
rear++;
queue[rear] = student;
if (front == -1) {
front = 0;
}
}
4. 出队操作
将队列头部元素出队,如果队列已空则提示“Queue is empty!”。
Student dequeue() {
if (front == -1) {
printf("Queue is empty!\n");
Student student = {"", 0, 0};
return student;
}
Student student = queue[front];
if (front == rear) {
front = -1;
rear = -1;
} else {
front++;
}
return student;
}
5. 排序操作
按照成绩从高到低排序队列中的元素。
void sortQueue() {
if (front == -1) {
printf("Queue is empty!\n");
return;
}
for (int i = front; i <= rear; i++) {
for (int j = i + 1; j <= rear; j++) {
if (queue[i].score < queue[j].score) {
Student temp = queue[i];
queue[i] = queue[j];
queue[j] = temp;
}
}
}
}
完整代码如下:
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.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)
![](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)