鱼C论坛

 找回密码
 立即注册
查看: 2875|回复: 0

[技术交流] 刚学链表写了一个关于链表的插入与删除不足的请指教

[复制链接]
发表于 2014-8-1 10:44:54 | 显示全部楼层 |阅读模式

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

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

x
刚学链表写了一个关于链表的插入与删除不足的还请指教
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>
  4. #define LN sizeof(struct student)        //大小
  5. struct student *creat();                 //输入与插入
  6. struct student *del(struct student *head);//删除
  7. void print (struct student *head);        //打印
  8. struct student *head=NULL;               
  9. int n=0;
  10. struct student
  11. {
  12.         long num;
  13.         float score;
  14.         struct student *next;
  15. };
  16. main()
  17. {
  18.         struct student *stu;
  19.         int i=0;
  20.         printf("*************************");
  21.         printf("   欢迎使用学生成绩录入系统");
  22.         printf("************************\n");
  23.         printf("建立与插入请按  1 \n删除请按        2\n打印请按        3\n结束请按        0\n");
  24.         scanf("%d",&i);
  25.         while(i)
  26.         {
  27.                 switch(i)
  28.                 {
  29.                 case 1:
  30.                         stu=creat();
  31.                         break;
  32.                 case 2:
  33.                         stu=del(stu);
  34.                         break;
  35.                 case 3:
  36.                         print(stu);
  37.                         break;
  38.                 }
  39.                 printf("建立与插入请按1\n删除请按2\n打印请按3\n结束请按0\n");
  40.                 scanf("%d",&i);
  41.         }
  42. }
  43. struct student *creat()  //建立与插入
  44. {
  45.         struct student *p1,*p2,*p3;
  46.         p3=(struct student *)malloc(LN);
  47.         n++;
  48.         printf("请输入第%d学生的学号和成绩\n",n);
  49.         scanf("%ld%f",&p3->num,&p3->score);
  50.         while(p3->score>100||p3->score<0)
  51.         {
  52.       printf("输入成绩错误请输入第%d学生的学号和成绩\n",n);
  53.           scanf("%ld%f",&p3->num,&p3->score);

  54.         }

  55.         while(p3->num)
  56.         {
  57.         if(head==NULL)
  58.         {
  59.                 head=p3;
  60.                 n++;
  61.                 head->next=NULL;
  62.         }
  63.                 p1=head;
  64.                 while(p3->num>p1->num&&p1->next!=NULL)
  65.                 {
  66.                         p2=p1;
  67.                         p1=p1->next;
  68.                 }
  69.                 if(p3->num<p1->num)
  70.                 {
  71.                         if(p1==head)
  72.                         {
  73.                                 p3->next=head;
  74.                                 head=p3;
  75.                                 n++;
  76.                         }
  77.                         else
  78.                         {
  79.                                 p2->next=p3;
  80.                                 p3->next=p1;
  81.                                 n++;
  82.                         }
  83.                 }
  84.                 else if(head!=NULL&&p3->num>p1->num)
  85.                 {
  86.                
  87.                                 p1->next=p3;
  88.                                 p3->next=NULL;
  89.                                 n++;
  90.                 }

  91.                 p3=(struct student*)malloc(LN);
  92.                 printf("请输入第%d学生的学号和成绩:\n",n);
  93.                 scanf("%ld%f",&p3->num,&p3->score);
  94.                 while(p3->score>100||p3->score<0)
  95.                 {
  96.                 printf("输入成绩错误请输入第%d学生的学号和成绩:\n",n);
  97.                 scanf("%ld%f",&p3->num,&p3->score);

  98.                 }
  99.         }
  100.        
  101.         return head;
  102. }
  103. struct student *del(struct student *head)   //删除
  104. {
  105.         struct student *p1,*p2;
  106.         long num;
  107.         if(head==NULL)
  108.         {
  109.                 printf("这是一个空链表\n");
  110.                 return head;
  111.         }
  112.         print(head);
  113.         printf("请输入上表要删除的学生学号:\n");
  114.         scanf("%ld",&num);
  115.         while(num)
  116.         {
  117.                 p1=head;
  118.                 while(p1->num!=num&&p1->next!=NULL)
  119.                 {
  120.                         p2=p1;
  121.                         p1=p1->next;
  122.                 }
  123.                 if(p1->num==num)
  124.                 {
  125.                         if(p1==head)
  126.                         {
  127.                                 head=p1->next;
  128.                                 n--;
  129.                         }
  130.                         else
  131.                         {
  132.                                 p2->next=p1->next;
  133.                                 n--;
  134.                         }
  135.                 }
  136.                 else
  137.                 {
  138.                         printf("没有符合删除条件的学生\n");
  139.                 }
  140.                 printf("请输入要删除的学生学号:\n");
  141.                 scanf("%ld",&num);
  142.         }
  143.         printf("删除后列表如下:\n");
  144.         p1=head;
  145.         while(p1)
  146.         {
  147.                 printf("%5d%6.1lf\t\n",p1->num,p1->score);
  148.                 p1=p1->next;
  149.         }
  150.         return head;
  151.        
  152. }
  153. void print(struct student *head)    //打印
  154. {
  155.         struct student *p;
  156.         p=head;
  157.         if(head==NULL)
  158.         {
  159.                 printf("没有可以查看的内容\n");
  160.         }
  161.         else
  162.         {
  163.                 printf("整理后列表如下:\n");
  164.                 while(p)
  165.                 {
  166.                         printf("%5ld%6.1f\t\n",p->num,p->score);
  167.                         p=p->next;
  168.                 }
  169.         }
  170. }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-11 10:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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