有10个学生,学生学籍库包括:学号、姓名、性别、出生年月、第二学期大学英语和数据结构的成绩。选用合适的数据结构抽象学生学籍库,本次实训要求使用链表结构,同时要求选用合适的存储方式,即把学籍库抽象成一个以学号作为主关键字的动态查找表,并用链表表示和储存它,在自己构建的链表上实现学生信息查询算法。编写程序,使之能提供以下功能。 1)系统菜单(即先输出提示信息序号1、2、3…,分别实现什么功能,输入序号后,调用相应的功能函数)。 2)学生信息录入功能。 3)学生信息删除功能。 4)显示学生信息。 5)学生信息查找。 6)依据总分成绩对学生信息进行排序。 要求程序设计一个.c或.cpp源文件,系统菜单和函数调用(设计一个源文件,其中有main函数)。
时间: 2024-02-18 13:01:36 浏览: 65
很好的问题,我可以回答。首先,我们可以使用结构体来表示学生的信息,如下所示:
```c++
struct Student {
int id;
string name;
string gender;
string birthDate;
double englishScore;
double dataStructureScore;
double totalScore;
};
```
然后,我们可以使用链表来存储这些学生信息,每个节点包含一个学生信息和指向下一个节点的指针,如下所示:
```c++
struct Node {
Student data;
Node* next;
};
```
我们可以将链表的头指针作为学生信息的主关键字,因为每个学生的学号是唯一的。
接下来,我们可以设计一个函数来创建一个新的节点,并将其插入链表中,实现学生信息录入功能:
```c++
void insert(Node* &head, Student student) {
Node* newNode = new Node;
newNode->data = student;
newNode->next = head;
head = newNode;
}
```
我们还可以设计一个函数来删除链表中的一个节点,实现学生信息删除功能:
```c++
void remove(Node* &head, int id) {
Node* prev = nullptr;
Node* curr = head;
while (curr != nullptr && curr->data.id != id) {
prev = curr;
curr = curr->next;
}
if (curr == nullptr) {
cout << "Student with id " << id << " not found" << endl;
} else {
if (prev == nullptr) {
head = curr->next;
} else {
prev->next = curr->next;
}
delete curr;
}
}
```
我们还可以设计一个函数来遍历链表,并输出每个节点的学生信息,实现显示学生信息功能:
```c++
void display(Node* head) {
Node* curr = head;
while (curr != nullptr) {
cout << "ID: " << curr->data.id << endl;
cout << "Name: " << curr->data.name << endl;
cout << "Gender: " << curr->data.gender << endl;
cout << "Birth Date: " << curr->data.birthDate << endl;
cout << "English Score: " << curr->data.englishScore << endl;
cout << "Data Structure Score: " << curr->data.dataStructureScore << endl;
cout << "Total Score: " << curr->data.totalScore << endl;
cout << endl;
curr = curr->next;
}
}
```
我们还可以设计一个函数来查找链表中的一个节点,并输出其学生信息,实现学生信息查找功能:
```c++
void search(Node* head, int id) {
Node* curr = head;
while (curr != nullptr && curr->data.id != id) {
curr = curr->next;
}
if (curr == nullptr) {
cout << "Student with id " << id << " not found" << endl;
} else {
cout << "ID: " << curr->data.id << endl;
cout << "Name: " << curr->data.name << endl;
cout << "Gender: " << curr->data.gender << endl;
cout << "Birth Date: " << curr->data.birthDate << endl;
cout << "English Score: " << curr->data.englishScore << endl;
cout << "Data Structure Score: " << curr->data.dataStructureScore << endl;
cout << "Total Score: " << curr->data.totalScore << endl;
cout << endl;
}
}
```
最后,我们可以设计一个函数来对链表中的学生信息按照总分成绩进行排序,实现依据总分成绩对学生信息进行排序功能:
```c++
void sort(Node* &head) {
Node* curr = head;
Node* prev = nullptr;
while (curr != nullptr) {
curr->data.totalScore = curr->data.englishScore + curr->data.dataStructureScore;
prev = curr;
curr = curr->next;
}
bool sorted = false;
while (!sorted) {
sorted = true;
curr = head;
prev = nullptr;
while (curr != nullptr && curr->next != nullptr) {
if (curr->data.totalScore < curr->next->data.totalScore) {
Node* temp = curr->next;
curr->next = temp->next;
temp->next = curr;
if (prev == nullptr) {
head = temp;
} else {
prev->next = temp;
}
prev = temp;
sorted = false;
} else {
prev = curr;
curr = curr->next;
}
}
}
}
```
最后,我们可以设计一个菜单函数,让用户能够选择要执行哪个功能:
```c++
void menu(Node* &head) {
int choice;
do {
cout << "1. Insert student information" << endl;
cout << "2. Remove student information" << endl;
cout << "3. Display student information" << endl;
cout << "4. Search for a student" << endl;
cout << "5. Sort student information by total score" << endl;
cout << "6. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1: {
Student student;
cout << "Enter student ID: ";
cin >> student.id;
cout << "Enter student name: ";
cin >> student.name;
cout << "Enter student gender: ";
cin >> student.gender;
cout << "Enter student birth date: ";
cin >> student.birthDate;
cout << "Enter student English score: ";
cin >> student.englishScore;
cout << "Enter student data structure score: ";
cin >> student.dataStructureScore;
student.totalScore = student.englishScore + student.dataStructureScore;
insert(head, student);
break;
}
case 2: {
int id;
cout << "Enter student ID to remove: ";
cin >> id;
remove(head, id);
break;
}
case 3: {
display(head);
break;
}
case 4: {
int id;
cout << "Enter student ID to search for: ";
cin >> id;
search(head, id);
break;
}
case 5: {
sort(head);
break;
}
case 6: {
cout << "Exiting program" << endl;
break;
}
default: {
cout << "Invalid choice" << endl;
break;
}
}
} while (choice != 6);
}
```
最后,我们可以在main函数中调用菜单函数,完成程序的编写:
```c++
int main() {
Node* head = nullptr;
menu(head);
return 0;
}
```
阅读全文