/*设计一个完整的程序,根据用户输入的学生人数n(n>=3)及每个学生姓名和成绩建立一个单链表,
并按照学生成绩递减排序,然后按照名次输出所有学生的姓名和成绩*/
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <string.h>
#include<stdlib.h>
struct student
{
char name[20];
int math;
int num;
};
//定义学生结点 包括姓名 成绩
typedef struct node
{
struct student data;//成绩域
struct node* next;//指针域
}StudList;
//创建链表
StudList* CreateList()
{
//创建链表
StudList* head = (StudList*)malloc(sizeof(StudList));
head->next = NULL;
return head;
}
//创建节点
StudList* CreateNode(struct student data)
{
StudList* p = (StudList*)malloc(sizeof(StudList));
p->data = data;
p->next = NULL;
return p;
}
//遍历节点
void printfList(StudList* head)
{
StudList* p = head->next;
printf("name\tnum\tmath\n");
while (p)
{
printf("%s\t%d\t%d\t", p->data.name,p->data.num,p->data.math);
p = p->next;
}
printf("\n");
}
//插入结点,插入哪个链表,插入结点的数据
void insertNodeByHead(StudList* head, struct student data)
{
//创建插入的节点
StudList* q = CreateNode(data);
q->next = head->next;
head->next = q;
}
//删除节点
void deleteNodeByNum(StudList* head, int num)
{
StudList* p = head->next;
StudList* pFront = head;
if (pFront == NULL)
printf("无法删除,链表为空");
else
{
//找不到就继续往下找
while (p->data.num != num)
{
p = p->next;
pFront = pFront->next;
//找到了表尾
if (p == NULL)
{
printf("没找到相关信息\n");
return;
}
}
//当找到了进行删除
pFront->next = p->next;
free(p);
}
}
int main()
{
StudList* list = CreateList();
struct student info;
while (1)
{
printf("请输入学生姓名 学号 成绩:");
setbuf(stdin, NULL);
scanf("%s%d%d",info.name,&info.num,&info.math);
insertNodeByHead(list, info);
printf("continue(Y/N)?\n");
setbuf(stdin, NULL);
int choice = getchar();
if (choice == 'N' || choice == 'n')
{
break;
}
}
printfList(list);
system("pause");
return 0;
}
07-07
1465
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-15
03-07
1764
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-29