_rivulet 2021-10-07 15:35 采纳率: 66.7%
浏览 51
已结题

请教一下这个print函数为什么无法运行


#include<stdio.h>
#include<stdlib.h>
#define InitSize 20
#define ERROR 0
#define OK 1
#define OVERFLOW -2
typedef int ElemType;

typedef struct{
    ElemType *elem;                //指向数据元素的基地址
    int length,MaxSize;        //线性表的当前长度和最大容量 
}Sqlist;

//顺序表的创建 
void InitList(Sqlist *L)
{
    (*L).elem=(int*)malloc(InitSize*sizeof(int));            //给顺序表申请内存 
    (*L).length=0;                    
    (*L).MaxSize=InitSize;                //初始化最大容量为已申请内存容量 
 } 
 
//顺序表的初始化
void InputList(Sqlist *L)
{
    int N;
    int i=0;
    printf("Enter the length of the Order table you want:\n");
    scanf("%d",&N);
    printf("input data:\n");
    while(N)
    {
        scanf("%d",&(*L).elem[i]);
        i++;
        N--; 
    }
 } 
 
//增加顺序表长度 
void IncreaseSize(Sqlist *L,int len)            //增加长度 
{
    ElemType *temp=(*L).elem;
    int i;
    (*L).elem=(ElemType*)malloc(((*L).length+len)*sizeof(ElemType));        
    for(i=0 ; i<(*L).length ; i++)
    {
        (*L).elem[i]=temp[i];            //将数据复制到新区域 
    }
    (*L).MaxSize=(*L).length+len;
    free(temp);                //释放temp所指向的内存区域 
}

//顺序表的打印 
void PrintList(Sqlist L)
{
    int i;
    printf("text\n");
    for(i=0 ; i<L.length-1 ; i++){
        printf("elem[%d]=%d\n",i,L.elem[i]);
    }printf("text2\n");
}

int main()
{
    Sqlist L;
    InitList(&L);
    InputList(&L);
    Empty(L);
    printf("Print Order table\n");
    PrintList(L);
  • 写回答

1条回答 默认 最新

  • ????27282 优质创作者: 信息安全技术领域 2021-10-07 15:44
    关注

    这是我写的顺序表

    #define overflow -1
    #define ok 1
    #define error 0
    #define maxsize 16
    #define increment 10
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    
    typedef int status;
    typedef int elemtype;
    typedef struct
    {
        elemtype *elem;
        int length;
        int listsize;
    } sqlist;
    
    status initlist(sqlist &L)
    {
        L.elem = (elemtype *)malloc(maxsize * sizeof(elemtype));
        if (!L.elem)
            exit(overflow);
        L.length = 0;
        L.listsize = maxsize;
        return ok;
    }
    
    status insertlist(sqlist &L, int i, elemtype e)
    {
        if (i < 1 || i > L.length)
            return error;
        elemtype *p;
        int j;
        if (L.length >= L.listsize)
        {
            p = (elemtype *)realloc(L.elem, (L.listsize + increment) * sizeof(elemtype));
            if (!p)
                exit(overflow);
            L.elem = p;
            L.listsize += increment;
        }
        for (j = L.length - 1; j >= i - 1; --j)
            L.elem[j + 1] = L.elem[j];
        L.elem[j + 1] = e;
        L.length++;
        return ok;
    }
    
    void printlist(sqlist L)
    {
        int i;
        for (i = 0; i < L.length; i++)
            printf("%d ", L.elem[i]);
        printf("\n");
    }
    
    status dellist(sqlist &L, int i, elemtype &e)
    {
        int j;
        if (i < 1 || i > L.length)
            return error;
        e = L.elem[i - 1];
        for (j = i; j < L.length; j++)
            L.elem[j - 1] = L.elem[j];
        --L.length;
        return ok;
    }
    
    int main()
    {
        sqlist LL;
        elemtype x;
        int r, i;
    
        if (!initlist(LL)) //初始化
            return error;
    
        for (i = 0; i < 10; i++)
            LL.elem[i] = i * 8;
        LL.length += 10;
    
        //printf("value index-->");  //插入
        //scanf("%d %d", &x, &r);
        x = 188, r = 3;
        insertlist(LL, r, x);
    
        printlist(LL);
    
        //printf("index-->");  //删除
        //scanf("%d", &r);
        dellist(LL, r, x);
    
        printlist(LL);
    
    
        printf("xiaoxiaoran");
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月15日
  • 已采纳回答 10月7日
  • 创建了问题 10月7日