[C++]实现异质链表

//异质链表,清翔兔 06,Jan.

#include<iostream>

#include<string>

using namespace std;

class data_rec{

protected:

       int ID;

       string Name;

       int Date;

       bool Sex;  // 0:男  1:女

public:

       int getID(){ return ID;}

       data_rec(int id,string n,int d,bool s):ID(id),Name(n),Date(d),Sex(s){}

       virtual void print()

       {

              cout << "ID:"     <<ID

                      << "/nName:" << Name

                      << "/nDate:" << Date <<endl;

                      if (!Sex) cout << "Sex:" <<endl;

                      else cout << "Sex:" <<endl;

       }

};

class Student:public data_rec{

private:

       int grade_point;

public:

       Student(int id,string n,int d,bool s,int g):data_rec(id,n,d,s),grade_point(g){}

       void print()

       {

              data_rec::print();

              cout <<"Grade Point:" << grade_point << endl;

       }

};

class Staff:public data_rec{

private:

       string dept;

public:

       Staff(int id,string n,int d,bool s,string de):data_rec(id,n,d,s),dept(de){}

       void print()

       {

              data_rec::print();

              cout <<"Dept:" << dept << endl;

       }

};

class Teacher:public data_rec{

private:

       double fund;

public:

       Teacher(int id,string n,int d,bool s,double f):data_rec(id,n,d,s),fund(f){}

       void print()

       {

              data_rec::print();

              cout <<"Fund:" << fund << endl;

       }

};

class ListItem{

public:

       ListItem(data_rec* n):item(n),next(0){}

       data_rec* item;

       ListItem *next;

};

class SchoolList{

private:

       ListItem* head;

public:

       SchoolList():head(0){}

       void print();

       void Insert(data_rec*);

       void Remove(int);

};

void SchoolList::print()

{

       ListItem *p = head;

       while(p)

       {

              p->item->print();

              p=p->next;

       }

}

void SchoolList::Insert(data_rec* pn)

{

       ListItem* n= new ListItem(pn);

       if (!head) head = n;

       else

       {

              n->next = head;

              head = n;

       }

}

void SchoolList::Remove(int id)

{

       ListItem* p =head;

       if (head->item->getID()==id)

       {

              head = p->next;

              delete p;

       }

       else

       {

              ListItem* prev =head;

              p = p->next;

              while(p)

              {

                     if (p->item->getID()==id)

                     {

                            prev->next = p->next;

                            delete p;

                            break;

                     }

                     else

                     {

                            prev = p;

                            p=p->next;

                     }

              }

       }

}

void main()

{

       Student stu(001,"王一",19850101,0,3);

       Staff sta(002,"王二",19701212,1,"教务处");

       Teacher tea(003,"王三",19500212,0,500);

       SchoolList TestList;

       TestList.Insert((data_rec*)&stu);

       TestList.Insert((data_rec*)&sta);

       TestList.Insert((data_rec*)&tea);

       cout <<"Before Test Remove function:" <<endl;

       TestList.print();

       cout <<"/n/nAfter Test Remove function:" <<endl;

       TestList.Remove(002);

       TestList.print();

}

阅读更多

没有更多推荐了,返回首页