问题遇到的现象和发生背景
是教材上的一个题目,要求用链表实现,写了好长时间了写不出来人的麻了。
成绩的排序都解决的,但是题目里要求如果成绩相同则并排输出且输出该成绩的人数
输出这一块真的写不出来。
用代码块功能插入代码,请勿粘贴截图
//定义学生信息结构
#include <stdio.h>
#include <malloc.h>
struct student
{
int id;
int score;
struct student* next;
};
typedef struct student Info;
create(Info* head)
{
Info* p, * q;
int id, score;
int flag = 1, no = 1;
printf("Input Info\n");
printf("Enter ID = -1 -> end\n");
do
{
printf("Student %d\n", no);
no++;
printf("ID:\t");
scanf("%d", &id);
printf("SOCRE:\t");
scanf("%d", &score);
//如果id输入为-1,则停止创建结点
if (id == -1)
{
flag = 0;
}
else
{
p = (Info*)malloc(sizeof(Info));
p->id = id;
p->score = score;
q = head;
while (q->next != NULL)
{
if ((p->score) > (q->next->score))
{
break;
}
else
{
q = q->next;
}
}
p->next = q->next;
q->next = p;
}
} while (flag);
}
output(Info* head)
{
Info* p;
int i = 0, prv, k = 0, rank = 1;
p = head->next;
while (p -> next != NULL)
{
prv = p->score;
printf("NO.%d\t", rank);
printf("SCORE: %d\t", p->score);
if (p->score != p->next->score)
{
printf("PEOPLE: 1\t");
printf("%d\n", p->id);
}
else
{
printf("%d\t", p->id);
}
p = p->next;
}
}
void main()
{
Info* head;
head = (Info*)malloc(sizeof(Info));
head->next = NULL;
create(head);
output(head);
}
output(Info* head)
{
Info* p;
int i = 0, prv, k = 0, rank = 1;
p = head->next;
while (p -> next != NULL)
{
prv = p->score;
printf("NO.%d\t", rank);
printf("SCORE: %d\t", p->score);
if (p->score != p->next->score)
{
printf("PEOPLE: 1\t");
printf("%d\n", p->id);
}
else
{
printf("%d\t", p->id);
}
p = p->next;
}
}
void main()
{
Info* head;
head = (Info*)malloc(sizeof(Info));
head->next = NULL;
create(head);
output(head);
}
我想要达到的结果
output那个函数我不知道怎么写