现有A、B两个班级的学生,学生的信息包括学号、姓名、成绩,将这两个班级的学生按照学号顺序分别保存在两个单链表中(学号从1开始编号)。 期末时经过转专业考试后,A班从i号开始有len个学生转专业考试通过,转到B班级,转到B班级后,将这len个学生安排在B班的j号学生之前。 请编写程序,先输入A、B班级的学生信息,再输入i,len和j的值,最后输出转专业后,A、B班的学生信息(按照顺序输出)。
时间: 2024-02-15 18:02:01 浏览: 130
以下是一个C++的实现代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
struct Student {
int id;
string name;
int score;
Student* next;
};
Student* createList() {
Student* head = new Student();
head->next = NULL;
Student* p = head;
int id, score;
string name;
while (cin >> id >> name >> score) {
Student* node = new Student();
node->id = id;
node->name = name;
node->score = score;
node->next = NULL;
p->next = node;
p = node;
}
return head;
}
void printList(Student* head) {
Student* p = head->next;
while (p != NULL) {
cout << p->id << " " << p->name << " " << p->score << endl;
p = p->next;
}
}
void transfer(Student* A, Student* B, int i, int len, int j) {
Student* p = A;
for (int k = 0; k < i - 1; k++) {
p = p->next;
}
Student* q = p;
for (int k = 0; k < len; k++) {
q = q->next;
}
Student* r = B;
for (int k = 0; k < j - 1; k++) {
r = r->next;
}
p->next = q->next;
q->next = r->next;
r->next = q;
}
int main() {
cout << "请输入A班级的学生信息(学号、姓名、成绩):" << endl;
Student* A = createList();
cout << "请输入B班级的学生信息(学号、姓名、成绩):" << endl;
Student* B = createList();
int i, len, j;
cout << "请输入转专业的学生范围(i、len)和插入位置(j):" << endl;
cin >> i >> len >> j;
transfer(A, B, i, len, j);
cout << "转专业后,A班级的学生信息为:" << endl;
printList(A);
cout << "转专业后,B班级的学生信息为:" << endl;
printList(B);
return 0;
}
```
该程序中,我们先定义了一个双向链表结构体`Student`,包含学号、姓名、成绩和指向下一个节点的指针。然后通过`createList`函数分别创建A、B两个链表。接着,我们通过`transfer`函数将A班级中从第i个学生开始的len个学生转到B班级的第j个学生之前。最后,我们通过`printList`函数分别输出转专业后的A、B班级的学生信息。
阅读全文
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.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)