新建单向链表

本文介绍了一个使用C++实现的学生数据链表操作案例,包括创建链表、输入学生学号和分数,以及计算所有学生分数的平均值。通过具体的代码示例,展示了链表的基本操作和平均值计算的实现过程。

#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;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值