定义一个包含学生信息(学号,姓名,成绩)的链队列,使其具有如下功能: (1) 初始化一个空队列; (2) 逐个输入学生信息并入队; (3) 出队,输出学号、姓名、成绩等信息; (4) 删除队列。 (5) 统计当前队列排队学生个数; (6) 请设计一个类似于买火车票的绿色通道功能,并简要叙述该功能对队列的影响。
时间: 2023-05-26 17:06:44 浏览: 201
以下是实现该链队列的代码:
```c++
#include <iostream>
using namespace std;
// 学生信息结构体
struct Student{
int stuNo;
string name;
double score;
Student(int no=0, string nam="", double sco=0.0){
stuNo = no;
name = nam;
score = sco;
}
};
// 队列节点结构体
struct Node{
Student data;
Node *next;
};
class LinkedQueue{
public:
LinkedQueue(){
front = rear = new Node;
front->next = NULL;
count = 0;
}
~LinkedQueue(){
clear();
delete front;
}
bool isEmpty(){
return front == rear;
}
void enqueue(Student stu){
rear->next = new Node;
rear = rear->next;
rear->data = stu;
rear->next = NULL;
count++;
}
Student dequeue(){
if(isEmpty()){
cerr<<"队列为空,无法出队!"<<endl;
exit(1);
}
Node *p = front->next;
front->next = p->next;
if(p->next == NULL){
rear = front;
}
Student stu = p->data;
delete p;
count--;
return stu;
}
void clear(){
while(!isEmpty()){
dequeue();
}
}
int size(){
return count;
}
void greenChannel(){
if(isEmpty()){
cerr<<"队列为空,无法使用绿色通道!"<<endl;
return;
}
Node *p = front->next;
Student maxScoreStu = p->data;
while(p != NULL){
if(p->data.score > maxScoreStu.score){
maxScoreStu = p->data;
}
p = p->next;
}
cout<<"绿色通道处理学生为:"<<maxScoreStu.name<<endl;
p = front->next;
if(p->data.stuNo == maxScoreStu.stuNo){
// 删除队头节点
dequeue();
}
else{
// 找到前驱节点,删除
Node *prev = front;
while(prev->next->data.stuNo != maxScoreStu.stuNo){
prev = prev->next;
}
Node *del = prev->next;
prev->next = del->next;
if(del->next == NULL){
// 删除队尾节点
rear = prev;
}
delete del;
count--;
}
}
void display(){
if(isEmpty()){
cout<<"队列为空!"<<endl;
return;
}
cout<<"当前队列共有"<<count<<"个学生:"<<endl;
Node *p = front->next;
while(p != NULL){
cout<<p->data.stuNo<<"\t"<<p->data.name<<"\t"<<p->data.score<<endl;
p = p->next;
}
}
private:
Node *front, *rear;
int count;
};
int main(){
LinkedQueue q;
cout<<"初始化队列完成!"<<endl;
int op = 0;
while(op != -1){
cout<<"请选择操作:"<<endl;
cout<<"1. 入队"<<endl;
cout<<"2. 出队"<<endl;
cout<<"3. 显示队列信息"<<endl;
cout<<"4. 统计队列大小"<<endl;
cout<<"5. 绿色通道"<<endl;
cout<<"6. 删除队列"<<endl;
cout<<"-1. 退出"<<endl;
cin>>op;
switch(op){
case 1:{
int no;
string name;
double score;
cout<<"请输入学号、姓名、成绩:"<<endl;
cin>>no>>name>>score;
Student stu(no, name, score);
q.enqueue(stu);
cout<<"已成功入队!"<<endl;
break;
}
case 2:{
Student stu = q.dequeue();
cout<<"已成功出队!出队的学生信息为:"<<endl;
cout<<stu.stuNo<<"\t"<<stu.name<<"\t"<<stu.score<<endl;
break;
}
case 3:{
q.display();
break;
}
case 4:{
cout<<"当前队列大小为:"<<q.size()<<endl;
break;
}
case 5:{
q.greenChannel();
break;
}
case 6:{
q.clear();
cout<<"已成功删除队列!"<<endl;
break;
}
case -1: break;
default:{
cout<<"输入的操作不存在!"<<endl;
break;
}
}
cout<<endl;
}
return 0;
}
```
绿色通道功能的设计如下:
该功能可以优先处理当前队列中成绩最高的学生,即可以让成绩优异的学生优先进入下一个环节或者更快地办理业务。在实现上,我们需要遍历整个队列,记录下成绩最高的学生信息,并删除该学生所在队列位置的节点。如果成绩最高的学生在队头,则可以直接出队;否则需要找到该学生的前驱节点并修改其指针。
该功能主要的影响在于对队列的修改。由于优先处理特定学生,因此可能会引起其他学生的等待时间变长,或者改变其他学生的位置。由于队列是一种先进先出的数据结构,因此删除某个位置的节点会导致这个节点之后的所有节点需要前移一个位置,而删除队头节点则不需要前移操作。因此,绿色通道功能可能会改变队列中学生的相对位置,但不会改变它们在整个队列中的顺序。在设计时应该考虑这点,尽量减少对队列的扰动。
阅读全文
相关推荐



















