m0_63996800 2021-12-13 20:43 采纳率: 100%
浏览 25
已结题

顺序表的基本操作代码运行不报错但运行不了。

我用的gcc
代码运行不报错但就是执行不了。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define list 10
#define max 10

typedef int Elemtype;
typedef struct sqlist 
{
    Elemtype *elem;
    int lenght;
    int listsize;
}*List;
void intlist (List L)
{
    L->elem=(Elemtype *)malloc(list*sizeof(Elemtype));
    L->lenght=0;
    L->listsize-max;
}

void creat(List L,int a[])
{
    intlist(L);
    int i;
    for(i=0;i<max;i++)
    {
        L->elem[i]=a[i];
        L->lenght++;
    }
}

int print(List L)
{
    int i;
    L->lenght=0;
    for(i=0;i<max;i++)
    {printf("%d",L->elem[i]);
    L->lenght--;}
    return 0;
}

int main(List L)
{
    int a[10]={1,2,3,4,5,6,7,8,9,0};
    creat(L,a);
    print(L);
    return 0;
}


  • 写回答

1条回答 默认 最新

  • CSDN专家-link 2021-12-13 20:47
    关注

    int main居然还带个List参数???
    creat用List类型作为参数,在函数内分配空间是不行的,不能修改外部L指针变量的地址的

     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <malloc.h>
    #define list 10
    #define max 10
    typedef int Elemtype;
    typedef struct sqlist 
    {
        Elemtype *elem;
        int lenght;
        int listsize;
    }*List;
    void intlist (List L)
    {
        L->elem=(Elemtype *)malloc(list*sizeof(Elemtype));
        L->lenght=0;
        L->listsize=max;
    }
    List creat(List L,int a[])
    {
        L = (List)malloc(sizeof(struct sqlist));
        intlist(L);
        int i;
        for(i=0;i<max;i++)
        {
            L->elem[i]=a[i];
            L->lenght++;
        }
        return L;
    }
    int print(List L)
    {
        int i;
        for(i=0;i<max;i++)
        {printf("%d",L->elem[i]);
        L->lenght--;}
        return 0;
    }
    int main()
    {
        List L;
        int a[10]={1,2,3,4,5,6,7,8,9,0};
        L = creat(L,a);
        print(L);
        return 0;
    }
     
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 12月22日
  • 已采纳回答 12月14日
  • 创建了问题 12月13日

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)