鱼C论坛

 找回密码
 立即注册
查看: 1710|回复: 3

[已解决]报错 改了还是

[复制链接]
发表于 2015-10-13 14:40:47 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 ~风介~ 于 2015-10-13 20:16 编辑
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>

  4. #define  LEN sizeof(struct student)  //student 结构的大小

  5. struct  student *creat();
  6. struct  student *del(struct student *head,int num);       //del 函数用于删除结点, *head 即是链表
  7. struct  student *add(struct student *head,struct student *stu_2);  //需要被插入的链表  
  8.                                                                     // 待插入的结构的地址
  9. void print(struct student *head);  //打印链表

  10. struct student
  11. {
  12.         int num;
  13.         float score;
  14.         struct  student *next;

  15. };

  16. int n;

  17. void main()
  18. {

  19.         struct student *stu,*p,stu_2;
  20.         int n;

  21.         stu = creat();
  22.         p = stu;
  23.         print(p);

  24.         printf("Please enter the num to delete:");
  25.         scanf("%d",&n);
  26.         print(del (p,n) );

  27.     printf("Please enter the num to add:");
  28.         scanf("%d",&stu_2.num);
  29.         printf("Please input the score to add: ");
  30.         scanf("%f",&stu_2.score);

  31.         p = add(stu,&stu_2);
  32.         print(p);

  33.         printf("\n\n");
  34.         system ("pause");

  35. }

  36. struct  student *creat()
  37. {
  38.         struct student *head;
  39.         struct student *p1,*p2;

  40.         p1 = p2 = (struct student *)malloc(LEN);

  41.         printf("Please enter the num:");
  42.         scanf("%d",&p1->num);
  43.        
  44.         printf("Please enter the score:");
  45.         scanf("%f",&p1->score);
  46.        
  47.         head = NULL;

  48.         n = 0;
  49.         while (p1-> num)
  50.         {
  51.                 n++;
  52.                 if(1 == n)
  53.                 {
  54.                         head = p1;
  55.                
  56.                 }
  57.                 else
  58.                 {
  59.                         p2->next = p1;
  60.                 }
  61.                
  62.                 p2=p1;
  63.                 p1=(struct student *)malloc(LEN);

  64.                 printf("\nPlease enter the num:");
  65.                 scanf("%d",&p1->num);
  66.                 printf("Please enter the score:");
  67.                 scanf("%f",&p1->score);
  68.        
  69.        
  70.         }
  71.                 p2->next = NULL;

  72.                 return head;       
  73. }

  74. struct  student *add(struct student *head,struct student *stu_2)
  75. {
  76.         struct student *p0,*p1,*p2;

  77.         p1 = head;
  78.         p0 = stu_2;

  79.         if(NULL == head)
  80.         {
  81.        
  82.                 head = p0;
  83.                 p0->next = NULL;
  84.        
  85.         }
  86.         else
  87.         {
  88.                 while((p0->num > p1->num) && (p1->next != NULL))
  89.                 {
  90.                         p2 = p1;
  91.                         p1 = p1->next;
  92.                
  93.                 }
  94.                 if(p0->num <= p1->num)
  95.                 {
  96.                         if(head == p1)   
  97.                         {
  98.                                 head = p0;
  99.                
  100.                         }
  101.                         else
  102.                         {
  103.                                 p2->next = p0;
  104.                         }
  105.                         p0->next = p1;
  106.                
  107.                 }
  108.                 else
  109.                 {
  110.                
  111.                         p1->next = p0;
  112.                         p0->next = NULL;
  113.                 }
  114.        
  115.         }
  116.         n = n+1;

  117.         return head;
  118. }

  119. struct  student *del(struct student *head,int num)
  120. {
  121.         struct student *p1,*p2;

  122.         if(NULL == head)
  123.         {
  124.                 printf("\nThis list is null!\n");
  125.                 goto END;
  126.        
  127.         }
  128.         p1 = head;

  129.         while(p1 -> num!= num && p1->next !=NULL)
  130.         {
  131.                 p2 = p1;
  132.                 p1 = p1->next;
  133.        
  134.         }
  135.         if(num == p1->num)
  136.         {
  137.                 if(p1 == head)    //当要删除的结点位于有结点的时候
  138.                 {
  139.                         head = p1->next;
  140.                 }
  141.             else                  //一般情况
  142.                 {
  143.                
  144.                         p2->next = p1->next;
  145.                 }
  146.                 printf("\nDelete NO:%d succeed!\n",num);
  147.                 n = n-1;             //n 是作为一个全局变量,用来记录链表的数据数
  148.         }
  149.         else
  150.         {
  151.                 printf("%d not been found!\n",num);
  152.        
  153.         }
  154. END:
  155.         return head;

  156. }


  157. void print(struct student *head)
  158. {
  159.         struct student *p;
  160.         printf("\nThere are %d records!\n\n",n);
  161.        
  162.         p=head;

  163.         if(head)
  164.         {
  165.                 do
  166.                 {
  167.                         printf("学号为%d的成绩是:%f\n",p->num,p->score);
  168.                         p = p->next;
  169.                
  170.                 }while(p);
  171.         }
  172. }
复制代码
最佳答案
2015-10-13 16:05:25
楼主放关键代码啊
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2015-10-13 14:42:26 | 显示全部楼层
报错在ADD 模块
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-10-13 16:05:25 | 显示全部楼层    本楼为最佳答案   
楼主放关键代码啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-10-13 21:05:03 | 显示全部楼层
康小泡 发表于 2015-10-13 16:05
楼主放关键代码啊

报错了  没改对  
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-10 22:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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