SInfo::~SInfo() //析构函数
{
StuNode *p;
while (StuListHead)
{
p = StuListHead;
StuListHead = StuListHead->nextstu;
delete p;
}
StuListHead = NULL;
}
void SInfo::CreatSinfo() //创建学生信息表
{
int n;
StuNode *p, *s;
p = StuListHead;
cout << "请输入学生人数:";
cin >> n;
for (int i = 1; i <= n; i++)
{
s = new StuNode;
cin >> s->num >> s->math>>s->eng>>s->yuwen;
s->sum = s->math + s->eng + s->yuwen;
s->nextstu = p->nextstu;
p->nextstu = s;
p = p->nextstu;
}
if (p == NULL) //判断学生信息表是否创建成功
{
cout << "创建失败请重新创建!" << endl;
CreatSinfo();
}
}
void SInfo::ShowInfo() //遍历输出
{
StuNode *p;
cout << "学号" << '\t' << "数学" << '\t' << "英语" << '\t' << "语文" << '\t' << "总分" << endl;
for (p = StuListHead->nextstu; p != NULL; p = p->nextstu)
{
cout << p->num << '\t' << p->math << '\t' << p->eng << '\t' << p->yuwen << '\t' << p->sum << endl;
}
}
void SInfo::StuInsert(int snum, int smath,int seng,int syuwen) //插入学生信息(头插法)
{
StuNode *s,*p;
s = new StuNode;
s->num = snum;
s->math = smath;
s->eng = seng;
s->yuwen = syuwen;
s->sum = s->math + s->eng + s->yuwen;
p = StuListHead;
s->nextstu = p->nextstu;
p->nextstu = s;
}
void SInfo::StuDelete(int snum)
{
StuNode *p, *ptemp;
p = StuListHead;
ptemp = p;
while (p->nextstu && p->num!=snum) //循环终止条件为p->nextstu不为空 而且没有找到相应学号的学生
{
ptemp = p;
p = p->nextstu;
}
if (p->num == snum)
{
ptemp->nextstu = p->nextstu;
delete p;
}
else
{
cout << "未找到该学生信息!" << endl;
}
}
StuNode *SInfo::StuFind(int snum)
{
StuNode *p;
p = StuListHead->nextstu;
while (p->nextstu && p->num != snum) //循环终止条件为p->nextstu不为空 而且没有找到相应学号的学生
{
p = p->nextstu;
}
if (p->num == snum)
{
return p;
}
else
{
cout << "未找到该学生信息!" << endl;
return NULL;
}
}
void SInfo::StuModify(int snum, int smath, int seng, int syuwen)
{
StuNode *ItemStu = StuFind(snum); //直接调用查找函数
if (ItemStu != NULL)
{
ItemStu->math = smath;
ItemStu->num = snum;
ItemStu->math = smath;
ItemStu->eng = seng;
ItemStu->yuwen = syuwen;
ItemStu->sum = ItemStu->math + ItemStu->eng + ItemStu->yuwen;
}
}