C++新手,刚学完链表的作业。
请大佬们麻烦看一下问题出在哪儿了,谢谢了~
#include<iostream>
using namespace std;
class student
{
public:
char name;
float score;
student* next;
};
student* cin_data(int n)//建立链表
{
student* head, * pre, * p;
head = new student;
head->next = NULL;
pre = head;
for (int i = 0; i < n; i++)
{
p = new student;
cout << "输入第 " << i + 1 << " 位学生的名字:" << endl;
cin >> p->name;
cout << "输入第 " << i + 1 << " 位学生的成绩:" << endl;
cin >> p->score;
p->next = NULL;
pre->next = p;
pre = p;
}
return head;
}
void del(student* head, int n)//删除函数
{
student* pre, * p;
pre = head;
p = pre->next;
while (p != NULL)
{
if (p->score < 60)
{
pre->next = p->next;
delete(p);
n = n - 1;
p = pre->next;
}
else
{
pre = p;
p = p->next;
}
}
}
void change(student* head, int m,char a[],float b[])//重新建立数组
{
student* p = head->next;
for (int i = 0; i < m; i++)
{
a[i] = p->name;
b[i] = p->score;
p = p->next;
}
}
void sort(student* head, int x,char c[],float d[])//x为删除后的人数。
{
int i, j;
char name;
float score;
student* p = head->next;
student* pre = p->next;
for (int i = 0; i < x - 1; i++)
{
for (int j = 0; j < x - i - 1; j++)
{
if (d[j]>d[j+1])
{
score = d[j + 1];
d[j + 1] = d[j];
d[j] = score;
name = c[j + 1];
c[j + 1] = c[j];
c[j] = name;
}
}
}
}
void renew(student* head, int m, char c[], float d[])
{
student* p = head->next;
while (p != NULL)
{
for (int i = 0; i < m; i++)
{
p->name = c[i];
p->score = d[i];
p = p->next;
}
}
}
int main()
{
int m,n;
char a[10];
float b[10];
cout << "输入总人数:" << endl;
cin >> m;
student* h = cin_data(m), * p;
del(h, m);
n = m;//记录及格人数
change(h, n, a, b);
sort(h, n, a, b);
renew(h, n, a, b);
p = h->next;
while (p != NULL)
{
cout << "姓名:" << p->name;
cout << "成绩:" << p->score;
p = p->next;
}
return 0;
}