学生健康情况管理系统(单链表)

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

文件"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
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值