1、有一个学生结构体,其数据成员有:学号,姓名,3 门课程。从键盘上输入5 个学生
的信息。要求输出:
(1) 按照学号递增输出全部学生信息,每个学生的信息一行。(格式:学号姓名分数
1 分数2 分数3 总分)
(2) 输出每门课程最高分的学生的信息
(3) 输出每门课程的平均分
(4) 按照总分输出学生排名
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
int student_ID;
char name[20];
int math;
int chemistry;
int physics;
int total;
}student_t, *p_student;
int compare1(p_student p1, p_student p2)
{
return p1->student_ID > p2->student_ID;
}
void output_by_ID(p_student p) {
printf("student_ID name math chemistry physics total\n");
qsort(p, 5, sizeof(student_t), compare1);
for (int i = 0; i < 5; i++)
{
printf("%d %10s %3d %5d %8d %8d\n",p[i].student_ID, p[i].name, p[i].math, p[i].chemistry, p[i].physics, p[i].total);
}
return;
}
int compare2_1(p_student p1, p_student p2)
{
return p1->math > p2->math;
}
int compare2_2(p_student p1, p_student p2)
{
return p1->chemistry > p2->chemistry;
}
int compare2_3(p_student p1, p_student p2)
{
return p1->physics > p2->physics;
}
void output_by_highest_score(p_student p)
{
qsort(p, 5, sizeof(student_t), compare2_1);
printf("%d %s math:%d\n", p[4].student_ID, p[4].name, p[4].math);
qsort(p, 5, sizeof(student_t), compare2_2);
printf("%d %s chemistry:%d\n", p[4].student_ID, p[4].name, p[4].chemistry);
qsort(p, 5, sizeof(student_t), compare2_3);
printf("%d %s physics:%d\n", p[4].student_ID, p[4].name, p[4].physics);
}
void output_by_average_score(p_student p)
{
int math = 0;
int chemistry = 0;
int physics = 0;
for (int i = 0; i < 5; i++)
{
math += p[i].math;
chemistry += p[i].chemistry;
physics += p[i].physics;
}
math /= 5;
chemistry /= 5;
physics /= 5;
printf("math average score: %d\n", math);
printf("chemistry average score: %d\n", chemistry);
printf("physics average score: %d\n", physics);
return;
}
int compare3(p_student p1, p_student p2)
{
return p1->total > p2->total;
}
void output_by_total_score(p_student p)
{
qsort(p, 5, sizeof(student_t), compare3);
for (int i = 0; i < 5; i++)
{
printf("%d %10s %5d %5d %5d %5d\n", p[i].student_ID, p[i].name, p[i].math, p[i].chemistry, p[i].physics, p[i].total);
}
return;
}
int main()
{
student_t arr[5];
for (int i = 0; i < 5; i++)
{
scanf("%d %s %d %d %d", &arr[i].student_ID, arr[i].name, &arr[i].math, &arr[i].chemistry, &arr[i].physics);
arr[i].total = arr[i].math + arr[i].chemistry + arr[i].physics;
}
printf("\nOne:Output all student information according to the student_ID\n");
output_by_ID(arr);
printf("\nTwo:Output student information about highest score in each course\n");
output_by_highest_score(arr);
printf("\nThree:Output the average score for each course\n");
output_by_average_score(arr);
printf("\nFour:Output the rank of student by total score\n");
output_by_total_score(arr);
return 0;
}
2、用头插法建立链表。
3、用尾插法建立链表
4、建立有序的链表
5、找出链表的中间节点
6、判断单链表是否有环
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int number;
struct node* pnext;
}listnode, *plistnode;
void listHeadInsert(plistnode *head, plistnode *tail, int num)
{
plistnode pnew = (plistnode)calloc(1, sizeof(listnode));
pnew->number = num;
if (NULL == *head) {
*head = pnew;
*tail = pnew;
}
else {
pnew->pnext = *head;
*head = pnew;
}
return;
}
void listTailInsert(plistnode* head, plistnode* tail, int num)
{
plistnode pnew = (plistnode)calloc(1, sizeof(listnode));
pnew->number = num;
if (NULL == *head) {
*head = pnew;
*tail = pnew;
}
else {
(*tail)->pnext = pnew;
*tail = pnew;
}
return;
}
void listSortInsert(plistnode* head, plistnode* tail, int num)
{
plistnode prehead = *head;
plistnode curhead = *head;
plistnode pnew = (plistnode)calloc(1, sizeof(listnode));
pnew->number = num;
if (NULL == curhead) {
*head = pnew;
*tail = pnew;
}
else if (curhead->number >= num) {
pnew->pnext = *head;
*head = pnew;
}
else {
while (curhead) {
if (curhead->number >= num) {
prehead->pnext = pnew;
pnew->pnext = curhead;
break;
}
prehead = curhead;
curhead = curhead->pnext;
}
if (NULL == curhead) {
prehead->pnext = pnew;
prehead = pnew;
}
}
return;
}
void FindlistMiddleNode(plistnode head) {
plistnode slowptr = head;
plistnode fastptr = head;
plistnode ptr = NULL;
while (fastptr) {
if (fastptr->pnext != NULL) {
fastptr = fastptr->pnext->pnext;
}
else {
fastptr = fastptr->pnext;
}
ptr = slowptr;
slowptr = slowptr->pnext;
}
int value = ptr->number;
printf("The value of middle node: %d\n", value);
}
void JudgeListExistCircle(plistnode head) {
plistnode slowptr = head;
plistnode fastptr = head;
while (fastptr) {
if (fastptr->pnext != NULL) {
fastptr = fastptr->pnext->pnext;
}
else {
fastptr = fastptr->pnext;
}
slowptr = slowptr->pnext;
if (fastptr == slowptr) {
printf("list exist circle\n");
return;
}
}
printf("list not exist circle\n");
}
void listNodePrint(plistnode head)
{
while (head) {
printf("%d -> ", head->number);
head = head->pnext;
}
printf("NULL\n");
return;
}
int main()
{
int num;
plistnode phead = NULL;
plistnode ptail = NULL;
while (scanf("%d", &num) != EOF)
{
listHeadInsert(&phead,&ptail,num);
//listTailInsert(&phead,&ptail,num);
//listSortInsert(&phead, &ptail, num);
}
listNodePrint(phead);
FindlistMiddleNode(phead);
JudgeListExistCircle(phead);
return 0;
}
//int main()
//{
// plistnode head1 = (plistnode)calloc(1, sizeof(listnode));
// plistnode head2 = (plistnode)calloc(1, sizeof(listnode));
// plistnode head3 = (plistnode)calloc(1, sizeof(listnode));
// plistnode head4 = (plistnode)calloc(1, sizeof(listnode));
// plistnode head5 = (plistnode)calloc(1, sizeof(listnode));
// head1->pnext = head2;
// head2->pnext = head3;
// head3->pnext = head4;
// head4->pnext = head5;
// head5->pnext = head2;
// JudgeListExistCircle(head1);
// return 0;
//}