鱼C论坛

 找回密码
 立即注册
查看: 1174|回复: 6

[已解决]链表学生管理为什么学号降序直接强制退出呢?

[复制链接]
发表于 2021-7-19 17:56:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. struct student{
  4.         char name[20];
  5.         int num;
  6.         int score;
  7. };
  8. struct Node{
  9.         struct student data;//自定义类型数据域data
  10.         struct Node *next;
  11. };
  12. struct Node *createlist()//创建链表
  13. {
  14.         struct Node *headNode = (struct Node*)malloc(sizeof(struct Node));
  15.         headNode->next = NULL;
  16.         return headNode;
  17. }
  18. struct Node *createNode(struct student data)//创建节点
  19. {
  20.         struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
  21.         newNode->data = data;
  22.         newNode->next = NULL;
  23.         return newNode;
  24. }
  25. void printlist(struct Node *headNode)//打印链表
  26. {
  27.         struct Node *pMove = headNode->next;
  28.         printf("name\tnum\tscore\n");
  29.         while(pMove)
  30.         {
  31.                 printf("%s\t%d\t%d\n",pMove->data.name,pMove->data.num,pMove->data.score);
  32.                 pMove = pMove->next;
  33.         }
  34. }
  35. void insertNodeByHead(struct Node *headNode,struct student data)//插入节点(头插法)
  36. {
  37.         struct Node *newNode = createNode(data);
  38.         newNode->next = headNode->next;
  39.         headNode->next = newNode;
  40. }
  41. void deleteNodeByAppoinNum(struct Node *headNode,int num)//删除节点
  42. {
  43.         struct Node *posNode = headNode->next;
  44.         struct Node *posNodeFront = headNode;
  45.         if(posNode == NULL)
  46.         {
  47.                 printf("链表为空无法删除\n");
  48.         }
  49.         else
  50.         {
  51.                 while(posNode->data.num != num)
  52.                 {
  53.                         posNodeFront = posNode;
  54.                         posNode = posNodeFront->next;
  55.                         if(posNode == NULL)
  56.                         {        
  57.                                 printf("没有找到无法删除\n");
  58.                                 break;
  59.                         }
  60.                 }               
  61.                 posNodeFront->next = posNode->next;
  62.                 free(posNode);
  63.         }
  64. }
  65. void modifyNodeByAppoinNum(struct Node *headNode,int num)//修改节点
  66. {
  67.         struct Node *posNode = headNode->next;
  68.         struct Node *posNodeFront = headNode;
  69.         if(posNode == NULL)
  70.         {
  71.                 printf("链表为空无法修改\n");
  72.         }
  73.         else
  74.         {
  75.                 while(posNode->data.num != num)
  76.                 {
  77.                         posNodeFront = posNode;
  78.                         posNode = posNodeFront->next;
  79.                         if(posNode == NULL)
  80.                         {        
  81.                                 printf("没有找到无法修改\n");
  82.                                 return;
  83.                         }
  84.                 }        
  85.                 printf("请输入学生姓名,学号,成绩:\n");
  86.                 scanf("%s%d%d",posNode->data.name,&posNode->data.num,&posNode->data.score);
  87.                 printf("修改成功!\n");
  88.         }               
  89. }
  90. void searchNodeByAppoinNum(struct Node *headNode,int num)//查找节点
  91. {
  92.         struct Node *posNode = headNode->next;
  93.         struct Node *posNodeFront = headNode;
  94.         if(posNode == NULL)
  95.         {
  96.                 printf("链表为空\n");
  97.         }
  98.         else
  99.         {
  100.                 while(posNode->data.num != num)
  101.                 {
  102.                         posNodeFront = posNode;
  103.                         posNode = posNodeFront->next;
  104.                         if(posNode == NULL)
  105.                         {        
  106.                                 printf("没有找到\n");
  107.                                 return;
  108.                         }
  109.                 }
  110.                 printf("name\tnum\tscore\n");
  111.                 printf("%s\t%d\t%d\n",posNode->data.name,posNode->data.num,posNode->data.score);
  112.         }
  113. }
  114. struct Node *DescendingByNum(struct Node *headNode)//学号降序
  115. {
  116.         struct Node *p = headNode->next;
  117.         struct Node *pre = headNode;
  118.         struct Node *temp = NULL,*tail = NULL;
  119.         
  120.         while((headNode->next->next)!=tail)
  121.         {
  122.                 while(p->next != tail)
  123.                 {
  124.                         if((p->data.num) < (p->next->data.num))
  125.                         {
  126.                                 pre->next = p->next;
  127.                                 temp = p->next->next;
  128.                                 p->next->next = p;
  129.                                 p->next = temp;
  130.                                 p = pre->next;
  131.                         }
  132.                         p = p->next;
  133.                         pre = pre->next;
  134.                 }
  135.                 tail = p;
  136.         }
  137.         return headNode;
  138. }
  139. void Save(struct Node *headNode)//写入文件
  140. {
  141.         FILE *fp = fopen("student.dat","w");
  142.         
  143.         if(headNode == NULL)
  144.                 return;
  145.         struct Node *pMove = headNode->next;
  146.         while(pMove)
  147.         {
  148.                 fprintf(fp,"%-8s%-8s%-8s\n%-8s%-8d%-8d\n","name","num","score",pMove->data.name,pMove->data.num,pMove->data.score);
  149.                 pMove = pMove->next;
  150.         }
  151.         fclose(fp);
  152.         printf("写入成功\n");
  153. }
  154. void Read(struct Node *headNode)//读取文件
  155. {
  156.         FILE *fp = fopen("student.dat","r");
  157.         char ch;
  158.         
  159.         if (fp == 0)
  160.         {
  161.                 printf("读取失败!文件不存在\n");
  162.                 return;
  163.         }
  164.         else
  165.         {
  166.                 while(EOF != (ch  =fgetc(fp)))
  167.                 {
  168.                         putchar(ch);
  169.                 }
  170.                 fclose(fp);
  171.                 printf("读取成功!\n");
  172.         }
  173. }
  174. void main()
  175. {
  176.                
  177.         char choice,type;
  178.         int run = 1,searchnum;
  179.         struct Node *list = createlist();
  180.         struct student info;
  181.         
  182.         system("mode con cols=100 lines=30");
  183.         printf("Management for Students' scores\n");
  184.     printf("1.Append record\n");
  185.         printf("2.List record\n");
  186.         printf("3.Delete record\n");
  187.         printf("4.Modify record\n");
  188.         printf("5.Search record\n");        
  189.         printf("6.Sort Score in descending order by sum\n");
  190.         printf("7.Sort Score in ascending order by sum\n");
  191.         printf("8.Sort Score in descending order by num\n");
  192.         printf("7.Sort Score in ascending order by num\n");
  193.     printf("i.Insert Sort Score in ascending order by sum\n");
  194.         printf("w.Write to a File\n");
  195.         printf("r.Read from a File\n");        
  196.         printf("0.Exit\n");
  197.         
  198.         do{
  199.                 printf("Please Input your choice:");
  200.                 fflush(stdin);
  201.                 scanf("%c",&type);
  202.                 switch(type){
  203.                 case '0':
  204.                         run = 0;
  205.                         break;
  206.                 case '1':
  207.                         printf("请输入学生姓名 学号 成绩\n");
  208.                         scanf("%s%d%d",info.name,&info.num,&info.score);
  209.                         insertNodeByHead(list,info);
  210.                         while(1)
  211.                         {
  212.                                 printf("是否继续(Y/N)?\n");
  213.                                 fflush(stdin);
  214.                                 choice = getchar();
  215.                                 if(choice == 'N'||choice == 'n')
  216.                                         break;
  217.                                 else
  218.                                         printf("请输入学生姓名 学号 成绩\n");
  219.                                         scanf("%s%d%d",info.name,&info.num,&info.score);
  220.                                         insertNodeByHead(list,info);
  221.                         }
  222.                         break;
  223.                 case '2':
  224.                         printlist(list);
  225.                         break;
  226.                 case '3':
  227.                         printf("请输入学号用于删除:\n");
  228.                         scanf("%d",&searchnum);
  229.                         deleteNodeByAppoinNum(list,searchnum);
  230.                         break;
  231.                 case '4':
  232.                         printf("请输入学号用于修改:\n");
  233.                         scanf("%d",&searchnum);
  234.                         modifyNodeByAppoinNum(list,searchnum);
  235.                         break;
  236.                 case '5':
  237.                         printf("请输入学号用于检索:\n");
  238.                         scanf("%d",&searchnum);
  239.                         searchNodeByAppoinNum(list,searchnum);
  240.                         break;
  241.                 case '6':
  242.                         break;
  243.                 case '7':
  244.                         break;
  245.                 case '8':
  246.                         DescendingByNum(list);
  247.                         break;
  248.                 case '9':
  249.                         break;
  250.                 case 'w':
  251.                         Save(list);
  252.                         break;
  253.                 case 'r':
  254.                         Read(list);
  255.                         break;
  256.                 default :
  257.                         break;               
  258.         }}while(run);         
  259. }

