_喜欢睡觉 2022-12-05 22:54 采纳率: 77.8%
浏览 15
已结题

链表的插入问题,print函数出现致命错误,为什么

系统报错说print函数有一个致命错误,但是我实在找不到

#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct su)
struct su//定义结构体
{
int num;
struct su *next;
};
struct su *create(int n);
struct su *inset(struct su *head,struct su *cha,int j,int n);
void print(struct su *head);
int i=0;
int main()
{
struct su *a,*cha;
int n,j;
scanf("%d",&n);//输入要存多少数
a=create(n);//创建链表
scanf("%d",&j);//输入在那个结点后插数
scanf("%d",&cha->num);//输入想插的数
a=inset(a,cha,j,n);
print(a);//打印链表
printf("\n");
return 0;

}
struct su *create(int n)//传入要输出多少个数
{
struct su *head;
struct su *p1,*p2;//定义3个结构体指针
p1=p2=(struct su *)malloc(LEN);//开辟内存
scanf("%d",&p1->num);//输入元素
head=NULL;//初始化指针
while(i<n)
{
i++;//计算结点
if(i==1)
{
head=p1;//第一个结点当头指针
}
else
{
p2->next=p1;//不是第一个就将p2指向新开辟的结点
}
p2=p1;//再将p2拉到与p1同起跑线
p1=(struct su *)malloc(LEN);//用p1开辟新结点
if(i==n)//当达到所要的数,就停止开辟新结点
break;
scanf("%d",&p1->num);//输入新结点的数据域的值
}
p2->next=NULL;//将最后的结点指向null
return head;//返回头指针
}
struct su *inset(struct su *head,struct su *cha,int j,int n)
{
struct su *p1,*p2,*p0;
int count=0;
p1=head;p0=cha;
if(head==NULL)
{
head=p0;p0->next=NULL;
}
else
{

    while(count<=j)//计数到想插结点
    {
        count++;
        p2=p1;p1=p1->next;
    }

if(head==p1)
{
p0->next=head;head=p0;//想插结点为首指针时
}
else
{
p2->next=p0;p0->next=p1;//非首非尾时

}
return head;//返回头指针

}
void print(struct su *head)//传入头指针
{
struct su *p=head;//定义指针p
if(head!=0)//当传入的头指针不是0
{
do{
printf("%d ",p->num);//打印
p=p->next;//指下一个
}while(p!=0);//循环到尾指针
}

}

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-12-06 01:59
    关注

    缺了为 cha 结点分配空间,其它见注释修改处,供参考:

    #include <stdio.h>
    #include <stdlib.h>
    #define LEN sizeof(struct su)
    struct su//定义结构体
    {
        int    num;
        struct su *next;
    };
    struct su *create(int n);
    struct su *inset(struct su *head,struct su *cha,int j,int n);
    void   print(struct su *head);
    int i=0;
    int main()
    {
        struct su *a,*cha;
        int n,j;
        scanf("%d",&n);//输入要存多少数
        a=create(n);//创建链表
    
        cha = (struct su*)malloc(LEN);     //修改,需要为cha结点分配空间
        scanf("%d",&j);//输入在那个结点后插数
        scanf("%d",&cha->num);//输入想插的数
        a=inset(a,cha,j,n);
    
        print(a);//打印链表
        printf("\n");
        return 0;
    }
    struct su *create(int n)//传入要输出多少个数
    {
        struct su *head;
        struct su *p1,*p2;//定义3个结构体指针
        //p1=p2=(struct su *)malloc(LEN);//开辟内存     修改
        //scanf("%d",&p1->num);//输入元素               修改
        head=NULL;//初始化指针
        while(i<n)
        {
            p1=(struct su *)malloc(LEN);//用p1开辟新结点   修改
    
            //if(i==n)//当达到所要的数,就停止开辟新结点   修改
            //     break;
            scanf("%d",&p1->num);//输入新结点的数据域的值  修改
    
            i++;//计算结点
    
            if(i==1)
            {
                head=p1;//第一个结点当头指针
            }
            else
            {
                p2->next=p1;//不是第一个就将p2指向新开辟的结点
            }
            p2=p1;//再将p2拉到与p1同起跑线
    
        }
        p2->next=NULL;//将最后的结点指向null
        return head;//返回头指针
    }
    struct su *inset(struct su *head,struct su *cha,int j,int n)
    {
        struct su *p1,*p2,*p0;
        int count=0;
        p1=head;p0=cha;
        if(head==NULL)
        {
            head=p0;
            p0->next=NULL;
        }
        else
        {
            while(p1 && count<j)//(count<=j) 修改
            {
                count++;
                p2=p1;
                p1=p1->next;
            }
            if(head==p1)
            {
                p0->next=head;head=p0;//想插结点为首指针时
            }
            else
            {
                p2->next=p0;p0->next=p1;//非首非尾时
            }
        }
        return head;//返回头指针
    }
    void print(struct su *head)//传入头指针
    {
        struct su *p=head;//定义指针p
        if(head!=NULL)// (head!=0)   修改
        {
            do{
                printf("%d ",p->num);//打印
                p=p->next;//指下一个
            }while(p!=NULL);//while(p!=0)
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月14日
  • 已采纳回答 12月6日
  • 创建了问题 12月5日

悬赏问题

  • ¥15 matlab数据降噪处理,提高数据的可信度,确保峰值信号的不损失?
  • ¥15 怎么看我在bios每次修改的日志
  • ¥15 python+mysql图书管理系统
  • ¥15 Questasim Error: (vcom-13)
  • ¥15 船舶旋回实验matlab
  • ¥30 SQL 数组,游标,递归覆盖原值
  • ¥15 为什么我的数据接收的那么慢呀有没有完整的 hal 库并 代码呀有的话能不能发我一份并且我用 printf 函数显示处理之后的数据,用 debug 就不能运行了呢
  • ¥20 gitlab 中文路径,无法下载
  • ¥15 用动态规划算法均分纸牌
  • ¥30 udp socket,bind 0.0.0.0 ,如何自动选取用户访问的服务器IP来回复数据