一个异质链表,具体要求如下:
编制一个处理异质结点链表的C++程序,即该链表中的每个结点是几种可能的数据结构中的一种。为简便计,结点仅涉及高校中的学生(student)、职工(staff)、教师(teacher)三类,其中:
student属性有:证件号(identity)、姓名(name)、出生年月(date_of_birth)、性别(sex)、学分(grade_point);
staff属性有:证件号(identity)、姓名(name)、出生年月(date_of_birth)、性别(sex)、部门(dept);
teacher属性有:证件号(identity)、姓名(name)、出生年月(date_of_birth)、性别(sex)、科研经费(fund)。
要求在主程序中建立一个有三个结点的异质链表,结点分别为student,staff,teacher类的对象,打印该链表,再将三个结点全部删除。
很经典的一个题目哦,以下是我的一个实现:
//myDeclaration.h
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
Person(int identity,string name,string birth, char sex)
{
m_identity = identity;
m_name = name;
m_birth = birth;
m_sex = sex;
}
virtual void Display() = 0;
protected:
int m_identity;
string m_name;
string m_birth;
char m_sex;
};
class Student: public Person
{
public:
Student(int identity,string name,string birth, char sex,int gradePoint)
:Person(identity, name, birth, sex),m_gradePoint(gradePoint){}
void Display()
{
cout << "证件号:"<< m_identity << endl;
cout << "姓名:" << m_name << endl;
cout << "性别:" << (m_sex == '0'?"男":"女") << endl;
cout << "出生年月:" << m_birth << endl;
cout << "学分: " << m_gradePoint << endl << endl;
}
private:
int m_gradePoint;
};
class Staff: public Person
{
public:
Staff(int identity,string name,string birth, char sex,string dept)
:Person(identity, name, birth, sex),m_dept(dept){}
void Display()
{
cout << "证件号:"<< m_identity << endl;
cout << "姓名:" << m_name << endl;
cout << "性别:" << (m_sex == '0'?"男":"女") << endl;
cout << "出生年月:" << m_birth << endl;
cout << "部门: " << m_dept << endl << endl;
}
private:
string m_dept;
};
class Teacher: public Person
{
public:
Teacher(int identity,string name,string birth, char sex,int fund)
:Person(identity, name, birth, sex),m_fund(fund){}
void Display()
{
cout << "证件号:"<< m_identity << endl;
cout << "姓名:" << m_name << endl;
cout << "性别:" << (m_sex == '0'?"男":"女") << endl;
cout << "出生年月:" << m_birth << endl;
cout << "科研经费: " << m_fund << endl << endl;
}
private:
int m_fund;
};
template <typename T>class HeterList;
template <typename T>
class Node
{
friend class HeterList<T>;
public:
Node(T *data):m_data(data),m_next(NULL){}
private:
T *m_data;
Node<T> *m_next;
};
template <typename T>
class HeterList
{
public:
HeterList():m_head(NULL),m_tail(NULL){}
void Insert(T *newNode);
void Delete();
void Print();
private:
int m_length;
Node<T> *m_head;
Node<T> *m_tail;
};
////////////////////////////////////////////////////////////////
template <typename T>
void HeterList<T>::Insert(T *newNode)
{
Node<T> *tmp = new Node<T>(newNode);
if (!m_head)
{
m_head = m_tail = tmp;
m_length++;
}
else
{
m_tail->m_next = tmp;
m_tail = tmp;
m_length++;
}
}
template <typename T>
void HeterList<T>::Delete()
{
Node<T> *tmp = m_head;
m_head = m_head->m_next;
delete tmp;
}
template <typename T>
void HeterList<T>::Print()
{
if (!m_head)
{
cout << "链表空..." << endl;
return;
}
Node<T> *tmp;
for(tmp=m_head; tmp; tmp=tmp->m_next)
{
tmp->m_data->Display();
}
}
#include <string>
using namespace std;
class Person
{
public:
Person(int identity,string name,string birth, char sex)
{
m_identity = identity;
m_name = name;
m_birth = birth;
m_sex = sex;
}
virtual void Display() = 0;
protected:
int m_identity;
string m_name;
string m_birth;
char m_sex;
};
class Student: public Person
{
public:
Student(int identity,string name,string birth, char sex,int gradePoint)
:Person(identity, name, birth, sex),m_gradePoint(gradePoint){}
void Display()
{
cout << "证件号:"<< m_identity << endl;
cout << "姓名:" << m_name << endl;
cout << "性别:" << (m_sex == '0'?"男":"女") << endl;
cout << "出生年月:" << m_birth << endl;
cout << "学分: " << m_gradePoint << endl << endl;
}
private:
int m_gradePoint;
};
class Staff: public Person
{
public:
Staff(int identity,string name,string birth, char sex,string dept)
:Person(identity, name, birth, sex),m_dept(dept){}
void Display()
{
cout << "证件号:"<< m_identity << endl;
cout << "姓名:" << m_name << endl;
cout << "性别:" << (m_sex == '0'?"男":"女") << endl;
cout << "出生年月:" << m_birth << endl;
cout << "部门: " << m_dept << endl << endl;
}
private:
string m_dept;
};
class Teacher: public Person
{
public:
Teacher(int identity,string name,string birth, char sex,int fund)
:Person(identity, name, birth, sex),m_fund(fund){}
void Display()
{
cout << "证件号:"<< m_identity << endl;
cout << "姓名:" << m_name << endl;
cout << "性别:" << (m_sex == '0'?"男":"女") << endl;
cout << "出生年月:" << m_birth << endl;
cout << "科研经费: " << m_fund << endl << endl;
}
private:
int m_fund;
};
template <typename T>class HeterList;
template <typename T>
class Node
{
friend class HeterList<T>;
public:
Node(T *data):m_data(data),m_next(NULL){}
private:
T *m_data;
Node<T> *m_next;
};
template <typename T>
class HeterList
{
public:
HeterList():m_head(NULL),m_tail(NULL){}
void Insert(T *newNode);
void Delete();
void Print();
private:
int m_length;
Node<T> *m_head;
Node<T> *m_tail;
};
////////////////////////////////////////////////////////////////
template <typename T>
void HeterList<T>::Insert(T *newNode)
{
Node<T> *tmp = new Node<T>(newNode);
if (!m_head)
{
m_head = m_tail = tmp;
m_length++;
}
else
{
m_tail->m_next = tmp;
m_tail = tmp;
m_length++;
}
}
template <typename T>
void HeterList<T>::Delete()
{
Node<T> *tmp = m_head;
m_head = m_head->m_next;
delete tmp;
}
template <typename T>
void HeterList<T>::Print()
{
if (!m_head)
{
cout << "链表空..." << endl;
return;
}
Node<T> *tmp;
for(tmp=m_head; tmp; tmp=tmp->m_next)
{
tmp->m_data->Display();
}
}
//main.cpp
#include "myDeclaration.h"
void main()
{
Student student(0,"Tom","19850615",'0',100);
Staff staff(1,"Andy","19860101",'0',"学工处");
Teacher teacher(2,"Douglas","19800422",'1',5000);
HeterList<Person> myList;
//加入链表
myList.Insert(&student);
myList.Insert(&staff);
myList.Insert(&teacher);
myList.Print();
//逐个删除
myList.Delete();
myList.Delete();
myList.Delete();
myList.Print();
}
void main()
{
Student student(0,"Tom","19850615",'0',100);
Staff staff(1,"Andy","19860101",'0',"学工处");
Teacher teacher(2,"Douglas","19800422",'1',5000);
HeterList<Person> myList;
//加入链表
myList.Insert(&student);
myList.Insert(&staff);
myList.Insert(&teacher);
myList.Print();
//逐个删除
myList.Delete();
myList.Delete();
myList.Delete();
myList.Print();
}
运行结果如下:
----------------------------------------------------------------------
证件号:0
姓名:Tom
性别:男
出生年月:19850615
学分: 100
证件号:1
姓名:Andy
性别:男
出生年月:19860101
部门: 学工处
证件号:2
姓名:Douglas
性别:女
出生年月:19800422
科研经费: 5000
链表空...
Press any key to continue
----------------------------------------------------------------------