q571927406 2021-08-12 22:31 采纳率: 0%
浏览 50

stack around the variable ‘s’ was corrupted是指针操作有什么问题吗,搞不明白

img


#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#include< stdbool.h >


typedef struct pnode{
    int date;
    struct pnode *pnext;
}pnode,linklist;

void creat_list( linklist *L) //新建带头节点链表
{
    L = (pnode*)malloc(sizeof(pnode));
    if (L)
    {
        (L)->pnext = NULL;
    }
    else
    {
        printf("内存不足以分配malloc");
    }
}
bool initlist(linklist *L)//头插法链表初始化
{
    int x;
    scanf_s("%d", &x);
    pnode* newnode=0;
    pnode* tailnode = L;
    if (!L)
    {
        return false;
    }
    if (x != 999)
    {
        while (x != 999)
        {
            
            newnode = (pnode*)malloc(sizeof(pnode));
            if (newnode)
            {
                newnode->date = x;
                newnode->pnext = NULL;
                tailnode->pnext = newnode;
                tailnode = newnode;
                printf("请输入数据\n");
                scanf_s("%d", &x);
            }
            else {
                printf("空间不足以为newnode分配malloc");
            }
            
        }
    }
    printf("链表初始化完毕\n");
    return true;

}
void Printf(struct linklist* L)//打印链表
{
    pnode* n;
    n = L;
    while (n->pnext)
        {
            
            n = n->pnext;
            printf("%d->", n->date);


        }
}
 

int main()
{
    linklist* s;
    creat_list(&s);
    
    initlist(&s);
    Printf(&s);
    
    return 0;
}
  • 写回答

3条回答 默认 最新

  • qzjhjxj 2021-08-12 22:46
    关注

    修改如下,供参考:

    #include<stdio.h>
    #include<malloc.h>
    #include<stdlib.h>
    #include<string.h>
    #include< stdbool.h >
    
    typedef struct pnode{
        int date;
        struct pnode *pnext;
    }pnode,linklist;
    
    void creat_list( linklist *L) //新建带头节点链表
    {
        L = (pnode*)malloc(sizeof(pnode));
        if (L)
        {
            (L)->pnext = NULL;
        }
        else
        {
            printf("内存不足以分配malloc");
        }
    }
    bool initlist(linklist *L)//头插法链表初始化
    {
        int x;
        scanf_s("%d", &x);
        pnode* newnode=0;
        pnode* tailnode = L;
        if (!L)
        {
            return false;
        }
        //if (x != 999)
        //{
            while (x != 999)
            {
                newnode = (pnode*)malloc(sizeof(pnode));
                if (newnode)
                {
                    newnode->date = x;
                    newnode->pnext = NULL;
                    tailnode->pnext = newnode;
                    tailnode = newnode;
                    printf("请输入数据\n");
                    scanf_s("%d", &x);
                }
                else {
                    printf("空间不足以为newnode分配malloc");
                }
            }
        //}
        printf("链表初始化完毕\n");
        return true;
    }
    void Printf(linklist* L)//void Printf(struct linklist* L)//打印链表
    {
        pnode* n;
        n = L;
        while (n->pnext)
        {
             n = n->pnext;
             printf("%d->", n->date);
        }
        printf("\n");
    }
     
    int main()
    {
        linklist* s;
        creat_list(s); //&s
        initlist(s);   //&s
        Printf(s);     //&s
        return 0;
    }
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 8月12日