鱼C论坛

 找回密码
 立即注册
查看: 3888|回复: 1

空链表中插入节点

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

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

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

x
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #define LEN sizeof(struct student )        
  4. int n=0;//for whole
  5. struct student
  6.         {
  7.                 int num;
  8.                 float score;
  9.                 struct student *next;
  10.         };        

  11. struct student *creat();//creat declearation
  12. void print(struct student *head); //declear of print
  13. struct student *del(int i,struct student *head);
  14. struct student *insert(struct student *head,struct student *pstu2);


  15. int main(void)
  16. {
  17.         int i;// for del
  18.         struct student *head_for_del;
  19.         struct student *head_for_insert;
  20.         struct student stu;
  21.         struct student *head;
  22.         
  23.         struct student *stu2;

  24.         head=creat();
  25.         print(head);
  26.         
  27.     stu2= (struct student *)malloc(LEN);
  28.    
  29.             printf("\n\n");
  30.         printf("Now please input a new member:\n");
  31.         printf("plz input   num:");
  32.         scanf("%d",&(stu2->num));
  33.         printf("plez input  score:");
  34.         scanf("%f",&(stu2->score));
  35.                 printf("\n");
  36.         
  37.      insert(head,stu2);
  38.         
  39.         //        printf("now the inserted order is:\n");
  40.         //        print(insert(stu2,head));
  41.          print(insert(head,stu2));

  42. return 0;
  43. }


  44. struct student *creat()
  45. {
  46.         struct student *p1,*p2,*head;
  47.         head=p1=(struct student *)malloc(LEN);//len is sizeof (struct student)

  48.         
  49.         printf("plz input   num:");
  50.         scanf("%d",&(p1->num));
  51.         printf("plez input  score:");
  52.         scanf("%f",&(p1->score));
  53.         printf("\n");
  54.         
  55.         if (p1->num==0)
  56.         {
  57.         head=NULL;
  58.         n=0;
  59.         goto end;        
  60.         }
  61.         
  62. /*        while (p1->num!=0)
  63.          {  
  64.                  
  65.        p2=p1;
  66.            p1=(struct student *)malloc(LEN);
  67.            p2->next = p1;
  68.          
  69.            printf("plz input   num:");
  70.         scanf("%d",&(p1->num));
  71.         printf("plez input  score:");

  72.         scanf("%f",&(p1->score));
  73.         printf("\n");
  74.           n=n+1;         
  75.          }
  76.          */
  77.          
  78.          //p1->next=NULL;
  79.          
  80.          
  81.          end:
  82.   return head;
  83. }

  84. void print(struct student *head)
  85. {
  86.         printf("there are %d record is this groop!\n",n);
  87.         struct student *p;
  88.         p=head;
  89. //        printf("%0x",head); // 在这里的时候 head ==0   63行代码
  90.         if(head!=NULL&&p->next==NULL)
  91.         {
  92.         printf("%d\t",p->num);
  93.         printf("%f\n",p->score);
  94.         }
  95.         
  96. /*        while(head!=NULL&&p->next!=NULL)
  97.         {
  98.         
  99.         printf("%d\t",p->num);
  100.         printf("%f\n",p->score);
  101.         p=p->next;
  102.                
  103.         }*/
  104. }


  105. struct student *insert(struct student *head,struct student *pstu2)
  106. {
  107.         struct student *p,*p1;
  108.         
  109.         //p=head;
  110.         
  111.         if(head==NULL) //空链表
  112.         {
  113.                
  114.                 printf("daima 128 n=%d\n",n);   // 128行代码
  115.                 head=pstu2;
  116.                 pstu2->next=NULL;
  117.                 goto end;
  118.         }
  119. /*        if(p->next==NULL)
  120.         {
  121.                
  122.         }
  123.         */
  124.         
  125.         
  126.         
  127.         
  128.         end :
  129.         return head;
  130. }
复制代码
为什么128行代码 执行了2次?
运行时 首先请输入 学号为0 ,才会创建一个空的链表
会发现错误
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-1-16 18:25:18 | 显示全部楼层
本帖最后由 思无邪 于 2013-1-16 18:26 编辑

12.struct student *creat();//creat declearation
看看你创建链表的函数,头结点都没传进去,你返回的是你creat()内的局部变量,这样是不对的。
再好好研究下吧。
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-8-9 04:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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