//前途比爱情重要,爱情比前途难得,但对的人会站在你的前途里
#include<iostream>
using namespace std;
struct Student
{
string num;
string name;
char gender;
double score;
};
typedef struct _LinkNode {
Student data;
struct _LinkNode* next;//结点的指针域
}LinkNode, Linklist;
class XSlist
{
public:
bool InitList(Linklist*& L);
void ListInsert_back(Linklist*& L, LinkNode* node);
bool ListInsert(Linklist*& L, int i, Student dataa);
void findList(Linklist*& L, int a33);
void delNode(Linklist*& L, int a44);
void LinkPrint(Linklist*& L);
void LinklistDestroy(Linklist*& L);
private:
Linklist* first;
};
bool XSlist :: InitList(Linklist*& L) {
L = new LinkNode;
if (!L) return false;//生成结点失败
L->next = NULL;
return true;
}
//尾插法
void XSlist :: ListInsert_back(Linklist*& L, LinkNode* node) {
Linklist* r = nullptr, * s = nullptr;
first = new Linklist;
cout << "请输入插入学生的个数:";
int n; cin >> n;
r = first;
while (n--)
{
s = new LinkNode;//生成新结点s
cout << "请输入学生名字:";
cin >> s->data.name;
cout << "请输入学生学号:";
cin >> s->data.num;
cout << "请输入学生性别:";
cin >> s->data.gender;
cout << "请输入学生成绩:";
cin >> s->data.score;
cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
r->next = s; r = s;
}
r->next = nullptr;
}
//任意位置插入
bool XSlist::ListInsert(Linklist*& L, int i, Student dataa) {
if (!first->next)return false;
int j = 0;
Linklist* p, * s;
p = first;
while (p->next && j < i - 1)//查找位置为i-1的结点,p指向该结点
{
p = p->next;
j++;
}
if ( j > i - 1) {
return false;
}
s = new LinkNode;//生成新结点
s->data = dataa;
s->next = p->next;
p->next = s;
return true;
}
void XSlist::findList(Linklist*& L, int a33) {//查询学生信息3
if (a33 == 1) {
cout << "请输入要查找的学号:"; string xh; cin >> xh; cout << endl;
Linklist* p;
p = first->next;
if (!first || !first->next) {
cout << "找不到此学生!" << endl;
}
else
{
while (p && p->data.num != xh)
{
p = p->next;
}
if (!p)
{
cout << "找不到此学生!" << endl;
}
else
{
cout << "您要查询的学生为:" << endl;
cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
cout << p->data.num << " " << p->data.name << " " << p->data.gender << " " << p->data.score << endl;
}
}
}
else {
cout << "请输入要查找的姓名:"; string xm; cin >> xm; cout << endl;
//在带头结点的单链表L中查找值为e的元素位置
Linklist* p;
p = first->next;
if (!first || !first->next) {
cout << "找不到此学生!" << endl;
}
else
{
while (p && p->data.name != xm)
{
p = p->next;
}
if (!p)
{
cout << "找不到此学生!" << endl;
}
else
{
cout << "您要查询的学生为:" << endl;
cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
cout << p->data.num << " " << p->data.name << " " << p->data.gender << " " << p->data.score << endl;
}
}
}
}
void XSlist::delNode(Linklist*& L, int a44) {//删除学生信息3
if (a44 == 1)
{
cout << "请输入要查找的学号:"; string xh; cin >> xh; cout << endl;
Linklist* p, * q = first;
p = first->next;
if (!first || !first->next) {
cout << "找不到此学生!" << endl;
}
else
{
while (p && p->data.num != xh)
{
q = p; p = p->next;
}
if (!p)
{
cout << "找不到此学生!" << endl;
}
else
{
q->next = p->next;//临时保存被删结点的地址以备释放空间
//改变删除结点前驱结点的指针域
delete p; //释放被删除结点的空间
cout << "该学生的信息已被删除!" << endl;
}
}
}
else
{
cout << "请输入要查找的姓名:"; string xm; cin >> xm; cout << endl;
Linklist* p, * q = first ;
p = first->next;
if (!first || !first->next) {
cout << "找不到此学生!" << endl;
}
else
{
while (p && p->data.name != xm)
{
q = p; p = p->next;
}
if (!p)
{
cout << "找不到此学生!" << endl;
}
else
{
q->next = p->next; //临时保存被删结点的地址以备释放空间
//改变删除结点前驱结点的指针域
delete p; //释放被删除结点的空间
cout << "该学生的信息已被删除!" << endl;
}
}
}
}
//单链表打印
void XSlist::LinkPrint(Linklist*& L) {
LinkNode* p = NULL;
if (!L) { cout << "此链表为空" << endl; return; }
p = first->next;
cout << " 学号 姓名 性别 成绩" << endl;
while (p) {
cout << p->data.num << " " << p->data.name << " " << p->data.gender << " " << p->data.score << endl;
p = p->next;
}
cout << endl;
}
//销毁单链表
void XSlist::LinklistDestroy(Linklist*& L) {
Linklist* p = L;
cout << "销毁链表" << endl;
while (p) {
L = L->next; //L指向下一个结点
delete p; //删除当前结点
p = L; //p移向下一个结点
}
}
void MenuList()
{
cout << " 学生信息管理系统" << endl;
cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
cout << " 1.学生信息线性表的建立" << endl;
cout << " 2.插 入 学 生 信 息" << endl;
cout << " 3.查 询 学 生 信 息" << endl;
cout << " 4.删 除 学 生 信 息" << endl;
cout << " 5.输 出 所 有 学生信息" << endl;
cout << " 0.退出学生管理系统" << endl;
cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
cout << "请选择功能序号,按数字键0退出:";
}
int main()
{
int n;
XSlist SL;
Linklist* L = NULL;
Linklist* s = NULL;
//1.初始化一个空的链表
SL.InitList(L);
while (true)
{
MenuList();
while (cin >> n)
{
if (n == 1)//建立学生信息表
{
//使用尾插法插入数据
SL.ListInsert_back(L, s);
break;
}
if (n == 2)//学生信息表插入
{
//5.任意位置插入元素
cout << "请问你要插入几个学生" << endl;
int j; cin >> j;
while (j--) {
int i;
cout << "请输入插入学生的位置:";
cin >> i;
Student dataadd;
cout << "请输入学生名字:";
cin >> dataadd.name;
cout << "请输入学生学号:";
cin >> dataadd.num;
cout << "请输入学生性别:";
cin >> dataadd.gender;
cout << "请输入学生成绩:";
cin >> dataadd.score;
if (SL.ListInsert(L, i, dataadd)) {
cout << "插入成功" << endl;
}
else {
cout << "插入失败" << endl;
}
SL.LinkPrint(L);
}
break;
}
if (n == 3)//学生信息查询功能
{
cout << "请输入要查找的方式:" << endl;
cout << "1.按学号查询" << endl;
cout << "2.按姓名查询" << endl;
cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
cout << "请选择1或2:"; int a3; cin >> a3; cout << endl;
SL.findList(L, a3);
break;
}
if (n == 4)//学生信息的删除
{
cout << "请先查找要删除的学生信息" << endl;
cout << "1.按学号查询" << endl;
cout << "2.按姓名查询" << endl;
cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
cout << "请选择1或2:"; int a4; cin >> a4; cout << endl;
SL.delNode(L, a4);
break;
}
if (n == 5)//输出学生信息表
{
cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
cout << " 输出所有学生信息" << endl;
cout << "*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=" << endl;
SL.LinkPrint(L);
break;
}
if (n == 0)
{
cout << "退出学生信息管理系统,欢迎下次使用!";
//9.销毁单链表
SL.LinklistDestroy(L);
system("pause");
return 0;
}
}
}
}