鱼C论坛

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

建立链表

[复制链接]
发表于 2013-1-1 02:07:14 | 显示全部楼层 |阅读模式

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

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

x
看鱼哥(看很多人这么叫,就跟着叫了)的教程,这个链表,是看着他教的的时候跟着他的思路写的,跟抄袭差不多,不知道怎么回事一个函数出错了,这个是看了看流程表自己dele()函数写的,请各位帮忙看看怎么样,先谢了!!
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>
  4. #define LEN sizeof(struct stu)  //stu 结构的大小
  5. int n;

  6. struct stu *creat();  //创建链表
  7. struct stu *dele();
  8. void print(struct stu *head);
  9. struct stu
  10. {
  11.         int num;
  12.         float score;
  13.         struct stu *next;
  14. }

  15. struct stu *creat() //这里出错了 !!
  16. {
  17.   struct stu *head;
  18.   struct stu *p1 ,*p2;
  19.   head=NULL;
  20.   n=0;
  21.   p1=p2=(struct stu *) malloc(LEN);
  22.   printf("input student id:");
  23.   scanf("%d",&p1->num);
  24.   printf("input student score:");
  25.   scanf("%f",&p1->score);
  26.   while(p1->num)
  27.   {
  28.           n++;
  29.           if(1==n)
  30.           {
  31.                   head=p1;
  32.           }
  33.           else
  34.           {
  35.                   p2->next=p1;
  36.           }
  37.           p2=p1;
  38.           p1=(struct stu *) malloc(LEN);
  39.           printf("input student id:");
  40.           scanf("%d",&p1->num);
  41.           printf("input student score:");
  42.           scanf("%f",&p1->num);
  43.   }
  44.   p2->next=NULL;
  45.   return head;
  46. }
  47. int main ()
  48. {
  49.         int i;
  50.         struct stu *stu;
  51.         stu=creat();
  52.         print(stu);
  53.         printf("\n");
  54.         printf("input delete student id:\n");
  55.         scanf("%d",&i);
  56.     struct stu *dele(int i);
  57.         system("pause");
  58.         return 0;
  59. }
  60. void print(struct stu *head)
  61. {
  62.         struct stu *p;
  63.     p=head;
  64.         if(head)
  65.         {
  66.                 do
  67.                 {
  68.                         printf("学号为%d的成绩是:%f\n",p->num,p->score);
  69.                         p=p->next;
  70.                 }while(p);
  71.         }

  72. }
  73. struct stu *dele(int i)
  74. {
  75.         struct stu *head;
  76.         struct stu *p,*p1;
  77.         p=head;
  78.         if(p->next)
  79.         {
  80.                 while(p->next)
  81.                 {
  82.                    p1=p;
  83.            p=p->next;
  84.                    printf("学号:%d 成绩:%f\n",p->num,p->score);
  85.            while(p1->num!=i&&p->next!=NULL)
  86.                    {
  87.                         p=head;
  88.                         p1=p;
  89.                         if(1==n)
  90.                         {
  91.                            head=p->next;
  92.                         }
  93.                         else if(p->next)
  94.                         {
  95.                                 p1->next=NULL;
  96.                         }
  97.                         else
  98.                         {
  99.                                 p1->next=p->next;
  100.                         }
  101.                    }
  102.                 }
  103.                
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-8-9 20:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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