23作业2 c++基础2编程题7-6成绩大于等于某值的学生信息输出

文章描述了一个编程问题,要求在C++中创建一个单向链表来存储学生信息,输入学生成绩,然后删除分数低于给定值的节点,并输出所有成绩大于等于该值的学生信息。作者给出了链表结构定义和相关函数实现。
摘要由CSDN通过智能技术生成

7-6 成绩大于等于某值的学生信息输出

分数 10

全屏浏览

切换布局

作者 王秀

单位 福州大学

输入若干个学生信息(包括学号、姓名和成绩),输入学号为0时输入结束,建立一个单向链表,再输入一个成绩值,将成绩大于等于该值的学生信息输出。

提示:

定义函数struct stud_node *Creat_Stu_Doc()完成创建链表

定义函数struct stud_node *DeleteDoc(struct stud_node *head,int min_score)将分数低于min_score的结点删除

定义函数void Ptrint_Stu_Doc(struct stud_node *head)打印链表

输入输出示例:括号内为说明,无需输入输出

输入样例:

1 zhang 78
2 wang 80
3 li 75
4 zhao 85
0
80

输出样例:

2 wang 80
4 zhao 85

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

我的解答:

#include<iostream>
#include<string>
using namespace std;

//定义框框
struct ListNode {
    int num;
    string name;
    int grade;
    ListNode* next;
    ListNode(int x, string y, int z) :
        num(x), name(y), grade(z), next(nullptr){}
};

class LinkedList {
public:
    ListNode* head;

    LinkedList() {
        head = NULL;
    }

    void append(int num,string name,int grade) {
        ListNode* newNode = new ListNode(num, name, grade);
        if (!head) {
            head = newNode;
            return;
        }
        ListNode* current = head;
        while (current->next) {
            current = current->next;
        }
        current->next = newNode;
    }

    void input() {
        int num;
        string name;
        int grade;
        while (cin >> num  && num != 0){
            cin >> name>> grade;
            append(num, name, grade);
        }
    }

    void deleteNode(ListNode*& head, int grade) {
        if (!head) {
            return;
        }
        if (head->grade == grade) {
            ListNode* temp = head;
            head = head->next;
            delete temp;
            return;
        }
        ListNode* current = head;
        while (current->next) {
            if (current->grade == grade) {
                ListNode* temp = current->next;
                current->next = current->next->next;
                delete temp;
                return;
            }
            current = current->next;
        }
    }

    void deletelow(int score) {
        while (head && head->grade < score) {
            deleteNode(head, head->grade);
        }
        ListNode* current = head;
        while (current && current->next) {
            if (current->next->grade < score) {
                deleteNode(current->next, current->next->grade);
            }
            else {
                current = current->next;
            }
        }
    }

    void print() {
        ListNode* current = head;
        while (current) {
            cout << current->num << " ";
            cout << current->name << " ";
            cout << current->grade << endl;
            current = current->next;
        }
    }
};

int main() {
    LinkedList list;
    int score;
    list.input();
    cin >> score;
    list.deletelow(score);
    list.print();
    return 0;
}

和7-7类似

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值