Yester07 2022-03-24 20:58 采纳率: 48.5%
浏览 43
已结题

链表出现了问题,堆栈已损坏,指针不兼容,求解决

代码:

#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
typedef struct
{
    int value;
    struct node* next;
}node;

int creatlistf(node* l, int data[], int length)
{
    node* p;
    if (l)
    {
        l->next = NULL;
        for (int i = 0; i < length; i++)
        {
            p = (node*)malloc(sizeof(node));
            p->value = data[i];
            p->next = l->next;
            l->next = p;
        }
        return 1;
    }
    return 0;
}

int creatlistr(node* l, int data[], int length)
{
    node* r, *p;
    if (l)
    {
        l->next = NULL;
        r = l;
        for (int i = 0; i < length; i++)
        {
            p = (node*)malloc(sizeof(node));
            p->value = data[i];
            printf("%d\t", p->value);
            r->next = p;
            r = p;
        }
        r->next = NULL;
        return 1;
    }
    return 0;
}

void displist(node* l)
{
    node* p;
    if (l)
    {
        p = l;
        while (p->next != NULL)
        {
            p = p->next;
            printf("%d\t", p->value);
        }
    }
}
int main()
{
    node* l;
    l = (node*)malloc(sizeof(node));
    int x[10] = { 1,2,3,4,5,6,7,8,9,10 },STATUS=0;
    STATUS = creatlistf(&l, x, 10);
    displist(&l);
    return 0;
}

具体报错如下(软件为vs),因为是xin shou,希望能点名原因和修改方法,谢谢!翻译是是l的两个堆栈已损坏。

img

谢谢!

  • 写回答

3条回答 默认 最新

  • fuill 2022-03-24 21:14
    关注

    改了三个地方,写注释里了

    #define _CRT_SECURE_NO_WARNINGS
    #include "stdio.h"
    #include "stdlib.h"
    typedef struct Node
    {
        int value;
        struct Node* next;
    }node;
     //定义的链表和节点是两个不同的结构体 ,名字不能一样
    //node是链表名,Node是节点名
    int creatlistf(node* l, int data[], int length)
    {
        node* p;
        if (l)
        {
            l->next = NULL;
            for (int i = 0; i < length; i++)
            {
                p = (node*)malloc(sizeof(node));
                p->value = data[i];
                p->next = l->next;
                l->next = p;
            }
            return 1;
        }
        return 0;
    }
     
    int creatlistr(node* l, int data[], int length)
    {
        node* r, *p;
        if (l)
        {
            l->next = NULL;
            r = l;
            for (int i = 0; i < length; i++)
            {
                p = (node*)malloc(sizeof(node));
                p->value = data[i];
                printf("%d\t", p->value);
                r->next = p;
                r = p;
            }
            r->next = NULL;
            return 1;
        }
        return 0;
    }
     
    void displist(node* l)
    {
        node* p;
        if (l)
        {
            p = l;
            while (p->next != NULL)
            {
                p = p->next;
                printf("%d\t", p->value);
            }
        }
    }
    int main()
    {
        node* l;
        l = (node*)malloc(sizeof(node));
        int x[10] = { 1,2,3,4,5,6,7,8,9,10 },STATUS=0;
        STATUS = creatlistf(l, x, 10);//&号去掉 
        displist(l);//&号去掉 
        return 0;
    }
     
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月1日
  • 已采纳回答 3月24日
  • 修改了问题 3月24日
  • 修改了问题 3月24日
  • 展开全部

悬赏问题

  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测