账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    顺序表实现过程中没有打印出任何值,求问哪里出现问题?
    28
    0
    #include <stdio.h>
    #include <stdlib.h>
    
    #define LIST_INIT_SIZE 100
    #define LISTINCREMENT 10
    
    #define OK 0
    #define error -1
    #define OVERFLOW -2
    
    typedef int ElemType;
    
    typedef struct _SqList
    {
        ElemType *elem;
        int length;        //表长度
        int listsize;    //当前分配的存储容量
    }SqList,*pSqList;
    
    //增加存储空间容量
    int addcapacity(SqList *sqlist)
    {
        ElemType *newbase;    //分配一个新的基址
        newbase = (ElemType *)malloc((sqlist->listsize + LISTINCREMENT) * sizeof(ElemType));
        if (newbase == NULL)
            exit(OVERFLOW);
        sqlist->elem = newbase;
        sqlist->listsize += LISTINCREMENT;
        return OK;
    }
    
    //初始化顺序表
    int InitList_sq(SqList *sqlist)
    {
        if (sqlist == NULL)
            return error;
        //给线性表分配初始容量
        sqlist->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
        if (!sqlist->elem)
            exit(OVERFLOW);
        sqlist->length = 0;
        sqlist->listsize = LIST_INIT_SIZE;
        return OK;
    }
    
    //在表的第i个位置插入新元素newelem
    int ListInsert(SqList *sqlist, int i, ElemType newelem)
    {
        if (sqlist == NULL || i<1 || i>sqlist->length + 1)
        {
            return error;
        }
        if (sqlist->length > sqlist->listsize)    //检查线性表是否已满,如果满了就扩充空间容量
        {
            if (addcapacity(sqlist) != OK)
                return OVERFLOW;
        }
        //将第i个元素以及第i个元素后面的元素后移
        for (int j = sqlist->length; j >= i; j--)
            sqlist->elem[j] = sqlist->elem[j - 1];
        sqlist->elem[i - 1] = newelem;
        sqlist->elem++;
        return OK;
    }
    
    //顺性表输出
    void Print_list(SqList *sqlist)
    {
        int i;
        for (i = 0; i < sqlist->length; i++)
            printf("%d  ", sqlist->elem[i]);
        printf("\n");
    }
    
    int main()
    {
        SqList L;
        InitList_sq(&L);
    
        for (int i = 0; i < 10; i++)    //将0-9插入顺序表
        {
            ListInsert(&L, i + 1, i);
        }
        Print_list(&L);
        return 0;
    }

    本来是想打印出来0-9的,可是没有输出任何值?这是哪个环节出现问题了?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部