异质链表

#include 
#include 
/*
  Name: 异质链表 
  Copyright: 1.0
  Author: 田保兴 
  Date: 01/11/13 17:21
  Description:  Graduate(研究生)继承类Student,Student(学生)继承 Person,Person是基础类。
  List保存person。 
*/

using namespace std;

//类声明完毕应该加分号
class Person{//最基础类 
      private:
              friend class List;
      protected:
                char name[20]; //姓名 
                int age;//年龄 
                char address[20];//地址 
                char tel[20];//手机号码 
                static Person *ptr;
                Person *next;
      public:
             Person(char *,int,char *,char *);
             virtual void print(); //virtual 虚函数 ,virtual主要是为了以后能实现它。主要是为了继承 
             virtual void insert()=0;
      }; 
class Student:public Person{ //学生 
      private:
              friend class List;
      protected:
              int level;//年级 
              char school[20];//学校 
      public:
             Student(char *,int ,char *,char *,int ,char *); 
             void print();
             void insert();
      }; 
class Graduate:public Student{//研究生 
      private:
              friend class List;
              char field[20] ;//研究领域
      public: 
              Graduate(char *,int ,char *,char *,int ,char *,char *); 
              void print();
              void insert();
      };

class List {//异质链表
      private:
              Person *root;//根节点 
      public:
             List(){
                    root=0;
                    } 
             void add(Person *node);
             void remove(char *name); 
             void show();
      
      };
Person::Person(char *name,int age,char *address,char *tel){
                    strcpy(Person::name,name);
                    Person::age=age;
                    strcpy(Person::address,address);
                    strcpy(Person::tel,tel);  
                    next=0;                  
                    }
void Person::print(){
       cout<<"\nname:"<name);
    Person *curr_node=root;//Person类是List类的友元,故可以直接访问
    Person *previous=0;
    while(curr_node!=0&&strcmp(curr_node->name,key)<0)
    {
        previous=curr_node;
        curr_node=curr_node->next;
    }
    node->insert();
    node->ptr->next=curr_node;
    if(previous==0)
    {
        root=node->ptr;//根指针指向新节点
    }
    else
    {
        previous->next=node->ptr;
    }

}

void List::remove(char *name)
{
    Person *curr_node=root;
    Person *previous=0;//记录之前结点
    while(curr_node!=0&&strcmp(curr_node->name,name)!=0)
    {
        previous=curr_node;
        curr_node=curr_node->next;
    }

    if(curr_node!=0&&previous==0)
    {
        root=curr_node->next;
        delete curr_node;
    }//是根结点的情况
    else if(curr_node!=0&&previous!=0)
    {
        previous->next=curr_node->next;
        delete curr_node;
    }
}

void List::show()
{
    Person *cur=root;
    while(cur!=0)
    {
        cur->print();
        cur=cur->next;
    }
}


Person *Person::ptr=0;      
int main(int argc, char *argv[])
{
    List list;
    Student stu("田强强",25,"河北","188107485",3,"北航");
    list.add(&stu);
    
    Graduate gra("田保兴",25,"北京","18810743",3,"北京航空航天大学","大数据");
    list.add(&gra);
    
    list.show(); 
    system("PAUSE");
    return EXIT_SUCCESS;
}

转载于:https://my.oschina.net/winHerson/blog/173488

你可能感兴趣的:(异质链表)