異質(zhì)節(jié)點鏈表,就是說對于不是同一個類型的節(jié)點,也讓它們構(gòu)成一個鏈表,而且可以通過遍歷指針的方法來實現(xiàn)遍歷,輸出,插入,刪除等。
此處我解決的辦法是:進行抽象,得到它們的共性,然后將它們放在一個公共的基類中,定義接口,即虛函數(shù),然后這些類的訪問,都可以通過虛函數(shù)來實現(xiàn),就是基類的指針作為遍歷指針,指向?qū)嶋H的對象,然后就可以實現(xiàn)輸出各個節(jié)點的信息了。
#pragma warning(disable:4996)

#include <iostream>

using namespace std;
const int MAX = 25;


class Base
{
public:
char name[MAX];
int age;
char social_Num[MAX];

virtual void print()
{}

virtual void set_next(Base *p)
{}

virtual Base *get_next()
{ return NULL;}
};


class Student:public Base
{
char grad[MAX];
double average_score;
public:
Base *next;

Student(char *name,int age1,char *social,char *grad,double average):next(NULL)
{
strcpy(this->name,name);
this->age = age1;
strcpy(this->social_Num,social);
strcpy(this->grad,grad);
this->average_score = average;
}

void set_next(Base *p)
{ next = p;}

Base *get_next()
{ return this->next; }

void print()
{
cout << "姓名: "<<this->name << "\t年齡:" << this->age << "\n社會保險號:"
<< this->social_Num << "\t年級:" << this->grad << "\t\t平均成績:" << this->average_score << endl;
}
};


class Clerk:public Base
{
double wage;
public:
Base *next;

Clerk(char *name,int age1,char *social,double wage1):next(NULL)
{
strcpy(this->name,name);
this->age = age1;
strcpy(this->social_Num,social);
this->wage = wage1;
}

void set_next(Base *p)
{ next = p;}

Base *get_next()
{ return this->next;}

void print()
{
cout << "姓名: "<<this->name << "\t年齡:" << this->age << "\n社會保險號:"
<< this->social_Num << "\t工資:" << this->wage << endl;
}
};


class Professer:public Base
{
double wage;
char major[MAX];
public:
Base *next;

Professer(char *name,int age1,char *social,double wage1,char *major1):next(NULL)
{
strcpy(this->name,name);
this->age = age1;
strcpy(this->social_Num,social);
this->wage = wage1;
strcpy(this->major,major1);
}

void set_next(Base *p)
{ next = p;}

Base *get_next()
{ return this->next;}

void print()
{
cout << "姓名: "<<this->name << "\t年齡:" << this->age << "\n社會保險號:"
<< this->social_Num << "\t工資:" << this->wage << "\t研究方向:" << this->major << endl;
}
};



class List
{//逆序建立鏈表
Base *head;
public:

List(Base *p)
{
head = p;
p->set_next(NULL); //將插入節(jié)點域的next賦值為NULL
}

void insert(Base *p)
{
p->set_next(head); //新的節(jié)點插入前面,建立兩個節(jié)點之間的聯(lián)系,即使不是同一個類,也可以利用指針
head = p; //重置初始表頭指針
}

void delete_list(Base *p)
{
Base *tmp = head;

while(tmp->get_next()!= p)
{
tmp = tmp->get_next();
}
Base *tmp2 = tmp->get_next()->get_next();
delete tmp->get_next(); //刪除那個中間的節(jié)點
tmp->set_next(tmp2);
}

void print()
{
Base *ptmp = head;

while(ptmp->get_next()!=NULL)
{
ptmp->print();
ptmp = ptmp->get_next();
}
ptmp->print(); //輸出最后一個,因為后繼節(jié)點為NULL,所以特殊處理
}
};
//可以實現(xiàn)的是輸出,而且是滿足虛函數(shù)的特性,這里針對的是next指針和print虛函數(shù),從結(jié)果可以看到,是根據(jù)虛指針
//實際指向的單元的類型來輸出的。next指針都是Base *類型的,不過輸出的不是Base的信息(那樣就沒有輸出了),
//而是實際指向的類型,比如這里的是Clerk。
int main()


{
Student student("jack",20,"132343","g1",87.8);
Clerk clerk("Clerk",34,"23434",1000);
Professer professer("Professor",50,"234356",10000,"computer");
List aList(&student);
aList.insert(&clerk);
aList.insert(&professer);
aList.print();
return 0;
}


posted on 2010-01-26 00:46
deercoder 閱讀(970)
評論(0) 編輯 收藏 引用