汪的爱琪 2023-09-14 10:03 采纳率: 67.5%
浏览 7
已结题

数据结构链表指针问题

首先附上我的代码

#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语言指针给我指麻了

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2023-09-14 13:12
    关注

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

    #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;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月27日
  • 已采纳回答 1月19日
  • 创建了问题 9月14日

悬赏问题

  • ¥15 有人会用py或者r画这种图吗
  • ¥15 MOD04_3K图像预处理
  • ¥15 [VASP]关于超胞大小和k 点网格的收敛性测试
  • ¥15 pip下载paddle2onnx离谱错误
  • ¥60 db2move nlzxams import 导出db2备份数据报错
  • ¥15 关于#python#的问题:全文总结功能咨询
  • ¥15 俄罗斯方块中无法同时消除多个满行
  • ¥15 使用gojs3.0,如何在nodeDataArray设置好text的位置,再go.TextBlock alignment中进行相应的改变
  • ¥15 psfusion图像融合指标很低
  • ¥15 银河麒麟linux系统如何修改/etc/hosts权限为777