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

📄 linked_list.h

📁 信息管理系统_by伍仟蚊(完整版)功能在于管理各种成员信息
💻 H
字号:
#include <iostream>
#include<fstream>
#include <string>
using namespace std;
//定义结点类型

template <class T>//定义T为虚类型
struct node
{
	T d;
	node *next;
};


template <class T>//模板声明
class linked_List
{
	private:
		node<T> *head;//链表头指针
	public:
		linked_List();		//构造函数,建链表
		int flag_linked_List();//检测单链表状态
		void prt_linked_List();//从头指针开始扫描输出链表中的元素
		void ins_linked_List(T);//将新元素插入到链头
		T del_linked_List();//删除链头元素
		int len_linked_List();//求链表长度
		bool test_linked_List(int);//检测学号是否存在
		void num_sort_linked_List();//按学号排序函数
		void name_sort_linked_List();//按名字排序函数
		void num_del_linked_List(int);//查找学号删除
		void name_del_linked_List(char *);//查找名字删除
		bool num_sel_linked_List(int);//按学号查找
		void name_sel_linked_List(char *);//按姓名查找
		void save_linked_List();//保存信息
};

//建立空链表
template <class T>
linked_List<T>::linked_List()
{
	head=NULL;return;
}


//检测单链表状态
template<class T>
int linked_List<T>::flag_linked_List()
{
	if(head==NULL)return;
	return(1);
}




//遍历输出链表数据
template <class T>
void linked_List<T>::prt_linked_List()
{
	node<T> *p;
	p=head;
	if(p==NULL){cout<<"信息库为空!!!"<<endl;return;} 
	do{cout<<"学号"<<p->d.num<<",姓名"<<p->d.name<<endl;
	p=p->next;
	}
	while(p!=NULL);
	return;
}
 
//将新结点插入到链头
template <class T>
void linked_List<T>::ins_linked_List(T x)
{
	node<T> *p;
	p=new node<T>;
	p->d=x;
	p->next=head;
	head=p;
	return;
}

//删除链表头元素,并返回删除值
template<class T>
T linked_List<T>::del_linked_List()
{
	T y;
	node<T> *q;
	if(head==NULL){cout<<"信息库为空!!!"<<endl;return;}
	q=head;
	y=q->d;
	head=q->next;
	delete q;
	return(y);
}

//求链表总长度
template<class T>
int linked_List<T>::len_linked_List()
{
	node<T> *p;
	int len=0;
	p=head;
	if(head==NULL)
		return len;
	while(p!=NULL)
	{len++;p=p->next;}
	return len;
}

//检查学号存在
template<class T>
bool linked_List<T>::test_linked_List(int temp)
{
	node<T> *q=head,*p=head;
	if(len_linked_List()==0)return false;
	if(head->d.num==temp)
		return true;
	while(p)
		{
			if(p->d.num==temp)
				return true;
			p=p->next;
		}
	return false;
}

//按学号选择排序设计
template<class T>
void linked_List<T>::num_sort_linked_List()
{
	//分别声明指针,其中q为max跟踪指针,r为遍历指针的跟踪指针
	node<T> *head1,*p,*q,*r,*max;
	head1=head;					//将无序交给head1
	head=NULL;
	while(head1)				//外循环控制选择有序表
	{
		p=head1;
		max=head1;
		while(p->next)			//内循环控制选择最大值
		{
			r=p;
			p=p->next;
			if(p->d.num > max->d.num)
			{
				q=r;
				max=p;
			}
		}
		if(max==head1)			//假设无序表中第一个为最大
			head1=head1->next;	//直接断开
		else q->next=max->next;	//否则在用max的跟踪指针断开
		max->next=NULL;
		if(!head)				//处理head为空时的情况
		{head=max;continue;}
		max->next=head;
		head=max;				//将max插入有序表首项
	}
}



//按名字选择排序设计
template<class T>
void linked_List<T>::name_sort_linked_List()
{
	//分别声明指针,其中q为max跟踪指针,r为遍历指针的跟踪指针
	node<T> *head1,*p,*q,*r,*max;
	head1=head;					//将无序交给head1
	head=NULL;
	while(head1)				//外循环控制选择有序表
	{
		p=head1;
		max=head1;
		while(p->next)			//内循环控制选择最大值
		{
			r=p;
			p=p->next;
			if(strcmp(p->d.name,max->d.name)>0)
			{
				q=r;
				max=p;
			}
		}
		if(max==head1)			//假设无序表中第一个为最大
			head1=head1->next;	//直接断开
		else q->next=max->next;	//否则在用max的跟踪指针断开
		max->next=NULL;
		if(!head)				//处理head为空时的情况
		{head=max;continue;}
		max->next=head;
		head=max;				//将max插入有序表首项
	}
}

