m0_64486874 2022-05-13 01:15 采纳率: 100%
浏览 30
已结题

链表的插入问题,提交oj显示运行错误,不知道怎么改对😥

void insert(struct node *H,int p)
{
struct node *pr=H;
struct node node;
node=(struct node
)malloc(sizeof(struct node));
while(pr->next ->x<p)
{
pr=pr->next;
}
node->next =pr->next ;
node->x =p;
pr->next =node;

}

  • 写回答

2条回答 默认 最新

  • 丨秋水丨 2022-05-13 09:20
    关注

    malloc有问题:
    应该是struct node* node = (struct node*)malloc(sizeof(struct node));
    而且还需要判断是否为NULL

    void insert(struct node* H, int p)
    {
        struct node* pr = H;
        struct node* node = (struct node*)malloc(sizeof(struct node));
        while (pr->next && pr->next->x < p)
        {
            pr = pr->next;
        }
        node->next = pr->next;
        node->x = p;
        pr->next = node;
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

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