赞
踩
(实验)自定义数据元素的类型和存储结构(顺序表或链表均可),完成如下的功能:
①录入:从键盘输入学生信息表的各个数据元素(至少包含学号,姓名,年龄,语文成绩,数学成绩,英语成绩);
②查找:可按学号查找后输出学生的完整信息。
③插入:在表中第i个元素前插入新的学生信息;
④删除:按照学号删除指定学生信息。
⑤输出:输出所有学生信息;
#include <iostream> using namespace std; //学生信息结构体 struct Student { int id; //序列 long studentid; //学号 string name; //姓名 int age; //年龄 int Score[3]; //成绩 }; //单链表的存储结构 typedef struct LNode { Student data; //节点的数据项 struct LNode* next; //节点的指针域 }LNode,*LinkList; //LinkList为指向结构体LNode的指针类型 //单链表的初始化 LinkList InitList(LinkList &L) { L = new LNode; //生成头结点 L->next = NULL; //头结点的指针域置为空 return L; } //单链表的创建 int CreatList(LinkList& L, int n) { L->next = NULL; int a = 0; char s = 'y'; while (s != 'n') { LNode* p = new LNode; p->data.id = ++n; a = n; cout << "序列:" << n<< endl; cout << "学号:" << endl; cin >> p->data.studentid; cout << "姓名:" << endl; cin >> p->data.name; cout << "年龄:" << endl; cin >> p->data.age; cout << "语文:" << endl; cin >> p->data.Score[0]; cout << "数学:" << endl; cin >> p->data.Score[1]; cout << "英语:" << endl; cin >> p->data.Score[2]; p->next = L->next; L->next = p; cout << "是否要继续添加 y或n"<<endl; cin >> s; } return a; } //取链表中的所有值 LNode* GetStudent(LinkList L, int i) { LinkList p = L; int j = 0; while (p&&j<=i) { p = p->next; ++j; } return p; } //根据学号查找 LNode* queryById(LinkList L,long studentid) { LinkList p = L->next; while (p&&p->data.studentid!=studentid) { p = p->next; } if (!p) { cout << "查无此人!"; p = NULL; } return p; } //插入新的学生信息 bool addStudent(LinkList& L, int i, Student e) { LinkList p = L; int j = 0; while (p&&(j<i-1)) { p = p->next; ++j; } if (!p||i<=0) { cout << "插入失败!"; return false; } LNode* s = new LNode; s->data = e; s->next = p->next; p->next = s; return true; } //删除第i个学生信息 bool DeleteStudent(LinkList& L, long deleteId) { LNode* p = GetStudent(L, queryById(L, deleteId)->data.id -1); LNode* q = new LNode; q = p->next; p->next = q->next; delete q; return true; } int main() { //新建 LinkList L; L = InitList(L); int num = 0; //cout << "请输入要录入的学生人数:"<< endl; //cin >> num; num = CreatList(L, num); cout<< num; //输出 cout << "输出所有学生" << endl; for (int i = 0; i < num; i++) { Student q = GetStudent(L, i)->data; cout << "学号:" << q.studentid << endl; cout << "年龄:" << q.age << endl; cout << "姓名:" << q.name << endl; cout << "语文:" << q.Score[0] << endl; cout << "数学:" << q.Score[1] << endl; cout << "英语:" << q.Score[2] << endl; } //查找 cout << "按学号查找学生的完整信息" << endl; cout << "请输入学号" << endl; int studentid; cin >> studentid; Student s = queryById(L, studentid)->data; cout << "学号:"<< s.studentid << endl; cout << "年龄:"<< s.age << endl; cout << "姓名:"<< s.name << endl; cout << "语文:"<< s.Score[0] << endl; cout << "数学:"<< s.Score[1] << endl; cout << "英语:"<< s.Score[2] << endl; //插入 cout << "在表中第i个元素前插入新的学生信息" << endl; cout << "请输入要插入的学生信息" << endl; Student s1; s1.id = ++num; cout << "序列:" << s1.id << endl; cout << "学号:" << endl; cin >> s1.studentid; cout << "姓名:" << endl; cin >> s1.name; cout << "年龄:" << endl; cin >> s1.age; cout << "语文:" << endl; cin >> s1.Score[0]; cout << "数学:" << endl; cin >> s1.Score[1]; cout << "英语:" << endl; cin >> s1.Score[2]; bool add = false; int n; while (add == false) { cout << "请输入要在第几个插入" << endl; cin >> n; add = addStudent(L, n, s1); if (add == false) { cout << "插入失败!请重新在表中第i个元素前插入新的学生信息" << endl; } } cout << "插入成功!" << endl; //输出 cout << "输出所有学生" << endl; for (int i = 0; i < num; i++) { Student q = GetStudent(L, i)->data; cout << "学号:" << q.studentid << endl; cout << "年龄:" << q.age << endl; cout << "姓名:" << q.name << endl; cout << "语文:" << q.Score[0] << endl; cout << "数学:" << q.Score[1] << endl; cout << "英语:" << q.Score[2] << endl; } //删除 long deleteId; bool del = false; while (del == false) { cout << "请输入要删除的学生的学号" << endl; cin >> deleteId; del = DeleteStudent(L, deleteId); if (add == false) { cout << "删除失败!请重新输入要删除的学生的学号" << endl; } } cout << "删除成功!" << endl; num--; //输出 cout << "输出所有学生"<< endl; for (int i = 0; i < num; i++) { Student q = GetStudent(L, i)->data; cout << "学号:" << q.studentid << endl; cout << "年龄:" << q.age << endl; cout << "姓名:" << q.name << endl; cout << "语文:" << q.Score[0] << endl; cout << "数学:" << q.Score[1] << endl; cout << "英语:" << q.Score[2] << endl; } return 1; }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。