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日

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b