如何实现异质链表???

petit 2002-12-31 09:03:34
如何实现异质链表???有源代码更好
...全文
462 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
million 2003-01-02
  • 打赏
  • 举报
回复
这是c++中的多态性吗?
fixopen 2003-01-02
  • 打赏
  • 举报
回复
class linkElement //base class
{
};

class linkNode
{
//linkNode* pPrev; //if bilink use this field
linkElement* pElement;
linkNode* pNext;
};

//here you can define any link element(异质的)
class realLinkElement : public linkElement
{
//...
};
rushman 2003-01-02
  • 打赏
  • 举报
回复
这样:
class Person{
//friend ListPoint;
protected:
Person *next;
char *name;
int age;
int id;
public:
Person(char *name,int age,int id){
this->name=new char[strlen(name)+1];
strcpy(this->name,name);
this->age=age;
this->id=id;
}
virtual void display(){ //<----加上virtual就行了,子类中不需要明确声明,也是虚的
cout<<name<<" "<<age<<" "<<id<<endl;
}
};
rushman 2003-01-02
  • 打赏
  • 举报
回复
你把display改成虚函数就行了。
petit 2003-01-02
  • 打赏
  • 举报
回复
#include <iostream.h>
#include <string.h>
#define NULL 0
class Person;
class Student;
class ListPoint{
public:
int size;
Person *head;
Student *s;

};
class Person{
//friend ListPoint;
protected:
Person *next;
char *name;
int age;
int id;
public:
Person(char *name,int age,int id){
this->name=new char[strlen(name)+1];
strcpy(this->name,name);
this->age=age;
this->id=id;
}
void display(){
cout<<name<<" "<<age<<" "<<id<<endl;
}
};
class Student:public Person{
friend ListPoint;
protected:
int grade;
int average;
public:
Student(char *name,int age,int id,int grade,int average):Person(name,age,id){
this->grade=grade;
this->average=average;
}
void display(){
cout<<name<<" "<<age<<" "<<id<<" "<<grade<<" "<<average<<endl;

}
};
void main(){

Student stu("petit",23,1,4,88);
ListPoint lp;
lp.head=&stu;
(lp.head)->display();
}
//这段代码我想输出petit 23 1 4 88
但是却是输出petit 23 1,好像是head指针只是指向Person,不能指向Student,怎么实现异质的链接?
petit 2003-01-01
  • 打赏
  • 举报
回复
怎么就没人说话呢?
petit 2003-01-01
  • 打赏
  • 举报
回复
欢迎大家发言
rushman 2002-12-31
  • 打赏
  • 举报
回复
--Person类中的指针可以指向Staff或Perfessor的对象吗?怎么实现?
可以。只要是public派生都可以。
不过,上面这些类定义中的成员还是不要放在public中,保护起来,再提供相应的接口。

class Person{
protected:
char name[5];
int age;
int id;
Person *next;
public:
Person(..);
virtual ~Person();

// 将公用的接口定义为虚函数
virtual Person * get_next(){return(next);}
virtual Person * get_last(){....}
virtual Person * insert(Pseron * node);
virtual Person * remove();
......
};

Person * link_head;
......

Person * tnode;
link_head = new Staff(...);
tnode = new Student(...);
link_head->insert(tnode);
tnode = new Perfessor(...);
link_head->insert(tnode);
link_head->get_next()->remove();
........
petit 2002-12-31
  • 打赏
  • 举报
回复
class Person{
public:
char name[5];
int age;
int id;
Person *next;
};
class Student:public Person{
public:
int grade;
float average;
};
class Staff:public Person{
public:
float salary_hour;
};
class Perfessor:public Person{
public:
float salary_year;
};
Person类中的指针可以指向Staff或Perfessor的对象吗?怎么实现?
rushman 2002-12-31
  • 打赏
  • 举报
回复
如果,连表的使用者自己能判断对象的类型,那么用 void 指针代替具体类型指针就可以了(没有“类”的支持也可以应用)。
否则,“异质”就不能太彻底,至少要有一些共有的接口,用多态来实现它的应用。这样,让数据对象从共同的基类派生出来,用一个基类类型的指针,也可以解决。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