⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linklist.cpp

📁 数据库实验1,华南师范大学的,不含实验报告
💻 CPP
字号:
//此LinkList.cpp里的LinkList的成员函数的函数体都是空的,需要填写函数体

#include <iostream.h>
#include <fstream.h>
#include "LinkList.h"

LinkList::LinkList(){  //建立空链表
    head=tail=currptr=NULL;
}

void LinkList::Clear(){ //把链表里的所有学生信息删掉,释放结点空间,成为空表
	while(head!=NULL)
	{currptr=head;
	head=head->next;
	delete currptr;
	}
}


void LinkList::Insert(){ //插入一个学生健康信息
	ListNode *TempNode;
	Datatype itemval;
	cout<<"请输入学生信息数据:";
	cin>>itemval;
	TempNode=new ListNode (itemval,NULL);
	if(currptr==NULL)
	{   
		currptr=TempNode;
		if(head==NULL)head=TempNode;
		else
		{
			TempNode->next=head;
			head=TempNode;
		}
	}
	else
	{
		TempNode->next=currptr->next;
		currptr->next=TempNode;
	}
	cout<<"插入成功!"<<endl;
	cout<<"插入学生信息如下:"<<currptr->data<<endl;
}

void LinkList::Delete(){ //删除一个学生信息
	ListNode *p;
	if(currptr==NULL)cout<<"删除失败!\n";
	else
		if(currptr==head)
				head=head->next;
			else
			
				p=head;
				while(p->next->data.no !=currptr->data.no)
					p=p->next;
				if(currptr->next==NULL)
				{
					p->next=NULL;
				}
				else
					p->next=currptr->next;
			
			delete currptr;
			currptr=NULL;
}

void LinkList::Load(){ //从文件里读出学生健康表,建立为链表
	ListNode *p;
	int i=1;
	fstream ifile;
	ifile.open("txt1.txt",ios::in);
	if(!ifile)
	{
		cout<<"text1.dat can't open"<<endl;
		abort();
	}
	else if(p=NULL) cout<<"无信息读取!"<<endl;
    else cout<<"读取成功!读取信息如下:"<<endl;
	while(!ifile.eof())
	{   
		ifile.read((char*)&p,sizeof(p));
		cout<<p->data<<endl;
		p=p->next;
	}
	ifile.close ();
   
}

void LinkList::Save(){ //往文件里写入学生健康表
	ListNode *p=head;
	if(head==NULL)
		cout<<"无信息写入!写入失败!"<<endl;
	else cout<<"信息写入文件成功!"<<endl;
	fstream ofile;
	ofile.open("txt1.txt",ios::out);
	if(!ofile)
	{
		cout<<"text1.dat can't open"<<endl;
		abort();
	}
	while(p!=NULL)
	{
		ofile.write((char*)&p,sizeof(p));
		p=p->next;
	}
	ofile.close();
}

void LinkList::SearchNo(){ //按学号查询
    int i;
	cout<<"请输入学号:";
	cin>>i;
	ListNode *p=head;
	if(head==NULL)
		cout<<"查找失败!无此信息!"<<endl;
	else {
		cout<<"查找成功!学生信息如下:"<<endl;
		for(int j=1;j<i;j++)
			p=p->next;
		cout<<p->data<<endl;
	}
}

void LinkList::Display(){  //屏幕上显示所有学生信息
	cout<<"全部学生信息如下:"<<endl;
	ListNode *p=head;
	if(head==NULL)
		cout<<"无信息!"<<endl;
	while(p!=NULL)
	{
		cout<<p->data<<endl;
        p=p->next;
	}

}

istream& operator>>(istream& is,Datatype& itemval)
{
	cout<<"input the student'number:"<<endl;
	is>>itemval.no;
	cout<<"input the student'name:"<<endl;
	is>>itemval.name;
	cout<<"input the student'health:"<<endl;
	is>>itemval.health;
	
	return is;
}


ostream& operator<<(ostream& os,Datatype& itemval)
{
	cout<<"the student's number is:";
	os<<itemval.no<<endl;
	cout<<"the student'name is:";
	os<<itemval.name<<endl;
	cout<<"the student'health is:";
	os<<itemval.health<<endl;

	return os;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -