class link {
private:
//传入类型的时候,先传给link然后link会传给node;
node* head;
public:
link() {
}
bool add(){
}
bool del(){
}
。。。
==========================================================================
泛型就是要将你写的类型,像对待int string类型那样对待。首先要进行的就是运算符重载
重载了运算符你可以使用cin,cout直接对相应的对象进行输入,输出。可以直接使用=进行赋值。
具体实现如下:
class officer
{
//重载了标准输入输出流函数、文件输入输出流函数(可以直接存文件)
friend ostream& operator<<(ostream& out, officer& obj);
friend istream& operator>>(istream& in, officer& obj);
friend ofstream& operator<<(ofstream& outfile, officer& obj);//--------输出到文件
friend ifstream& operator>>(ifstream& infile, officer& obj);//---------读取文件
private:
string id_card;
string name;
string sex;
int age;
string post;
int money;
public://---------------------------------私有属性管理方法
officer() {
id_card = “”;
name = “”;
sex = “”;
age = 0;
post = “”;
money = 0;
}
//拷贝构造函数结合赋值函数,直接进行赋值。
officer(officer& obj) {
this->id_card = obj.getid();
this->name = obj.getname();
this->sex = obj.getsex();
this->age = obj.getage();
this->money = obj.getmoney();
this->post = obj.getpost();
}
officer& operator=(officer& obj) {
this->id_card = obj.getid();
this->name = obj.getname();
this->sex = obj.getsex();
this->age = obj.getage();
this->money = obj.getmoney();
this->post = obj.getpost();
return *this;
}
//查重时使用
bool operator==(officer& obj) {
if (this->getid() == obj.getid() && this->getname() == obj.getname() && this->getsex() == obj.getsex()\
&& this->getage() == obj.getage() && this->getpost() == obj.getpost() && this->getmoney() == obj.getmoney()) {
return true;
}
return false;
}
bool operator!=(officer& obj) {
if (this->getid() == obj.getid()) {
return false;
}
return true;
}
//排序时使用,根据工资的高低。
bool operator>(officer& obj) {
if (this->getmoney() > obj.getmoney()) {
return true;
}
return false;
}
bool operator<(officer& obj) {
if (this->getmoney() < obj.getmoney()) {
return true;
}
return false;
}
void setpost(string post) {
this->post = post;
}
void setmoney(int money) {
this->money = money;
}
void setid(string id) {
id_card = id;
}
void setname(string name) {
this->name = name;
}
void setsex(string sex) {
this->sex = sex;
}
void setage(int age) {
this->age = age;
}
string getid() {
return id_card;
}
string getname() {
return name;
}
string getsex() {
return sex;
}
int getage() {
return age;
}
string getpost() {
return post;
}
int getmoney() {
return money;
}
};
泛型的链表类、节点类一般就是写死的,做到换一个数据类还可以用的效果
所以在泛型链表类中的提示性语句要有一定的泛化程度,不可以针对某种类型提示。
template
class link {
private:
node* head;
public:
link() {
string classname;
ifstream infile;
node* p, * q;
p = new node;
p->setnext(NULL);
head = new node;
head->setnext(NULL);
q = head;
classname = head->GetClass();
classname.erase(remove(classname.begin(), classname.end(), ‘<’), classname.end());
classname.erase(remove(classname.begin(), classname.end(), ‘>’), classname.end());
//cout << classname << endl;
infile.open(classname);
while (infile >> p->opedata()) {
q->setnext§;
q = q->getnext();
p = new node;
p->setnext(NULL);
}
delete p;
infile.close();
}
void addnode() {//-------------------------增
node* p, * q;
p = new node;
p->setnext(NULL);
cout << “请输入您要存储的数据:” << endl;
cin >> p->opedata();
q = head;
for (; q->getnext() != NULL; q = q->getnext()) {
if (q->getnext()->opedata() == p->opedata()) {
cout << “您输入的数据,已存在,不需要重复录入” << endl;
system(“pause”);
delete p;
return;
}
}
q->setnext§;
savelink();
cout << “存储成功!” << endl;
system(“pause”);
}
void delnode() {//---------------------------删
bool k = false;
node* p, * q, * r;
p = new node;
p->setnext(NULL);
cout << “请输入您要删除的数据” << endl;
cin >> p->opedata();
q = head;
for (; q->getnext() != NULL; q = q->getnext()) {
if (q->getnext()->opedata() == p->opedata() && q->getnext()->getnext() != NULL) {
r = q->getnext();
q->getnext() = q->getnext()->getnext();
delete r;
k = true;
break;
}
else if (q->getnext()->opedata() == p->opedata() && q->getnext()->getnext() == NULL) {
r = q->getnext();
delete r;
q->setnext(NULL);
k = true;
break;
}
}
if (k == false) {
cout << “没有找到您要删除的数据,请核实后再来!” << endl;
}
else if (k == true) {
savelink();
cout << “删除成功!” << endl;
}
delete p;
system(“pause”);
return;
}
void altenode() {//-------------------------------改
int judgecin = 0;
bool k = false;
node* p, * q, * r;
p = new node;
p->setnext(NULL);
cout << “请输入您要改动的数据” << endl;
judgecin = 1;
cin >> p->opedata();
judgecin = 0;
q = head;
for (; q->getnext() != NULL; q = q->getnext()) {
if (q->getnext()->opedata() == p->opedata()) {
cout << “请输入新的数据:” << endl;
rewrite:
cin >> p->opedata();
for (r = head; r->getnext() != NULL; r = r->getnext()) {
if (r->getnext()->opedata() == p->opedata() && p->opedata() != q->getnext()->opedata()) {
system(“cls”);
judgecin++;
if (judgecin == 3) {
cout << “您多次输入信息错误,请核实后再来,将要返回主菜单” << endl;
delete p;
system(“pause”);
return;
}
cout << “请输入您自己的身份信息!或输入新的身份证号” << endl;
goto rewrite;
}
}
q->getnext()->opedata() = p->opedata();
k = true;
}
}
if (k == true) {
savelink();
cout << “修改成功!” << endl;
}
else {
cout << “修改失败!没有找到该数据!” << endl;
}
delete p;
system(“pause”);
return;
}
void selnode() {//----------------------------------查
cout << “请输入您要查找的数据” << endl;
bool k = false;
node* p, * q;
p = new node;
p->setnext(NULL);
cin >> p->opedata();
for (q = head; q->getnext() != NULL; q = q->getnext()) {
if (q->getnext()->opedata() == p->opedata()) {
k = true;
cout << “您要查找的数据如下!” << endl;
cout << q->getnext()->opedata() << endl;
//break;
}
}
if (k == false) {
cout << “没有找到您要查找的数据!抱歉” << endl;
}
system(“pause”);
return;
}
void printlink() {//------------------------------打印链表
node* p;
sortlink();
for (p = head; p->getnext() != NULL; p = p->getnext()) {
cout << p->getnext()->opedata() << endl;
}
system(“pause”);
}
void sortlink() {//-------------------------------排序
node* p, * q;
if (head->getnext() == NULL) {
cout << “没有数据,无需排序!” << endl;
return;
}
if (head->getnext()->getnext() == NULL) {
cout << “一组数据,无需排序!” << endl;
return;
}
for (p = head->getnext(); p->getnext() != NULL; p = p->getnext()) {
for (q = p->getnext(); q != NULL; q = q->getnext()) {
if (q->opedata() > p->opedata()) {
node temp;
temp = *q;
*q = *p;
*p = temp;
temp.getnext() = q->getnext();
q->getnext() = p->getnext();
p->getnext() = temp.getnext();
}
}
}
}
void savelink() {//--------------------------------------存链表
ofstream outfile;
string classname;
node* p;
p = head;
classname = head->GetClass();
classname.erase(remove(classname.begin(), classname.end(), ‘<’), classname.end());
classname.erase(remove(classname.begin(), classname.end(), ‘>’), classname.end());
outfile.open(classname);
while (p->getnext() != NULL) {
p = p->getnext();
outfile << p->opedata() << endl;
}
outfile.close();
}
~link() {//--------------------------------------------销毁链表
node* p;
p = head->getnext();
while (p != NULL) {
delete head;
head = p;
p = p->getnext();
}
}//
};
========================================================================
将officer类型注释掉,使用int类型
可以看到就算将officer类型换为int类型,程序依旧可以稳定的运行。
这里打印职工信息系统的提示性语句出现到了这里,如果大家真的理解了泛型的思想
肯定可以轻而易举的改掉这处不足,有什么想法的小伙伴评论区留言博主吧。
======================================================================
#include
#include
using namespace std;
#include<windows.h>
#include<conio.h>
char off_menu();
class officer;
int mynum(string str) {//-----------------判断字符串是否全为数字
for (unsigned int i = 0; i < str.length(); i++) {
if (!isdigit(str[i])) {
return 1;
}
}
return 0;
}
template
class node {
private:
T data;
node* next;
public:
node() {
}
void setnext(node* p) {
if (p != NULL) {
next = new node;
next->data = p->opedata();
next->next = p->getnext();
}
else
next = p;
}
T& opedata() {
return data;
}
node*& getnext() {
return next;
}
const char* GetClass()
{
return typeid(*this).name();
}
};
template
class link {
private:
node* head;
public:
link() {
string classname;
ifstream infile;
node* p, * q;
p = new node;
p->setnext(NULL);
head = new node;
head->setnext(NULL);
q = head;
classname = head->GetClass();
classname.erase(remove(classname.begin(), classname.end(), ‘<’), classname.end());
classname.erase(remove(classname.begin(), classname.end(), ‘>’), classname.end());
//cout << classname << endl;
infile.open(classname);
while (infile >> p->opedata()) {
q->setnext§;
q = q->getnext();
p = new node;
p->setnext(NULL);
}
delete p;
infile.close();
}
void addnode() {//-------------------------增
node* p, * q;
p = new node;
p->setnext(NULL);
cout << “请输入您要存储的数据:” << endl;
cin >> p->opedata();
q = head;
for (; q->getnext() != NULL; q = q->getnext()) {
if (q->getnext()->opedata() == p->opedata()) {
cout << “您输入的数据,已存在,不需要重复录入” << endl;
system(“pause”);
delete p;
return;
}
}
q->setnext§;
savelink();
cout << “存储成功!” << endl;
system(“pause”);
}
void delnode() {//---------------------------删
bool k = false;
node* p, * q, * r;
p = new node;
p->setnext(NULL);
cout << “请输入您要删除的数据” << endl;
cin >> p->opedata();
q = head;
for (; q->getnext() != NULL; q = q->getnext()) {
if (q->getnext()->opedata() == p->opedata() && q->getnext()->getnext() != NULL) {
r = q->getnext();
q->getnext() = q->getnext()->getnext();
delete r;
k = true;
break;
}
else if (q->getnext()->opedata() == p->opedata() && q->getnext()->getnext() == NULL) {
r = q->getnext();
delete r;
q->setnext(NULL);
k = true;
break;
}
}
if (k == false) {
cout << “没有找到您要删除的数据,请核实后再来!” << endl;
}
else if (k == true) {
savelink();
cout << “删除成功!” << endl;
}
delete p;
system(“pause”);
return;
}
void altenode() {//-------------------------------改
int judgecin = 0;
bool k = false;
node* p, * q, * r;
p = new node;
p->setnext(NULL);
cout << “请输入您要改动的数据” << endl;
judgecin = 1;
cin >> p->opedata();
judgecin = 0;
q = head;
for (; q->getnext() != NULL; q = q->getnext()) {
if (q->getnext()->opedata() == p->opedata()) {
cout << “请输入新的数据:” << endl;
rewrite:
cin >> p->opedata();
for (r = head; r->getnext() != NULL; r = r->getnext()) {
if (r->getnext()->opedata() == p->opedata() && p->opedata() != q->getnext()->opedata()) {
system(“cls”);
judgecin++;
if (judgecin == 3) {
cout << “您多次输入信息错误,请核实后再来,将要返回主菜单” << endl;
delete p;
system(“pause”);
return;
}
cout << “请输入您自己的身份信息!或输入新的身份证号” << endl;
goto rewrite;
}
}
q->getnext()->opedata() = p->opedata();
k = true;
}
}
if (k == true) {
savelink();
cout << “修改成功!” << endl;
}
else {
cout << “修改失败!没有找到该数据!” << endl;
}
delete p;
system(“pause”);
return;
}
void selnode() {//----------------------------------查
cout << “请输入您要查找的数据” << endl;
bool k = false;
node* p, * q;
p = new node;
p->setnext(NULL);
cin >> p->opedata();
for (q = head; q->getnext() != NULL; q = q->getnext()) {
if (q->getnext()->opedata() == p->opedata()) {
k = true;
cout << “您要查找的数据如下!” << endl;
cout << q->getnext()->opedata() << endl;
//break;
}
}
if (k == false) {
cout << “没有找到您要查找的数据!抱歉” << endl;
}
system(“pause”);
return;
}
void printlink() {//------------------------------打印链表
node* p;
sortlink();
for (p = head; p->getnext() != NULL; p = p->getnext()) {
cout << p->getnext()->opedata() << endl;
}
system(“pause”);
}
void sortlink() {//-------------------------------排序
node* p, * q;
if (head->getnext() == NULL) {
cout << “没有数据,无需排序!” << endl;
return;
}
if (head->getnext()->getnext() == NULL) {
cout << “一组数据,无需排序!” << endl;
return;
}
for (p = head->getnext(); p->getnext() != NULL; p = p->getnext()) {
for (q = p->getnext(); q != NULL; q = q->getnext()) {
if (q->opedata() > p->opedata()) {
node temp;
temp = *q;
*q = *p;
*p = temp;
temp.getnext() = q->getnext();
q->getnext() = p->getnext();
p->getnext() = temp.getnext();
}
}
}
}
void savelink() {//--------------------------------------存链表
ofstream outfile;
string classname;
node* p;
p = head;
classname = head->GetClass();
classname.erase(remove(classname.begin(), classname.end(), ‘<’), classname.end());
classname.erase(remove(classname.begin(), classname.end(), ‘>’), classname.end());
outfile.open(classname);
while (p->getnext() != NULL) {
p = p->getnext();
outfile << p->opedata() << endl;
}
outfile.close();
}
~link() {//--------------------------------------------销毁链表
node* p;
p = head->getnext();
while (p != NULL) {
delete head;
head = p;
p = p->getnext();
}
}//
};
class officer
{
friend ostream& operator<<(ostream& out, officer& obj);
friend istream& operator>>(istream& in, officer& obj);
friend ofstream& operator<<(ofstream& outfile, officer& obj);//--------输出到文件
friend ifstream& operator>>(ifstream& infile, officer& obj);//---------读取文件
private:
string id_card;
string name;
string sex;
int age;
string post;
int money;
public://---------------------------------私有属性管理方法
officer() {
id_card = “”;
name = “”;
sex = “”;
age = 0;
post = “”;
money = 0;
}
officer(officer& obj) {
this->id_card = obj.getid();
this->name = obj.getname();
this->sex = obj.getsex();
this->age = obj.getage();
this->money = obj.getmoney();
this->post = obj.getpost();
}
officer& operator=(officer& obj) {
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)
![](https://i-blog.csdnimg.cn/blog_migrate/69d4f488a1772e4e3143501f665bfe91.jpeg)
结尾
正式学习前端大概 3 年多了,很早就想整理这个书单了,因为常常会有朋友问,前端该如何学习,学习前端该看哪些书,我就讲讲我学习的道路中看的一些书,虽然整理的书不多,但是每一本都是那种看一本就秒不绝口的感觉。
以下大部分是我看过的,或者说身边的人推荐的书籍,每一本我都有些相关的推荐语,如果你有看到更好的书欢迎推荐呀。
-读取文件
private:
string id_card;
string name;
string sex;
int age;
string post;
int money;
public://---------------------------------私有属性管理方法
officer() {
id_card = “”;
name = “”;
sex = “”;
age = 0;
post = “”;
money = 0;
}
officer(officer& obj) {
this->id_card = obj.getid();
this->name = obj.getname();
this->sex = obj.getsex();
this->age = obj.getage();
this->money = obj.getmoney();
this->post = obj.getpost();
}
officer& operator=(officer& obj) {
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-LKIrKup7-1712380777567)]
[外链图片转存中…(img-8rAPWbew-1712380777568)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
[外链图片转存中…(img-BZZrySws-1712380777568)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)
![](https://i-blog.csdnimg.cn/blog_migrate/69d4f488a1772e4e3143501f665bfe91.jpeg)
结尾
正式学习前端大概 3 年多了,很早就想整理这个书单了,因为常常会有朋友问,前端该如何学习,学习前端该看哪些书,我就讲讲我学习的道路中看的一些书,虽然整理的书不多,但是每一本都是那种看一本就秒不绝口的感觉。
以下大部分是我看过的,或者说身边的人推荐的书籍,每一本我都有些相关的推荐语,如果你有看到更好的书欢迎推荐呀。