Nikel_Chieh 2022-02-26 10:59 采纳率: 83.3%
浏览 36
已结题

关于#链表#的问题,请各位专家解答!(语言-c语言)


#include <stdio.h>
#include <stdlib.h>

typedef struct node 
{
    int date;
    struct node *next;
}Node;

int main()
{
    //creat_linklist
    Node *head = NULL, *current, *previous;
    int n;
    scanf("%d", &n);
    while (n--)
    {
        current = (Node *)malloc(sizeof(Node *));
        scanf("%d", &current->date);
        current->next = NULL;
        if (head==NULL)
            head = current;
        else
            previous->next = current;
        previous = current;
    }

    Node *temp;
    Node *read;
    temp = (Node *)malloc(sizeof(Node *));
    scanf("%d", &temp->date);  //insert_date
    read = head;
    while ((read->next->date)>=(temp->date))
        read = read->next;
    temp->next = read->next;
    read->next = temp;

    read = head;
    while (read!=NULL)
    {
        printf("%d ", read->date);
        read = read->next;
    }
}

img

插入位置为什么不对

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2022-02-26 11:49
    关注

    第19行 和 第31行错误,用mallo()申请了个结构体指针的空间,修改如下:
    19行: current = (Node *)malloc(sizeof(Node));
    31行: temp = (Node *)malloc(sizeof(Node));

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月6日
  • 已采纳回答 2月26日
  • 创建了问题 2月26日

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格