终于考完了万恶的期末考,有时间把落下的blog更一下
异质链表means链表内节点类型可以不是同一种。
下面放上课程作业(一个小型的学校信息管理系统)中成员类型定义的.h文件。
其中有一个抽象节点(Node)基类Person类,其它类都在此基础上继承;
故在链表(HeterList)类中,定义链表头指针时,只需要将头指针定义为基类类型即可通过该头指针指向其继承类。
#include
#include
using namespace std;
//友元类需要事先声明
class HeterList;
/*
*节点类,各类class的定义
*/
//抽象基类 Person
class Person
{
friend class HeterList;
public:
//构造函数
//Person():_name(""),_sex('M'),_age(0),_IDnumber(""),_next(NULL){}
Person(char *name, char sex, int age, char *IDnumber)
:_sex(sex),_age(age),_next(NULL)
{
strcpy_s(Person::_name, name);
strcpy_s(Person::_IDnumber, IDnumber);
}
//纯虚函数
virtual void addNode() = 0; //将对象添加进链表,变成一个节点
virtual void display() = 0; //显示信息
//析构函数
~Person() {}
//希望与派生类分享,但不想被其他公共访问的成员
protected:
char _name[20];
char _sex;
int _age;
char _IDnumber[20];
static Person *_ptr; //定义一个指向Person类对象的静态指针
Person *_next; //指向下一个节点的指针
};
//教师类
class Teacher:public Person
{
friend class HeterList;
public:
//构造函数
//Teacher():Person(),_fund(0),_major(""),_salary(0){}
Teacher(char *name, char sex, int age, char *IDnumber, int fund, char *major, int salary)
:Person(name, sex, age, IDnumber),_fund(fund),_salary(salary)
{
strcpy_s(Teacher::_major, major);
}
//重写将对象添加进链表函数
void addNode()
{
_ptr = new Teacher(_name, _sex, _age, _IDnumber, _fund, _major, _salary);
}
//重写信息显示函数
void display()
{
cout<<"Name:"<<_name<cout<<"Sex:"<<_sex<cout<<"Age:"<<_age<cout<<"ID NUmber:"<<_IDnumber<cout<<"Job: Teacher"<cout<<"Fund:"<<_fund<cout<<"Major:"<<_major<cout<<"Salary:"<<_salary<cout<//析构函数
~Teacher() {}
private:
int _fund;
char _major[20];
int _salary;
};
//职工类
class Stuff:public Person
{
friend class HeterList;
public:
//构造函数
//Stuff():Person(),_department(""),_position(""),_salary(0){}
Stuff(char *name, char sex, int age, char *IDnumber, char *department, char *position, int salary)
:Person(name, sex, age, IDnumber),_salary(salary)
{
strcpy_s(Stuff::_department, department);
strcpy_s(Stuff::_position, position);
}
void display()
{
cout<<"Name:"<<_name<cout<<"Sex:"<<_sex<cout<<"Age:"<<_age<cout<<"ID NUmber:"<<_IDnumber<cout<<"Job: Stuff"<cout<<"Department:"<<_department<cout<<"Position:"<<_position<cout<<"Salary:"<<_salary<cout<//重写将对象添加进链表函数
void addNode()
{
_ptr = new Stuff(_name, _sex, _age, _IDnumber, _department, _position, _salary);
}
//析构函数
~Stuff() {}
private:
char _department[20];
char _position[20];
int _salary;
};
//本科生类
class Undergraduate:public Person
{
friend class HeterList;
public:
//构造函数
//Undergraduate():Person(),_gradePoint(0){}
Undergraduate(char *name, char sex, int age, char *IDnumber, int gradePoint)
:Person(name, sex, age, IDnumber),_gradePoint(gradePoint){}
void display()
{
cout<<"Name:"<<_name<cout<<"Sex:"<<_sex<cout<<"Age:"<<_age<cout<<"ID NUmber:"<<_IDnumber<cout<<"Job: Undergraduate"<cout<<"GradePoint:"<<_gradePoint<cout<//重写将对象添加进链表函数
void addNode()
{
_ptr = new Undergraduate(_name, _sex, _age, _IDnumber, _gradePoint);
}
//析构函数
~Undergraduate() {}
//希望与派生类分享,但不想被其他公共访问的成员
protected:
int _gradePoint;
};
//全日制研究生类
class FulltimePostgraduate:public Undergraduate
{
friend class HeterList;
//friend class Teacher;
public:
//构造函数
//FulltimePostgraduate():Undergraduate(),_major(""),_tutor(""){}
FulltimePostgraduate(char *name, char sex, int age, char *IDnumber, int gradePoint, char *major, char *tutor)
:Undergraduate(name, sex, age, IDnumber, gradePoint)
{
strcpy_s(FulltimePostgraduate::_major, major);
strcpy_s(FulltimePostgraduate::_tutor, tutor);
}
void display()
{
cout<<"Name:"<<_name<cout<<"Sex:"<<_sex<cout<<"Age:"<<_age<cout<<"ID NUmber:"<<_IDnumber<cout<<"Job: FulltimePostgraduate"<cout<<"GradePoint:"<<_gradePoint<cout<<"Major:"<<_major<cout<<"Tutor:"<<_tutor<cout<//重写将对象添加进链表函数
void addNode()
{
_ptr = new FulltimePostgraduate(_name, _sex, _age, _IDnumber, _gradePoint, _major, _tutor);
}
//析构函数
~FulltimePostgraduate() {}
//希望与派生类分享,但不想被其他公共访问的成员
protected:
char _major[20];
char _tutor[20];
};
//在职研究生类
class OnthejobPostgraduate:public FulltimePostgraduate
{
friend class HeterList;
public:
//构造函数
//OnthejobPostgraduate():FulltimePostgraduate(),_salary(0){}
OnthejobPostgraduate(char *name, char sex, int age, char *IDnumber, int gradePoint, char *major, char *tutor, int salary)
:FulltimePostgraduate(name, sex, age, IDnumber, gradePoint, major, tutor),_salary(salary){}
void display()
{
cout<<"Name:"<<_name<cout<<"Sex:"<<_sex<cout<<"Age:"<<_age<cout<<"ID NUmber:"<<_IDnumber<cout<<"Job: FulltimePostgraduate"<cout<<"GradePoint:"<<_gradePoint<cout<<"Major:"<<_major<cout<<"Tutor:"<<_tutor<cout<<"Salary:"<<_salary<cout<//重写将对象添加进链表函数
void addNode()
{
_ptr = new OnthejobPostgraduate(_name, _sex, _age, _IDnumber, _gradePoint, _major, _tutor, _salary);
}
//析构函数
~OnthejobPostgraduate() {}
//希望与派生类分享,但不想被其他公共访问的成员
protected:
int _salary;
};
//异质链表类
class HeterList
{
public:
//构造函数
HeterList():Root(NULL){}
void InsertNode(Person *node); //以基类指针为参数,可以接收所有派生类对象的指针
void SearchNode(char *name);
void DeleteNode(char *name);
void UpdateNode(char *name);
void displayList(); //待改
//析构函数
~HeterList() {}
private:
Person *Root; //定义链表头指针
};
void HeterList::InsertNode(Person *node)
{
//定义两个查找指针
Person *curr = Root; //指向当前节点的指针,从Root开始
Person *prev = NULL; //指向前一个节点的指针
//移到链表的最后一个节点
//如果当前链表没有节点,则curr=Root=NULL
while( curr != NULL )
{
//往后一个节点
prev = curr;
curr = curr->_next;
}
node->addNode();//有了指向当前对象的指针ptr
node->_ptr->_next = curr; //插在最后一个前面
if(prev==NULL) //当前链表没有节点
{
Root = node->_ptr;
}
else
{
prev->_next = node->_ptr;
}
}
void HeterList::SearchNode(char *name)
{
Person *curr = Root; //指向当前节点的指针,指向根节点
while( (curr!=NULL) && (strcmp(curr->_name, name) !=0) )
{//找到了跳出,或者到头了跳出
curr = curr->_next;
}
if( curr == NULL )
{//没找到
cout<<"Can't find this member, please check it again!"<else
{
curr->display();
}
}
void HeterList::DeleteNode(char *name)
{
Person *curr = Root; //指向当前节点的指针,指向根节点
Person *prev = NULL; //指向前一个节点的指针
while( (curr!=NULL) && (strcmp(curr->_name, name) !=0) )
{//找到了跳出,或者到头了跳出
prev = curr;
curr = curr->_next;
}
if( (prev==NULL) && (curr!=NULL) )
{//curr指向的第一个就是,把Root移动
Root = curr->_next;
delete curr;
}
else if( (prev!=NULL) && (curr!=NULL) )
{//不是第一个
prev->_next = curr->_next;
delete curr;
}
}
void HeterList::UpdateNode(char *name)
{
Person *curr = Root; //指向当前节点的指针,指向根节点
Person *prev = NULL; //指向前一个节点的指针
while( (curr!=NULL) && (strcmp(curr->_name, name) !=0) )
{//找到了跳出,或者到头了跳出
prev = curr;
curr = curr->_next;
}
if( curr == NULL )
{//没找到
cout<<"Can't find this member, please check it again!"<else
{//找到了
}
}
void HeterList::displayList()
{
Person *curr = Root;
while(curr != NULL)
{
curr->display();
curr = curr->_next;
}
}