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类似