Poweregg 2002年01月12日
请教异质链表的问题?
以下程序如何调通?错在哪里?请帮助

#include <iostream>
#include <string>
using namespace std;

class person
{
friend class list;
protected:
char name[15];
int age;
static person * ptr;
person * next;
public:
person(char *na,int a)
{
strcpy(name,na);
age=a;
};
virtual void print()
{
cout<<"\n"<<"name:"<<name;
cout<<"\n"<<"age:"<<age;
};
virtual void insert() {};
};

class student:public person
{
friend class list;
private:
int grade;
float average;
public:
student(char * na,int a,int gr,float av):person(na,a)
{
grade=gr;
average=av;
};
void print()
{
person::print();
cout<<"\n"<<"grade:"<<grade;
cout<<"\n"<<"average:"<<average;
};
void insert()
{
ptr=new student (name,age,grade,average);};
};

class teacher:public person
{
friend class list;
private:
char title[15];
char field[30];
float salary;
public:
teacher(char * na,int a,char * t,char * f,float s):person(na,a)
{
strcpy(title,t);
strcpy(field,f);
salary=s;
};
void print()
{
person::print();
cout<<"\ntitle:"<<title;
cout<<"\nfield:"<<field;
cout<<"\nsalary:"<<salary;
};
void insert()
{
ptr=new teacher (name,age,title,field,salary);
};
};

class list
{
private:
person * root;
public:
list() {root=NULL;};
void insert_p(person * p);
void remove_p(char * name);
void print_list();
};

void list::insert_p(person *p)
{
char key[15];
strcpy (key,p->name);
person *current=root;
person *previous=NULL;
while (current!=NULL && strcmp(current->name,key)<0)
{
previous=current;
current=current->next;
};
p->insert();
p->ptr->next=current;
if (previous==NULL)
root=p->ptr;
else
previous->next=p->ptr;
}

void list::remove_p(char * name)
{
person * current=root;
person * previous=NULL;
while (current!=NULL && strcmp(current->name,name)!=0)
{
previous=current;
current=current->next;
};
if (current!=NULL && previous!=NULL)
{
previous->next=current->next;
delete current;
}
else if (current!=NULL && previous==NULL)
{
root=current->next;
delete current;
};
}
void list::print_list()
{
person * p=root;
while (p!=NULL)
{
p->print();
cout<<endl;
p=p->next;
};
}
void main()
{
list man;
student stu1("liming",21,3,82);
teacher tea("wanghong",55,"professor","computer science",1200);
student stu2("zhangxy",23,4,88);
man.insert_p(&stu1);
man.insert_p(&tea);
man.insert_p(&stu2);
man.print_list();
man.remove_p("liming");
man.print_list();
}



...全文
108 点赞 收藏 6
写回复
6 条回复

还没有回复,快来抢沙发~

发动态
发帖子
VC/MFC
创建于2007-09-28

7872

社区成员

42.1w+

社区内容

VC/MFC相关问题讨论
社区公告
暂无公告