You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已实现Student类,如何实现单向Student对象列表的首元素指针?

实现单向Student对象列表的正确姿势

首先明确:完全不需要在Student类里嵌入列表相关的逻辑,Student类应该只负责封装学生的属性和读写方法,链表的节点管理、列表整体操作应该交给单独的StudentList类和链表节点类来处理,这样职责更清晰,代码也更易维护。

第一步:完善你的Student类

先修正你现有代码的小问题(比如adress拼写错误,补充set方法,完善构造函数),示例如下:

student.h

#ifndef STUDENT_H
#define STUDENT_H

#include <QString>

class Student {
private:
    QString name;
    QString address;  // 修正拼写错误
    int telephone;
    int age;
    int year_of_study;
public:
    // 无参构造
    Student();
    // 带参构造
    Student(QString name, QString address, int telephone, int age, int year_of_study);
    
    // 读方法(getter)
    QString getName() const;
    QString getAddress() const;
    int getTelephone() const;
    int getAge() const;
    int getYearOfStudy() const;
    
    // 写方法(setter)
    void setName(const QString& name);
    void setAddress(const QString& address);
    void setTelephone(int telephone);
    void setAge(int age);
    void setYearOfStudy(int year_of_study);
};

#endif // STUDENT_H

student.cpp

#include "student.h"

Student::Student() 
    : name(""), address(""), telephone(0), age(0), year_of_study(0) {}

Student::Student(QString name, QString address, int telephone, int age, int year_of_study)
    : name(name), address(address), telephone(telephone), age(age), year_of_study(year_of_study) {}

// Getter实现
QString Student::getName() const { return name; }
QString Student::getAddress() const { return address; }
int Student::getTelephone() const { return telephone; }
int Student::getAge() const { return age; }
int Student::getYearOfStudy() const { return year_of_study; }

// Setter实现
void Student::setName(const QString& name) { this->name = name; }
void Student::setAddress(const QString& address) { this->address = address; }
void Student::setTelephone(int telephone) { this->telephone = telephone; }
void Student::setAge(int age) { this->age = age; }
void Student::setYearOfStudy(int year_of_study) { this->year_of_study = year_of_study; }

第二步:实现单向链表的节点类

我们需要一个链表节点来包裹Student对象,同时持有指向下一个节点的指针,这样才能形成单向链表:

studentlist.h(新增)

#ifndef STUDENTLIST_H
#define STUDENTLIST_H

#include "student.h"

// 链表节点类
class StudentNode {
public:
    Student student;
    StudentNode* next;
    
    // 构造函数
    StudentNode(const Student& stu) : student(stu), next(nullptr) {}
};

// 单向Student列表类
class StudentList {
private:
    StudentNode* head;  // 指向链表首元素的指针,这就是你要的核心成员
public:
    StudentList() : head(nullptr) {}
    ~StudentList();  // 析构函数,需要释放节点内存
    
    // 列表操作方法示例
    void addStudent(const Student& stu);  // 添加学生到链表尾部
    void printAllStudents() const;        // 遍历打印所有学生信息
    // 你还可以扩展其他方法:删除学生、查找学生等
};

#endif // STUDENTLIST_H

第三步:实现StudentList类的方法

在studentlist.cpp里实现列表的操作逻辑:

#include "studentlist.h"
#include <QDebug>

StudentList::~StudentList() {
    // 析构时释放所有节点内存,避免内存泄漏
    StudentNode* current = head;
    while (current != nullptr) {
        StudentNode* temp = current;
        current = current->next;
        delete temp;
    }
    head = nullptr;
}

void StudentList::addStudent(const Student& stu) {
    StudentNode* newNode = new StudentNode(stu);
    if (head == nullptr) {
        // 链表为空时,新节点就是头节点
        head = newNode;
    } else {
        // 找到链表尾部,添加新节点
        StudentNode* current = head;
        while (current->next != nullptr) {
            current = current->next;
        }
        current->next = newNode;
    }
}

void StudentList::printAllStudents() const {
    StudentNode* current = head;
    while (current != nullptr) {
        const Student& stu = current->student;
        qDebug() << "Name:" << stu.getName()
                 << "Address:" << stu.getAddress()
                 << "Telephone:" << stu.getTelephone()
                 << "Age:" << stu.getAge()
                 << "Year of Study:" << stu.getYearOfStudy();
        current = current->next;
    }
}

为什么要这样设计?

  • 职责分离:Student类只处理学生数据,Node类处理链表节点结构,StudentList类处理链表的整体管理,每个类只做一件事,代码更清晰易维护。
  • 解耦:如果以后要修改Student的属性,不会影响链表的逻辑;同理修改链表操作,也不会影响Student类。

使用示例

你可以在主函数里这样测试:

#include <QCoreApplication>
#include "studentlist.h"

int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);
    
    StudentList stuList;
    
    // 创建几个学生对象
    Student stu1("Alice", "123 Main St", 123456789, 20, 2);
    Student stu2("Bob", "456 Oak Ave", 987654321, 21, 3);
    
    // 添加到列表
    stuList.addStudent(stu1);
    stuList.addStudent(stu2);
    
    // 打印所有学生
    stuList.printAllStudents();
    
    return a.exec();
}

内容的提问来源于stack exchange,提问作者overflush

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:30:55