abcdehhhW 2022-05-08 10:34 采纳率: 50%
浏览 69
已结题

为什么无法输出插入后的链表

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
#include<stdio.h>
#include<stdlib.h>
struct node{
    int data;
    struct node *next;
};
struct node *creat(int a[],int n);
struct node *insert(struct node *head,int x);
void print(struct node *head);
int main()
{
    struct node *head=NULL;
    int a[10];
    int repeat,i,j,n,x;//x-待插入的数据 
    scanf("%d",&repeat);
    for(i=0;i<repeat;i++)
    {
        scanf("%d",&n);
        for(j=0;j<n;j++)    
        scanf("%d",&a[j]); 
        scanf("%d",&x);
        head=creat(a,n);
        insert(head,x);
        head=insert(head,x);
        printf("size=%d:",n+1);
        print(head);
        printf("\n");
    }
        return 0;    
}
struct node *creat(int a[],int n)
{
    int i;
    struct node *head=NULL,*end,*p;
    end=head;    
    for(i=0;i<n;i++)
    {
    p=(struct node*)malloc(sizeof(struct node));
    p->data=a[i];
    p->next=NULL;
    if(head==NULL)
        {
            head=p;
            end=head;
        }
    else
    {
        end->next=p;
        end=end->next;
    }
    }
    end->next=NULL;
    return head;
}
struct node *insert(struct node *head,int x)
{
    struct node *p1=head,*p2=head->next,*p3=NULL,*end=NULL;
    p3->data=x;
    p3->next=NULL;
    for(end=head;end->next!=NULL;end=end->next);//最后一个结点 
    if(p3->data<head->data)//插入点在头结点前 
    {
        p3->next=head;
        return p3;
    }
    else if(x>end->data)
    {
    end->next=p3;
    return head;
    }
    else//插入点在链表中间 
    {
        for(p1=head;p1!=NULL,p2!=end;p1=p1->next)
        {
            p2=p1->next;
            if(x>=p1->data&&x<=p2->data)
            {
                p1->next=p3;
                p3->next=p2;
            }
        }
        return head;
    }
}
void print(struct node *head)
{
    struct node *p;
    p=head;
    for(p=head;p!=NULL;p=p->next)
    {
        if(p==head)
        printf("%d",p->data);
        else 
        printf(" %d",p->data);
    }
}

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-05-08 14:18
    关注

    修改处见注释,供参考:

    #include<stdio.h>
    #include<stdlib.h>
    struct node{
        int    data;
        struct node *next;
    };
    struct node *creat(int a[],int n);
    struct node *insert(struct node *head,int x);
    void print(struct node *head);
    int main()
    {
        struct node *head=NULL;
        int a[10];
        int repeat,i,j,n,x;//x-待插入的数据
        scanf("%d",&repeat);
        for(i=0;i<repeat;i++)
        {
            scanf("%d",&n);
            for(j=0;j<n;j++)
                scanf("%d",&a[j]);
    
            head=creat(a,n);
            print(head);   //修改
            printf("\n");  //修改
    
            scanf("%d",&x);
               //insert(head,x);//修改
            head=insert(head,x);
            printf("size=%d:",n+1);
            print(head);
            printf("\n");
        }
        system("pause");
        return 0;
    }
    struct node *creat(int a[],int n)
    {
        int i;
        struct node *head=NULL,*end,*p;
        end=head;
        for(i=0;i<n;i++)
        {
            p=(struct node*)malloc(sizeof(struct node));
            p->data=a[i];
            p->next=NULL;
            if(head==NULL)
            {
                head=p;
                end=head;
            }
            else
            {
                end->next=p;
                end=end->next;
            }
        }
        end->next=NULL;
        return head;
    }
    struct node *insert(struct node *head,int x)
    {
        struct node *p1=head,*p2=NULL,*p3=NULL; //,*end=NULL;//修改
    
        p3 = (struct node*)malloc(sizeof(struct node)); //修改
        p3->data=x;
        p3->next=NULL;
    
        //for(end=head;end->next!=NULL;end=end->next);//最后一个结点//修改
        while (p1){
              if(p3->data < p1->data)//插入点在头结点前 if(p3->data<head->data)//修改
              {
                   if (p1 == head){   //p3->next=head; //修改
                       p3->next = head;//return p3;  //修改
                       head = p3;
                   }
                   else{                           //修改
                       p3->next = p2->next;        //修改
                       p2->next = p3;              //修改
                   }
                   return head;                    //修改
              }
                   //else if(x>end->data)         //修改
                   //{
                   //     end->next=p3;
                   //     return head;
                   //}
              else //插入点在链表中间
              {
                   // for(p1=head;p1!=NULL,p2!=end;p1=p1->next) //修改
                   //{
                   //  p2=p1->next;
                   //if(x>=p1->data&&x<=p2->data)
                   // {
                   // p1->next=p3;
                   // p3->next=p2;
                   p2 = p1;      //修改
                   p1 = p1->next;//修改
              }
        }
        if (p1 == NULL)   //修改
            p2->next = p3;//修改
        return head;
    }
    void print(struct node *head)
    {
        struct node *p;
        p=head;
        for(p=head;p!=NULL;p=p->next)
        {
            if(p==head)
               printf("%d",p->data);
            else
               printf(" %d",p->data);
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月16日
  • 已采纳回答 5月8日
  • 创建了问题 5月8日

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入