c0re 2022-03-28 18:35 采纳率: 75%
浏览 35
已结题

c语言,链表节点插入

求解释一下我的代码为什么部分错误在PTA上。谢谢亲们,实在是检查不出来哪里错误!我想知道我的代码哪里错了!

 #include <stdio.h>
 #include <stdlib.h>
    struct node
  {
      int data;
      struct node *next;
  };
    struct node *creat(void);//这个是可能出错的函数
    struct node *creat(void)
    {
        int i,m,n,ii;
        struct node *h,*t,*p,*t2;
        h=(struct node *)malloc(sizeof(struct node));
        h->next=NULL;

        t=h;
        t2=h;
        //对应两种情况
        scanf("%d",&n);
        for (i=0;i<n;i++)
        {
            scanf("%d",&m);
            p=(struct node *)malloc(sizeof(struct node));
            scanf("%d",&p->data);
            p->next=NULL;
        if(m>i)//如果输入的数大于链表中的元素个数放在最后面
        {
            t=h;
            while(t->next!=NULL)
            {
                t=t->next;
            }
            t->next=p;
            p->next=NULL;
        }

          else//不大于的情况插在m元素后面
          {
              t2=h;
                for(ii=0;ii<m;ii++)
                {
                    t2=t2->next;
                }
              p->next=t2->next;
              t2->next=NULL;
              t2->next=p;
            }
        }
        return h;

    }
    //这个不可能出错下面是打印链表
        void print(struct node *head);
        void print(struct node *head)
        {
                struct node *p;
                int m=0;
                p=head->next;
                while(p)
                {
                    m++;
                    if(m==1)
                        printf("%d",p->data);
                    else
                    {
                          printf(" %d",p->data);

                    }
                    p=p->next;
                }
            printf("\n");

        }
        //下面是主函数也不可能出错
int main (void)
    {
        struct node *h;
        h=creat();
        print(h);
        return 0;
    }

img

  • 写回答

1条回答 默认 最新

  • zp_diandiandidi 2022-03-29 08:31
    关注

    代码太多了,建议你学会 debug,可以很快的帮你定位问题,学习 debug 很快的,百度下一会就明白了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    问题事件

    • 系统已结题 5月15日
    • 已采纳回答 5月7日
    • 创建了问题 3月28日

    悬赏问题

    • ¥15 act_ge_bytearray的草稿json数据复制到act_de_model 的model_editor_json的脚本
    • ¥15 cvi使用CreateThread创建线程时,出现存储空间不足无法处理此命令的错误
    • ¥15 求苹果推信imessage批量推信技术
    • ¥15 ubuntu 22.04 系统盘空间不足。隐藏的docker空间占用?(相关搜索:移动硬盘|管理系统)
    • ¥15 利用加权最小二乘法求亚马逊各类商品的价格指标?怎么求?
    • ¥15 c++ word自动化,为什么可用接口是空的?
    • ¥15 Matlab计算100000*100000的矩阵运算问题:
    • ¥50 VB6.0如何识别粘连的不规则的数字图片验证码
    • ¥16 需要完整的这份订单所有的代码,可以加钱
    • ¥30 写一个带界面控制的机房电脑一键开机关机并且实时监控的软件