STL的list模版的使用

结构体

typedef struct STUDENT
{
	char s_name[20];    //学生姓名 
	char xy_name[20];   //学院名字 
	char s_class[20];  //班级 
	char s_ID[20];     //学号 
}student;

宏定义

#define TCHAR char*
#define CHAR  char
#define INT   int

#define YES   1
#define NO    0

类定义

class CStudent
{
	public:
		CStudent();
		/******************************/
		//增加学生信息
		void Add();
		void Input(student *New);
		void ShowAdd();
		/****************************/
		//浏览
		void Printf();
		void ShowPrintf();
		/***************************/
		//查询
		void Find();
		void FindByXY(TCHAR xy);
		void FindByName(TCHAR name);
		void FindByClass(TCHAR s_Class);
		void FindByID(TCHAR s_ID);
		//模糊查询
		void FindMH();
		void PrintfMH(list<student> &show);
		/***************************/
		//修改
		void Modify();
		/*删除*/
		void Remove(TCHAR name);
		/***************************/
		/*表内排序*/
		void SortIn();
		bool SortByName(student &stu1, student &stu2);
		/*表外排序*/
		void Sort();
		void PrintfSort(list<student>::iterator *p);
		/***************************/
		//反向打印
		void RePrintf();
		/**************************/

		bool Warn(TCHAR text);
		~CStudent();
	protected:
		/*链表*/
		list<student> stu;
		/*获取链表头结点*/
		list<student>::iterator s;
};

添加函数:

void CStudent::ShowAdd()
{
	cout<<"=========================================================="<<endl;
	cout<<"|********************************************************|"<<endl;
	cout<<"|**|==================================================|**|"<<endl;
	cout<<"|**|                     增加学生信息                 |**|"<<endl;
	cout<<"|**|                                                  |**|"<<endl;
	cout<<"|**|==================================================|**|"<<endl;
	cout<<"|********************************************************|"<<endl;
	cout<<"==========================================================\n"<<endl;	
}

/*添加数据*/
void CStudent::Add()
{
	student New;
	CHAR text[] = "增加学生信息";
	INT flag = 1;
	ShowAdd();
	while(flag)
	{
		Input(&New);
		stu.push_back(New);     //stu.push_back是往节点尾部添加数据	
		if(Warn(text) == NO)
			flag = 0;
	}
}

浏览数据函数:

void CStudent::Printf()
{
	s = stu.begin();        //stu.begin()是头节点地址
	cout << "名字\t\t班级\t学号\t\t学院\n" << endl;
	while (s != stu.end())   //stu.end()是尾节点地址
	{
		cout << s->s_name << "\t\t" << s->s_class << "\t" << s->s_ID << "\t\t" << s->xy_name << endl;
		s++;            //s++是指向下一个结点地址
	}
}

修改函数:

void CStudent::Modify()
{
	CHAR s_ID[20];
	INT falg = 1;      
	CHAR text[] = "修改学生信息";
	int flag1 = 0;        //判断是否找都该学号
	while (falg)
	{
		s = stu.begin();
		cout << "请输入学生学号:";
		cin >> s_ID;
		while (s != stu.end())
		{
			if (0 == strcmp(s->s_ID, s_ID))
			{
				flag1++;
				break;
			}
			s++;
		}
		if (flag1 != 0)
		{
			cout << "请输入学号:";
			cin >> s->s_ID;
			cout << "\n" << endl;
			cout << "输入班级:";
			cin >> s->s_class;
			cout << "\n" << endl;
			cout << "输入学院:";
			cin >> s->xy_name;
			cout << "\n" << endl;
			cout << "姓名\t\t班级\t学号\t\t学院\n" << endl;
			cout << s->s_name << "\t\t" << s->s_class << "\t" << s->s_ID << "\t\t" << s->xy_name << "\n" << endl;
		}
		else
			cout << "查无此人\n"<<endl;
		if (Warn(text) == NO)
			falg = 0;
	}
}

模糊查询函数:

