我用的是基本的单链表思想,Student是我的类,我把“Student next;”直接写在Student类里面,但是我不知道怎么把对象放到节点上。这是我写的类的单链表:
/*Student *L, *s, *r;
L = (Student *) malloc (sizeof(Student));
r = L;
s = (Student *) malloc (sizeof(Student));
s->/这里不知道该写什么?*/ = stu[0];
r->next = s;
r = s;
r->next = NULL;*/
我不明白用什么来存储对象?
完整的代码:
#include
#include
#include
using namespace std;
//学生类的定义
class Student{
public: //外部接口,公有成员函数
Student(int stuNumber,string stuName,string stuGender,string stuSchool = "USST"); //构造函数
void showStudent();
Student *next;
private: //私有数据成员
int number; //学号
string name; //姓名
string gender; //性别
string school; //学校
};
//学生类成员函数的具体实现
Student::Student(int stuNumber,string stuName,string stuGender,string stuSchool){
number = stuNumber;
name = stuName;
gender = stuGender;
school = stuSchool;
}
inline void Student::showStudent(){
cout<<"\t"<<number<<"\t"<<name<<"\t"<<gender<<"\t"<<school<<endl;
}
//主函数
int main(){
cout<<"\t学号\t\t姓名\t性别\t学校"<<endl;
//对象数组
Student stu[3] = {Student(1219010635,"孟航","男"),Student(1219010623,"余志权","男"),Student(1219010627,"闵闯","男")};
for (int i = 0;i < 3;i ++){
stu[i].showStudent();
}
/*Student *L, *s, *r; //类的单链表如何实现?
L = (Student *) malloc (sizeof(Student));
r = L;
s = (Student *) malloc (sizeof(Student));
s->/*这里不知道该写什么?*/ = stu[0];
r->next = s;
r = s;*/
return 0;
}
你是想 把这个三个学生穿起来吧, 直接 l->next = stu[0];
stu[0].next = stu[1];
stu[1].next = stu[2];
stu[2].next = null;