#pragma once
#ifndef _LIST_H_
#define _LIST_H_
#include <string>
using namespace std;
struct Student_data
{
string number;
int score;
Student_data* next;
};
class Student
{
public:
Student_data* stulist(Student_data* );
void stumean(Student_data*);
};
Student_data* Student::stulist(Student_data * top)
{
Student_data * star;//记录指针,哨兵
star = top; //头指针不放数据,哨兵指针指向头指针
while (1)
{
Student_data *p = new Student_data;
cout << "输入学号:" << endl;
cin >> p->number;
if (p->number == "#")
{
break;
}
cout << "输入分数:" << endl;
cin >> p->score;
star->next = p; //star指向top,将p空间放于top->next内
star = p; //当前star指向目前p,对于下一次就是上一个位置,
}
star->next = NULL;
return top;
}
void Student::stumean(Student_data* top)
{
Student_data* p = top;
p = p->next;
double sum = 0;
int i = 0;
while (p != NULL)
{
i++;
sum = sum + p->score;
p = p->next;
}
cout << "平均值:" << sum / i << endl;
}
#endif // _LIST_H_
int main()
{
Student_data* top = new Student_data;
Student a;
a.stulist(top);
a.stumean(top);
return 0;
}
本文介绍了一个使用C++实现的学生数据链表操作案例,包括创建链表、输入学生学号和分数,以及计算所有学生分数的平均值。通过具体的代码示例,展示了链表的基本操作和平均值计算的实现过程。
3691

被折叠的 条评论
为什么被折叠?



