用一个异质链表保存学生数据

#include<iostream>  
#include<string>  
using namespace std;  
class Student  
{  
public:  
    virtual void get()=0;  
    virtual void display()=0;  
    string name;  
    string num;  
};  
class UnderGrad :public Student  
{  
public:  
    void get()  
    {  
        cout << "姓名:"; cin >> name;  
        cout << "学号:"; cin >> num;   
        cout << "班级号:"; cin >> classnum;  
    }  
    void display();  
private:  
    string classnum;  
};  
class Graduate :public Student  
{  
public:  
    void get()  
    {  
        cout << "姓名:"; cin >> name;  
        cout << "学号:"; cin >> num;  
        cout << "导师姓名:"; cin >> tutor;  
    }  
    void display();  
private:  
    string tutor;  
};  
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  
{  
    friend class 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){}  
    void Insert(T *newNode)  
    {     
        Node<T> *t = new Node<T>(newNode);    
        if (!head)    
        {         
            head = tail = t;          
            length++;     
        }     
        else      
        {     
            tail->next = t;        
            tail = t;         
            length++;         
        }  
    }  
    void Delete()  
    {     
        Node<T> *t = head;      
        head = head->next;     
        delete t;  
    }  
    void Print()  
    {     
        if (!head)    
        {         
            cout << "链表空..." << endl;         
            return;   
        }  
        Node<T> *t;     
        for (t = head; t; t = t->next)     
        {         
            t->data->display();     
        }  
    }  
private:  
    int length;  
    Node<T> *head;  
    Node<T> *tail;  
};  
  
int main()  
{  
    char c; UnderGrad U; Graduate G;  
    SList<Student> List;  
    for (;;)  
    {  
        cout << "创建学生:类型(U)本科生,(G)研究生,(E)结束:";  
        cin >> c;  
        if (c == 'E'){ cout << "销毁链表."<<endl; break; }  
        else if (c == 'U' || c == 'G')  
            switch (c)  
            {  
            case 'U':U.get(); List.Insert(&U); break;  
            case 'G':G.get(); List.Insert(&G); break;  
            default: break;  
            }         
        else cout << "输入无效请重新输入:" << endl;  
    }  
    List.Print();  
    List.Delete();  
    List.Delete();  
    return 0;  
}  
<img src="https://img-blog.csdn.net/20150602184957372" alt="" />

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值