复制代码
最佳答案
2021-7-19 20:21:54
老K哥 发表于 2021-7-19 19:11
单纯结构体时候二层for循环好点,链表的话我勉强改成这样

可以参考这个https://blog.csdn.net/weixin_443 ... 1018.2226.3001.4187
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-19 18:46:41 | 显示全部楼层
为啥我还能正常运行

                               
登录/注册后可看大图
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-19 18:54:02 | 显示全部楼层
大马强 发表于 2021-7-19 18:46
为啥我还能正常运行

三个及以上就不行了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-19 18:55:20 | 显示全部楼层
哦哦,我发现了,我再看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-19 19:01:17 | 显示全部楼层
建议搞一个嵌套循环来排序,每次找出最大的排在前面,我先去吃饭回来再看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-19 19:11:09 From FishC Mobile | 显示全部楼层
大马强 发表于 2021-7-19 19:01
建议搞一个嵌套循环来排序,每次找出最大的排在前面,我先去吃饭回来再看看

单纯结构体时候二层for循环好点,链表的话我勉强改成这样
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-19 20:21:54 | 显示全部楼层    本楼为最佳答案   
老K哥 发表于 2021-7-19 19:11
单纯结构体时候二层for循环好点,链表的话我勉强改成这样

可以参考这个https://blog.csdn.net/weixin_443 ... 1018.2226.3001.4187
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-27 05:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表