争光ONE 2014-03-30 13:36 采纳率: 0%
浏览 2272

在类中使用单链表,对象该怎么放到节点中?

我用的是基本的单链表思想,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;

}

  • 写回答

1条回答 默认 最新

  • 陈召东 2014-04-01 04:56
    关注

    你是想 把这个三个学生穿起来吧, 直接 l->next = stu[0];
    stu[0].next = stu[1];
    stu[1].next = stu[2];
    stu[2].next = null;

    评论

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波