#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
virtualvoid get()=0;
virtualvoid display()=0;
stringname;
stringnum;
};
class UnderGrad :public Student
{
public:
voidget()
{
cout<< "姓名:"; cin >> name;
cout<< "学号:"; cin >> num;
cout<< "班级号:"; cin >> classnum;
}
voiddisplay();
private:
stringclassnum;
};
class Graduate :public Student
{
public:
voidget()
{
cout<< "姓名:"; cin >> name;
cout<< "学号:"; cin >> num;
cout<< "导师姓名:"; cin >> tutor;
}
voiddisplay();
private:
stringtutor;
};
void UnderGrad::display()
{
cout << "姓名"<<"\t"<<"学号"<<"\t"<<"班级"<<endl;
cout<<name<<"\t"<<num<<"\t"<<classnum<<endl;
}
void Graduate::display()
{
cout << "姓名"<<"\t"<<"学号"<<"\t"<<"导师姓名"<<endl;
cout<<name<<"\t"<<num<<"\t"<<tutor<<endl;
}
template <typename T>
class SList;
template <typename T>
class Node
{
friendclass SList<T>;
public:
Node(T*data) :data(data), next(NULL){}
private:
T*data;
Node<T>*next;
};
template < typename T>
class SList
{
public:
SList():head(NULL),tail(NULL){}
voidInsert(T *newNode)
{
Node<T>*t = new Node<T>(newNode);
if(!head)
{
head= tail = t;
length++;
}
else
{
tail->next= t;
tail= t;
length++;
}
}
voidDelete()
{
Node<T>*t = head;
head= head->next;
deletet;
}
voidPrint()
{
if(!head)
{
cout<< "链表空..." << endl;
return;
}
Node<T>*t;
for(t = head; t; t = t->next)
{
t->data->display();
}
}
private:
intlength;
Node<T>*head;
Node<T>*tail;
};
int main()
{
charc; UnderGrad U; Graduate G;
SList<Student>List;
for(;;)
{
cout<< "创建学生:类型(U)本科生,(G)研究生,(E)结束:";
cin>> c;
if(c == 'E'){ cout << "销毁链表."<<endl; break; }
elseif (c == 'U' || c == 'G')
switch(c)
{
case'U':U.get(); List.Insert(&U); break;
case'G':G.get(); List.Insert(&G); break;
default:break;
}
elsecout << "输入无效请重新输入:" << endl;
}
List.Print();
List.Delete();
List.Delete();
return0;
}