/*模糊查询*/
void CStudent::FindMH()
{
	list<student> bc;     //用链表储存找到的数据
	s = stu.begin();
	CHAR name[20];
	cout << "模糊查询:";
	cin >> name;
	cout << "\n" << endl;
	while(s != stu.end())
	{ 
		if (strstr(s->s_name,name))      //strstr()判断是否含有要查找的信息
			bc.push_back(*s);
		s++;
	}
	if (bc.size())
		PrintfMH(bc);
}
/*打印模糊查询后的数据*/
void CStudent::PrintfMH(list<student> &show)
{
	s = show.begin();
	cout << "姓名\t\t班级\t学号\n" << endl;
	while(s != show.end())
	{ 
		cout << s->s_name << "\t\t" << s->s_class << "\t" << s->s_ID << "\t\t" << s->xy_name << "\n" << endl;
		s++;
	}
}

其他查询:

/*名字查询*/
void CStudent::FindByName(TCHAR name)
{
	s = stu.begin();
	cout << "姓名\t\t班级\t学号\t\t学院\n" << endl;
	while (s != stu.end())
	{
		if (0 == strcmp(s->s_name, name))
			cout << s->s_name << "\t\t" << s->s_class << "\t" << s->s_ID << "\t\t" << s->xy_name << "\n" << endl;
		s++;
	}
}

/*班级查询*/
void CStudent::FindByClass(TCHAR s_Class)
{
	s = stu.begin();
	cout << "姓名\t\t班级\t学号\t\t学院\n" << endl;
	while (s != stu.end())
	{
		if (0 == strcmp(s->s_class, s_Class))
			cout << s->s_name << "\t\t" << s->s_class << "\t" << s->s_ID << "\t\t" << s->xy_name << "\n" << endl;
		s++;
	}
}

/*学号查询*/
void CStudent::FindByID(TCHAR s_ID)
{
	s = stu.begin();
	cout << "姓名\t\t班级\t学号\t\t学院\n" << endl;
	while (s != stu.end())
	{
		if (0 == strcmp(s->s_ID,s_ID))
			cout << s->s_name << "\t\t" << s->s_class << "\t" << s->s_ID << "\t\t" << s->xy_name << "\n" << endl;
		s++;
	}
}

按名字表内排序:

/*按名字排序*/
bool CStudent::SortByName(student &stu1, student &stu2)
{
	return 0 < strcmp(stu1.s_name, stu2.s_name) ? 1 : 0;
}

/*表内排序*/
void CStudent::SortIn()
{
	stu.sort(SortByName);   //把函数的地址放进去
}

按名字表外排序:

/*表外排序*/
void CStudent::Sort()
{
	int n = stu.size(),i = 0;
	s = stu.begin();
	list<student>::iterator *ps = new list<student>::iterator[n+1];
	while (s != stu.end())
	{
		ps[i] = s;
		i++;
		s++;
	}
	i = 0;
	while(i < n)
	{ 
		int j = i+1;
		int m = i;
		while (j < n)
		{
			if (strcmp(ps[j]->s_name, ps[m]->s_name) > 0)
				m = j;
			j++;
		}
		if (i != m)
		{
			list<student>::iterator temp = ps[i];
			ps[i] = ps[m];
			ps[m] = temp;
		}
		i++;
	}
	PrintfSort(ps);
	delete []ps;
}

/*显示表外排序的数据*/
void CStudent::PrintfSort(list<student>::iterator *p)
{
	int i = 0;
	cout << "姓名\t\t班级\t学号\t\t学院\n" << endl;
	while (p[i] != stu.end())
	{
		cout << p[i]->s_name << "\t\t" << p[i]->s_class << "\t" << p[i]->s_ID << "\t\t" << p[i]->xy_name << "\n" << endl;
		i++;
	}
}

反向打印:

//反向打印
void CStudent::RePrintf()
{
	list<student>::reverse_iterator ri;
	cout << "反向打印:\n" << endl;
	cout << "姓名\t\t班级\t学号\t\t学院\n" << endl;
	ri = stu.rbegin();   //尾节点地址
	while (ri != stu.rend())  //头结点
	{
		cout << ri->s_name << "\t\t" << ri->s_class << "\t" << ri->s_ID << "\t\t" << ri->xy_name << "\n" << endl;
		ri++;
	}
}




猜你喜欢

转载自blog.csdn.net/qq_38611124/article/details/81005961
今日推荐