文件"sqlist.h"
//学生健康情况管理系统内部变量以及类的声明文件
class Stu_node;
//学生信息类
class student
{
public:
char num[20]; //学号
char name[20]; //姓名
char birthday[20]; //出生日期
char sex[10]; //性别
char healthy[10]; //健康状况
public:
student &operator = (student& e);
friend class Stu_node;
};
class Sq_list;
class Stu_node
{
public:
student elem;
Stu_node* next;
public:
Stu_node();
Stu_node(student e);
Stu_node &operator = (Stu_node e);
};
class Sq_list
{
private:
Stu_node* head; //头指针,指向头结点
Stu_node* tail; //尾指针,指向尾结点
void clean(); //清楚链表,保留头指针
public:
Sq_list();
~Sq_list();
Stu_node* Create(student d); //创建结点
bool empty(); //判断表是否为空
int length(); //计算表长
void Insert_front(Stu_node* p); //头插入
void Insert_rear(Stu_node* p); //尾插入
bool Insert(int pos,student d); //在第pos个结点后面插入
bool Search(char *newnum, Stu_node& d,int &pos); //按学号查找
bool Delete(int pos,Stu_node& d); //删除第pos个结点的信息
void Update(int pos,student d); //更新第pos个结点的信息
void Print(); //遍历链表
bool Write_to_file(); //信息写入文件
bool Read_from_file(); //读取文件信息
};
文件"sqlist.cpp"
//学生健康情况管理系统类的成员函数实现文件
#include "sqlist.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
student &student::operator = (student& e)
{
strcpy(num,e.num);
strcpy(name,e.name);
strcpy(birthday,e.birthday);
strcpy(sex,e.sex);
strcpy(healthy,e.healthy);
return *this;
}
Stu_node::Stu_node()
{
next=NULL;
}
Stu_node::Stu_node(student e)
{
elem=e;
next=NULL;
}
Stu_node &Stu_node::operator = (Stu_node e)
{
elem=e.elem;
next=e.next;
return *this;
}
Sq_list::Sq_list()
{
head=tail=new Stu_node();
}
Sq_list::~Sq_list()
{
clean();
delete head;
}
void Sq_list::clean()
{
//删除整条链,保留头结点
Stu_node* p=head->next;
while(p)
{
head->next=p->next;
delete p;
p=head->next;
}
}
Stu_node* Sq_list::Create(student d)
{
Stu_node* p=new Stu_node(d);
return p;
}
bool Sq_list::empty()
{
return (head->next==NULL);
}
int Sq_list::length()
{
int size=0;
Stu_node* p=head->next;
while(p)
{
size++;
p=p->next;
}
return size;
}
void Sq_list::Insert_front(Stu_node* p) //头插入
{
p->next=head->next;
head->next=p;
}
void Sq_list::Insert_rear(Stu_node* p) //尾插入
{
tail->next=p;
tail=p;
}
bool Sq_list::Insert(int pos,student d)
{
//在第pos个结点后面插入
if(pos<0 || pos>length())
{
cout<<"位置参数不合法"<<endl;
return false;
}
int i=1;
Stu_node* p=head->next;
Stu_node* temp=new Stu_node(d);
if(pos==0)
{
//在头结点后面插入
temp->next=head->next;
head->next=temp;
}
else
{
while(i<pos && p)
{
i++;
p=p->next;
}
temp->next=p->next;
p->next=temp;
}
return true;
}
bool Sq_list::Search(char *newnum, Stu_node& d,int &pos)
{
//根据学号在表中查询学生信息,查询到
//的信息由d来装载,该节点在表中位置由pos表示
if(this->empty())
{
cout<<"list is empty."<<endl;
return false;
}
pos=1;
Stu_node* p=head->next;
while(strcmp(p->elem.num,newnum)!=0 && p->next)
{
p=p->next;
pos++;
}
if(!p || strcmp(p->elem.num,newnum)!=0)
{
pos=-1;
return false;
}
d=*p;
return true;
}
bool Sq_list::Delete(int pos,Stu_node& d)
{
//删除第pos个结点,值由d来装载
if(pos<1 || pos>length())
{
cout<<"位置参数不合法"<<endl;
return false;
}
int i=1;
Stu_node* p=head->next;
if(pos==1)
{
head->next=p->next;
d=*p;
delete p;
return tr

该博客介绍了一个基于单链表实现的学生健康情况管理系统,涵盖了数据的存储、插入、删除等操作。通过头文件'sqlist.h'、源文件'sqlist.cpp'以及主函数'main.cpp'进行实现,并展示了测试结果。
最低0.47元/天 解锁文章
1万+

被折叠的 条评论
为什么被折叠?



