#include <iostream>
#include <cstring>
#include <iomanip>//设置域宽头文件
using namespace std;
typedef struct Link *List;
typedef struct Link {
string name;
string sex;
int age;
int num;
double score[3];
int total;
int size;
struct Link *next;
} Link;
List creat() { //创建链表
int n;
List head, p;
head = p = new Link;
cout << "输入班级总人数" << endl;
cin >> n;
cout << "请输入" << n << "位学生的数据" << endl;
head->size = n;
while (n--) {
p->next = new Link;
p = p->next;
p->total = 0;
cin >> p->name >> p->num >> p->age >> p->sex;
cout << "语文" << endl;
cin >> p->score[0];
cout << "数学" << endl;
cin >> p->score[1];
cout << "英语" << endl;
cin >> p->score[2];
for (int i = 0; i < 3; i++)
p->total += p->score[i];
}
p->next = NULL;
return head;
}
void output(List head) { //输出
List x = head->next;
if (x == NULL) {
cout << "此表为空表" << endl;
return ;
}
cout << "班级学生数据如下" << endl;
cout << setw(10) << "学号" << setw(10) << "姓名" << setw(10) << "性别" << setw(10) << "年龄" << setw(
10) << "语文" << setw(10) << "数学" << setw(10) << "英语" << setw(10) << "总分" << endl;
while (x != NULL) {
cout << setw(10) << x->num << setw(10) << x->name << setw(10) << x->sex << setw(10) << x->age << setw(
10) << x->score[0] << setw(10) << x->score[1]
<< setw(10) << x->score[2] << setw(10) << x->total << endl;
x = x->next;
}
}
void deleteList(List head) { //删除整个链表
List x = head;
while (x) {
head = x->next;
delete x;
x = head;
}
}
List find(List head, int i) { //查找
List x = head;
int j = 0;
if (i < 0 || i > x->size) {
cout << "输入错误位置信息" << endl;
return NULL;
}
while (j < i && x) {
x = x->next;
j++;
}
return x;
}
void insert(List head, Link p, int i) { //插入
List x, y;
x = find(head, i - 1);
if (x == NULL)
return ;
else {
y = new Link;
*y = p;
y->next = x->next;
x->next = y;
head->size++;
}
}
void delete_student(List head, int i) { //删除某个同学数据
List p, q;
if (i < 0 || i > head->size) {
cout << "删除位置错误" << endl;
return ;
}
p = find(head, i - 1);
q = p->next;
p->next = q->next;
head->size--;
delete q;
}
int main() {
List head, p;
Link x;
head = creat(); //创建
output(head);//输出
int n;
cout << "输入你要查找的学生位置" << endl; //查找
cin >> n;
p = find(head, n);
if (p) {
cout << "找到第" << n << "名学生信息是" << endl;
cout << setw(10) << p->num << setw(10) << p->name << setw(10) << p->sex << setw(10) << p->age << setw(
10) << p->score[0] << setw(10) << p->score[1]
<< setw(10) << p->score[2] << setw(10) << p->total << endl;
}
cout << "请输入你要添加的学生位置" << endl; //插入
cin >> n;
cout << "请输入你要添加的学生信息" << endl;
cin >> x.name >> x.num >> x.age >> x.sex;
cout << "语文" << endl;
cin >> x.score[0];
cout << "数学" << endl;
cin >> x.score[1];
cout << "英语" << endl;
cin >> x.score[2];
for (int i = 0; i < 3; i++)
x.total += x.score[i];
insert(head, x, n);
cout << "插入后的信息为:" << endl;
output(head);
cout << "请输入要删除学生信息的节点位置:" << endl;
cin >> n;
delete_student(head, n);
output(head);
return 0;
}