实现异质单链表类

大学人员分为两类,大学人员分为两类,一类是教学人员,一类是非教学人员。这两类人员的信息管理系统中一部分信息内容不同,另一部分信息内容相同。设教学人员的信息包括姓名、年龄和专业编号;非教学人员的信息包括姓名、年龄和业绩评定。现邀请设计一个能同时存储学习教学人员和非教学人员的异质单链表类。

 

#include <string.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
class DlinList;

class Person{     //建立一个基类,不同人员分别继承Person类
    friend class DlinList;
protected:
    string name;
    int age;
    Person* next;
    static Person* point;
public:
    Person(string nm,int ag);
    ~Person(){}

    virtual void print();
};

Person::Person(string nm,int ag)
{
    name=nm;
    age=ag;
    next=NULL;
}

void Person::print()
{
    cout<<"name:"<<name<<endl;
    cout<<"age:"<<age<<endl;
}
class Professor:public Person  //教学人员类
{
private:
    int SpecialNo;
public:
    Professor(string nm,int ag,int sn):Person(nm,ag)
    {SpecialNo=sn;}

    ~Professor(){};

    void print();
};

void Professor::print()
{
    Person::print();
    cout<<"zhuangye "<<SpecialNo<<endl;
}

class Staff:public Person   //非教学人员类
{
private:
    char comment;
public:
    Staff(string nm,int ag,char cm):Person(nm,ag)
    {comment=cm;}
    ~Staff(){}

    void print();
};


void Staff::print()
{
    Person::print();
    cout<<"yeji "<<comment<<endl;
}

class DlinList   //一个链表类,实现各种操作
{
private:
    Person* head;
    int size;
public:
    DlinList():head(NULL),size(0){}
    ~DlinList();

    Person* index(int pos);    //定位
    void insert(Person* p,int pos);  //插入
    void Delete(int pos);      //删除
    void print();
};

DlinList::~DlinList()
{
    Person* curr,*prev;
    curr=head;
    while(curr!=NULL)
    {
        prev=curr;
        curr=curr->next;
        delete prev;
    }
    size=0;
}

Person* DlinList::index(int pos)
{
    if(pos==-1)
        return head;
    Person* curr=head;
    int i=0;
    if(curr!=NULL&&i<pos)
    {
        curr=curr->next;
        i++;
    }
    return curr;
}

void DlinList::insert(Person* p,int pos)
{
    Person* prev=index(pos-1);

    Person::point=p;
    if(pos==0)
    {
        Person::point->next=head;
        head=Person::point;    
    }
    else
    {
        Person::point->next=prev->next;
        prev->next=Person::point;

    }
    size++;
}

void DlinList::Delete(int pos)
{
    if(size==0)
    {
        cout<<"link is empty"<<endl;
        exit(1);
    }
    Person* kill;
    Person* prev=index(pos-1);
    if(pos==0)
    {kill=prev;
    head=head->next;
    }
    else
    {
        kill=prev->next;
        prev->next=prev->next->next;
    }
    delete kill;
    size--;
}

void DlinList::print()
{
    Person* curr=head;
    while(curr!=NULL)
    {
        curr->print();
        curr=curr->next;
    }
}

Person* Person::point=NULL;

int main()
{
    DlinList personList;

    Person *p1=new Professor("zhangsan",40,2);
    Person *p2=new Professor("lisi",50,4);
    Person *p3=new Staff("wangwu",30,'A');
    Person *p4=new Staff("zhaoliu",30,'A');

    personList.insert(p1,0);
    personList.insert(p2,1);
    personList.insert(p3,2);
    personList.insert(p4,3);

    personList.Delete(0);
    personList.print();
    
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值