数据结构链表指针问题

首先附上我的代码

#include<stdio.h>
#include <stdlib.h>
#define ERROR 500
#define OK 200
typedef struct leader{
    int length;
    struct node * start;
    int firstIndex;
}head;
typedef struct node{
    int data;
    struct node * next;
}body;
int main(){
    head * Init_linklist();
    int  Set_linklist(head * h,int data);
    void Get_linklist(head * h);
    void Insert_linklist(head * h,int index,int data);
    void Delete_linklist(head * h);
    void Clear_linklist(head * h);
    void Destory(head * h);
    head * h = Init_linklist();
    Set_linklist(h,1);
    Set_linklist(h,2);
    Set_linklist(h,3);
    Set_linklist(h,4);
    Set_linklist(h,5);
body *current = h->start; // 初始化一个新的临时指针
while (current) { // 当当前节点不为空时
    printf("%d\t", current->data);
    current = current->next; // 移到下一个节点
}
    return OK;
}
body * dummy;
head * Init_linklist(){
    head * h = (head *)malloc(sizeof(head)); 
    dummy = (body *)malloc(sizeof(body));
    memset(h,0,sizeof(head));
    memset(dummy,0,sizeof(body));
    h->start = dummy;
    h->firstIndex=h->start;
    return h;
}
int Set_linklist(head * h,int data){
    if(h == NULL ){
        printf("set error with l NULL in line 36!\n");
        return ERROR;
    }
    struct node * n = NULL;
    n = (body*)malloc(sizeof(body));
    memset(n,0,sizeof(body));
    n->data = data;
    dummy->next = n;
    dummy = n;
    h->length++;
    return OK;
}

我的问题

img

img

img

img

img

img

img

img

img

img

img

img


C语言指针给我指麻了

【以下回答由 GPT 生成】

在上述代码中,存在一个指针问题。具体来说,当我们尝试打印链表内容时,current指针没有正确初始化,因此会导致程序崩溃。需要修复这个问题。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

修改如下,改动处见注释,供参考:

#include<stdio.h>
#include <stdlib.h>
#define ERROR 500
#define OK 200

typedef struct node {
    int    data;
    struct node* next;
}body;
typedef struct leader {
    int    length;
    int    firstIndex;
    struct node* start;
}head;
// 函数声明 移动到主函数外   修改
head* Init_linklist();
int  Set_linklist(head* h, int data);
void Get_linklist(head* h);
void Insert_linklist(head* h, int index, int data);
void Delete_linklist(head* h);
void Clear_linklist(head* h);
void Destory(head* h);

int main() {
    
    head* h = Init_linklist();
    Set_linklist(h, 1);
    Set_linklist(h, 2);
    Set_linklist(h, 3);
    Set_linklist(h, 4);
    Set_linklist(h, 5);
    body* current = h->start; // 初始化一个新的临时指针
    while (current) { // 当当前节点不为空时
        printf("%d ", current->data);
        current = current->next; // 移到下一个节点
    }
    return OK;
}
              //body* dummy;  修改
head* Init_linklist() {
    head* h = (head*)malloc(sizeof(head));
             //dummy = (body*)malloc(sizeof(body));  修改
             //memset(h, 0, sizeof(head));           修改
             //memset(dummy, 0, sizeof(body));       修改
    h->start = NULL; //h->start = dummy;             修改
    h->firstIndex = h->length = 0; //h->firstIndex = h->start; 修改
    return h;
}
int Set_linklist(head* h, int data) {
    if (h == NULL) {
        printf("set error with l NULL in line 36!\n");
        return ERROR;
    }
    struct node* n = NULL;
    n = (body*)malloc(sizeof(body));
    n->next = NULL;       //memset(n, 0, sizeof(body)); 修改
    n->data = data;
    n->next = h->start; //头插法  //dummy->next = n;    修改
    h->start = n;                 //dummy = n;          修改
    h->length++;
    return OK;
}