m0_65712630 2022-09-09 19:54 采纳率: 100%
浏览 58
已结题

数据结构 为什么一直说我L未初始化

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define OVERFLOW -2
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 10

typedef int Status;
typedef int ElemType;

typedef  struct {
ElemType *elem;
int length;
int listsize;
}SqList;

Status InitList_Sq(SqList *L);
Status ListInsert_Sq(SqList *L, int i, ElemType e);
void PrintList(SqList *L);

Status InitList_Sq(SqList L) {
L->elem = (ElemType
)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!L->elem)
exit(OVERFLOW);
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return OK;
}

void PrintList(SqList *L) {
for (int i = 0; i < L->length - 1; i++) {
printf("%c", L->elem[i]);
}
}

int main() {
SqList sq;
SqList* L = &sq;
Status InitList_Sq(L);
L->length = 6;
L->elem[0] = 1;
L->elem[1] = 2;
L->elem[2] = 3;
L->elem[3] = 4;
L->elem[4] = 5;
L->elem[5] = 6;
void PrintList(L); 
return OK;
}

  • 写回答

3条回答 默认 最新

  • 来灵 2022-09-09 20:02
    关注

    代码给你稍微修改了下

    #include <stdio.h>
    #include <malloc.h>
    #include <stdlib.h>
    #define OVERFLOW -2
    #define OK 1
    #define ERROR 0
    #define LIST_INIT_SIZE 10
    #define LISTINCREMENT 10
    
    typedef int Status;
    typedef int ElemType;
    
    typedef  struct {
        ElemType* elem;
        int length;
        int listsize;
    }SqList;
    
    Status InitList_Sq(SqList* L);
    Status ListInsert_Sq(SqList* L, int i, ElemType e);
    void PrintList(SqList* L);
    
    Status InitList_Sq(SqList* L) {
        L->elem = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType));
        if (!L->elem)
            exit(OVERFLOW);
        L->length = 0;
        L->listsize = LIST_INIT_SIZE;
        return OK;
    }
    
    void PrintList(SqList* L) {
        for (int i = 0; i < L->length - 1; i++) {
            printf("%c", L->elem[i]);
        }
    }
    
    int main() {
        SqList sq;
        SqList* L = &sq;
        InitList_Sq(L);
        L->length = 6;
        L->elem[0] = 1;
        L->elem[1] = 2;
        L->elem[2] = 3;
        L->elem[3] = 4;
        L->elem[4] = 5;
        L->elem[5] = 6;
        PrintList(L);
        return OK;
    }
    

    如有帮助,采纳一下呗

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 9月17日
  • 已采纳回答 9月9日
  • 创建了问题 9月9日

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?