異質(zhì)節(jié)點(diǎn)鏈表,就是說(shuō)對(duì)于不是同一個(gè)類型的節(jié)點(diǎn),也讓它們構(gòu)成一個(gè)鏈表,而且可以通過(guò)遍歷指針的方法來(lái)實(shí)現(xiàn)遍歷,輸出,插入,刪除等。
此處我解決的辦法是:進(jìn)行抽象,得到它們的共性,然后將它們放在一個(gè)公共的基類中,定義接口,即虛函數(shù),然后這些類的訪問(wèn),都可以通過(guò)虛函數(shù)來(lái)實(shí)現(xiàn),就是基類的指針作為遍歷指針,指向?qū)嶋H的對(duì)象,然后就可以實(shí)現(xiàn)輸出各個(gè)節(jié)點(diǎn)的信息了。
#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社會(huì)保險(xiǎn)號(hào):"
<< this->social_Num << "\t年級(jí):" << this->grad << "\t\t平均成績(jī):" << 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社會(huì)保險(xiǎn)號(hào):"
<< 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社會(huì)保險(xiǎn)號(hào):"
<< 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é)點(diǎn)域的next賦值為NULL
}

void insert(Base *p)
{
p->set_next(head); //新的節(jié)點(diǎn)插入前面,建立兩個(gè)節(jié)點(diǎn)之間的聯(lián)系,即使不是同一個(gè)類,也可以利用指針
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(); //刪除那個(gè)中間的節(jié)點(diǎn)
tmp->set_next(tmp2);
}

void print()
{
Base *ptmp = head;

while(ptmp->get_next()!=NULL)
{
ptmp->print();
ptmp = ptmp->get_next();
}
ptmp->print(); //輸出最后一個(gè),因?yàn)楹罄^節(jié)點(diǎn)為NULL,所以特殊處理
}
};
//可以實(shí)現(xiàn)的是輸出,而且是滿足虛函數(shù)的特性,這里針對(duì)的是next指針和print虛函數(shù),從結(jié)果可以看到,是根據(jù)虛指針
//實(shí)際指向的單元的類型來(lái)輸出的。next指針都是Base *類型的,不過(guò)輸出的不是Base的信息(那樣就沒(méi)有輸出了),
//而是實(shí)際指向的類型,比如這里的是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 閱讀(974)
評(píng)論(0) 編輯 收藏 引用