^334 2022-02-06 17:12 采纳率: 88.5%
浏览 42
已结题

谁能帮我看看增添链表错哪里了

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

```c

#include<stdio.h>
#include<stdlib.h>
typedef struct list {
    int date;
    struct list* next;

}node;
int main()
{
    int a;
    int b;
    int i, j;
    int x, z;
    int count;
    //链表的创立
    printf("请输入链表节点为几\n");
    scanf_s("%d", &a);
    printf("请输入链表数据\n");
    int* p = (int*)malloc(a * sizeof(int));
    for (j = 0; j < a; j++)
    {
        scanf_s("%d", &p[j]);
    }
    node* head = (node*)malloc(sizeof(node));
    head->next = NULL;
    for (i = 0; i < a; i++)
    {
        node* node1 = (node*)malloc(sizeof(node));
        node1->date = p[i];
        node1->next = head->next;
        head->next = node1;

    }
    //链表的遍历
    node* h;
    h = head->next;
    printf("你输入的链表如下\n");
    while (h != NULL)
    {
        printf("%d    ", h->date);
        h = h->next;
    }
    //链表的插入
    h = head->next;
    printf("请输入插在第几个数后面");
    scanf_s("%d", &x);
    printf("请输入插入的数");
    scanf_s("%d", &z);
    count = 0;
    while (h != NULL && count < x)
    {
        count++;
        h = h->next;


        if (count == x)
        {

            node* node1 = (node*)malloc(sizeof(node));
            node1->date = z;
            node1->next = head->next;
            head->next = node1;
        }

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



}

```

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2022-02-06 21:26
    关注

    修改处见注释,供参考:

    #include<stdio.h>
    #include<stdlib.h>
    typedef struct list {
        int date;
        struct list* next;
    }node;
    int main()
    {
        int a;
        int b;
        int i, j;
        int x, z;
        int count;
        //链表的创立
        printf("请输入链表节点为几\n");
        scanf_s("%d", &a);
        printf("请输入链表数据\n");
        int* p = (int*)malloc(a * sizeof(int));
        for (j = 0; j < a; j++)
        {
             scanf_s("%d", &p[j]);
        }
        node* head = (node*)malloc(sizeof(node));
        head->next = NULL;
        for (i = 0; i < a; i++)
        {
            node* node1 = (node*)malloc(sizeof(node));
            node1->date = p[i];
            node1->next = head->next;
            head->next = node1;
        }
        //链表的遍历
        node* h,*hre;   //修改
        h = head->next;
        printf("你输入的链表如下\n");
        while (h != NULL)
        {
            printf("%d    ", h->date);
            h = h->next;
        }
        printf("\n");
        //链表的插入
        hre = head;      //修改
        h = head->next;
        do{                                    //修改
            printf("请输入插在第几个数后面");
            scanf_s("%d", &x);
        }while(x <= 0 || x > a);               //修改
        printf("请输入插入的数");
        scanf_s("%d", &z);
        count = 0;
        while (h != NULL && count < x)  //修改
        {
            count++;
            hre = h;                    //修改
            h = h->next;
        }                                //修改
        node* node1 = (node*)malloc(sizeof(node));
        node1->date = z;
        node1->next = hre->next; //修改
        hre->next = node1;        //修改
    
        h = head->next;
        while (h != NULL)
        {
            printf("%d    ", h->date);
            h = h->next;
        }
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 2月16日
  • 已采纳回答 2月8日
  • 专家修改了标签 2月6日
  • 创建了问题 2月6日

悬赏问题

  • ¥15 socket通信实现多人聊天室疑惑
  • ¥15 DEV-C++编译缺失
  • ¥33 找熟练码农写段Pyhthon程序
  • ¥100 怎么让数据库字段自动更新
  • ¥15 antv g6 力导向图布局
  • ¥15 quartz框架,No record found for selection of Trigger with key
  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数