//按学号删除
template<class T>
void linked_List<T>::num_del_linked_List(int temp)
{
	node<T> *q=head,*p=head;
	if(head==NULL){cout<<"信息库为空!!!"<<endl;return;}
	if(head->d.num==temp)
	{
		head=head->next;
		cout<<"删除学生信息为:\n学号:"<<p->d.num<<",姓名:"<<p->d.name<<endl;
		delete p;
		return;
	}
	while(p)
		{
			if(p->d.num==temp)
				break;
			p=p->next;
		}
	if(p)
	{
		while(q->next!=p)
		q=q->next;
		q->next=(q->next)->next;
		cout<<"删除学生信息为:\n学号:"<<p->d.num<<",姓名:"<<p->d.name<<endl;
		delete p;
	}
	else cout<<"删除失败!"<<endl;
}

//按姓名删除
template<class T>
void linked_List<T>::name_del_linked_List(char *temp)
{
	node<T> *q=head,*p=head;
	if(head==NULL){cout<<"此为空链表!!!"<<endl;return;}
	if(strcmp(head->d.name,temp)==0)
	{
		head=head->next;
		cout<<"删除学生信息为:\n学号:"<<p->d.num<<",姓名:"<<p->d.name<<endl;
		delete p;
		return;
	}
	while(p)
		{
			if(strcmp(p->d.name,temp)==0)
				break;
			p=p->next;
		}
	if(p)
	{
		while(q->next!=p)
		q=q->next;
		q->next=(q->next)->next;
		cout<<"删除学生信息为:\n学号:"<<p->d.num<<",姓名:"<<p->d.name<<endl;
		delete p;
	}
	else cout<<"删除失败!"<<endl;
}


//按学号查找
template<class T>
bool linked_List<T>::num_sel_linked_List(int temp)
{
	node<T> *q=head,*p=head;
	if(head==NULL){cout<<"此为空链表!!!"<<endl;return false;}
	if(head->d.num==temp)
	{
		cout<<"查找学生信息为:\n学号:"<<p->d.num<<",姓名:"<<p->d.name<<endl;
		return true;
	}
	while(p)
		{
			if(p->d.num==temp)
				break;
			p=p->next;
		}
	if(p)
	{
		while(q->next!=p)
		q=q->next;
		cout<<"查找学生信息为:\n学号:"<<p->d.num<<",姓名:"<<p->d.name<<endl;
	}
	else cout<<"查找失败!"<<endl;
	return false;
}

//按姓名查找
template<class T>
void linked_List<T>::name_sel_linked_List(char *temp)
{
	node<T> *q=head,*p=head;
	if(head==NULL){cout<<"此为空链表!!!"<<endl;return;}
	if(strcmp(head->d.name,temp)==0)
	{
		cout<<"查找学生信息为:\n学号:"<<p->d.num<<",姓名:"<<p->d.name<<endl;
		return;
	}
	while(p)
		{
			if(strcmp(p->d.name,temp)==0)
				break;
			p=p->next;
		}
	if(p)
	{
		while(q->next!=p)
		q=q->next;
		cout<<"查找学生信息为:\n学号:"<<p->d.num<<",姓名:"<<p->d.name<<endl;
	}
	else cout<<"查找失败!"<<endl;
}

template<class T>
void linked_List<T>::save_linked_List()
{
	node<T> *p;
	p=head;
	if(p==NULL){cout<<"空的循环表!"<<endl;return;} 
	fstream fio("stu_info.dat",ios::out|ios::trunc|ios::binary);
	if(!fio){cerr<<"文件打开失败.\n"; exit(1);}
	fio<<len_linked_List();
	while(p!=NULL)
	{fio.write((char *)&(p->d.num),sizeof(p->d.num));fio.write((char *)&(p->d.name),sizeof(p->d.name));p=p->next;}
}

⌨️ 快捷键说明

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