博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C语言习题:根据struct某一成员变量对struct进行排序,头插法和尾插法建立链表,找出链表中间结点,判断链表是否存在环
阅读量:2432 次
发布时间:2019-05-10

本文共 5812 字,大约阅读时间需要 19 分钟。

1、有一个学生结构体,其数据成员有:学号,姓名,3 门课程。从键盘上输入5 个学生

的信息。要求输出:
(1) 按照学号递增输出全部学生信息,每个学生的信息一行。(格式:学号姓名分数
1 分数2 分数3 总分)
(2) 输出每门课程最高分的学生的信息
(3) 输出每门课程的平均分
(4) 按照总分输出学生排名

#define _CRT_SECURE_NO_WARNINGS#include 
#include
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 
#include
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;//}

转载地址:http://yxxmb.baihongyu.com/

你可能感兴趣的文章
【Java】【算法】——算法篇
查看>>
【Java】【数据库】知识重点——数据库篇
查看>>
【Java】知识重点——消息队列篇
查看>>
【Java】学习总结 —— HashMap之put()方法实现原理
查看>>
【计算机网络】【TCP】如何讲清楚Tcp的三次握手和四次挥手?
查看>>
【Java】-- Java核心知识点总结
查看>>
【数据库】SQL之重点知识点总结
查看>>
【计算机网络】计算机网络知识总结
查看>>
【Java】【Web】JavaWeb相关知识总结 2018-9-17
查看>>
【数据库】突破单一数据库的性能限制——数据库-分库分表总结 2018-9-20
查看>>
Slurm——作业调度处理
查看>>
Lustre 维护
查看>>
Lustre—磁盘配额测试
查看>>
SSH加密密码中的非对称式密码学
查看>>
Mac Redis安装入门教程
查看>>
python3安装教程配置配置阿里云
查看>>
Mac快捷键和实用技巧
查看>>
Git的多人协作和分支处理测试
查看>>
mysql索引回表
查看>>
iterm2 保存阿里云登陆并防止断开连接
查看>>