Xuezhong_ 2021-03-16 22:41 采纳率: 100%
浏览 54
已采纳

数据结构顺序表括号一直显示有问题

# include<stdio.h>
# include<malloc.h>
# include<stdlib.h>
# define TRUE 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define INFEASIBLE -1
# define OVERFLOW -2

typedef int Status;
# define LIST_INIT_SIZE 100 //线性表存储空间的初始分配量
# define LISTINCREMENT 10 //线性表存储空间的分配增量

typedef struct
{ int * elem;
  int length;
  int listsize; }SqList;
 
 Status InitList(SqList &L)//定义空表
{
    L.elem=(int*)malloc(LIST_INIT_SIZE*sizeof(int));
    if (!L.elem)
    exit(OVERFLOW);
    L.length = 0;
    L.listsize = LIST_INIT_SIZE;
    return OK;
    }
    
int ListLength(SqList &L)//返回表长
{
if(!L.elem)
{
 return ERROR;
}
return L.length;
}

//判断顺序表是否为空,为空就返回 true,否则返回 false
Status ListEmpty(SqList &L)
{ if(!L.elem)
{
    return ERROR;    //表不存在
}
if(L.length == 0) return TRUE;
else return FALSE;
}

void displist(SqList &L)
{
    int i;
    if(ListEmpty(L))
    printf("NULL");
    for(i=0;i<L.length;i++)
    printf("%d",L.elem[i]);
    printf("\n");
}

Status ListInsert1(SqList &L , int i , int e)
{
int k; if(!L.elem)
{
return ERROR;
}
if(i>L.length+1 || i<1)
{
    return ERROR;    //i 值不合法
} if(L.length >= L.listsize)
{
int *newbase;
//空间不足,重新分配
newbase = (int*)realloc(L.elem , sizeof(int)*(L.listsize + LISTINCREMENT));
L.elem = newbase;
if(!L.elem)
{
return OVERFLOW; //分配失败
}
L.elem = newbase;
L.listsize+=LISTINCREMENT; //新空间容量
}


Status ListInsert2(SqList &L)
{                            //编译报错  这个括号有问题
    int *q,*p;
    int i,j;
    printf("请输入N\n");
    scanf("%d",&N);
    for(j=0;j<N;j++)
    {
        int *newbase;
        if(L.length>=L.listsize)
        {
            
        newbase = (int*)realloc(L.elem , sizeof(int)*(L.listsize + LISTINCREMENT));
        if(!newbase)
        exit(OVERFLOW);
        L.elem=newbase;
        L.listsize+=LISTINCREMENT;
        }
        int i;  //想插入的位置
        printf("请输入想插入的位置\n") ;
        scanf("%d",&i);
        
        int e;//插入的数
        printf("请输入想插入的数\n") ;
        scanf("%d",&e);
        q=&(L.elem[i-1]);
        for(p=&(L.elem[L.length-1]);p>=q;--p)
        *(p+1)=*p;
        *q=e;
        ++L.length;
    }
}


int main()
{
    SqList L;
    ElemType e; printf("1.初始化顺序表 L\n");
    InitList(L);
    printf("初始表长为:%d\n\n", L.length);
     printf("2.依次采用尾插法插入 12345五个元素\n\n");
 ListInsert1(L , 1 , 1);
 ListInsert1(L , 2 , 2);
  ListInsert1(L , 3 , 3);
ListInsert1(L , 4 , 4);
 ListInsert1(L , 5 , 5);
    displist( L) ;
    ListLength( L);
    printf("插入\n");
    int N;
    ListInsert2(L);
    displist( L) ;
    return 0;
            
}//这个括号也有问题

84    1    C:\Users\86153\Desktop\数据结构\线性表2.cpp    [Error] a function-definition is not allowed here before '{' token

137    1    C:\Users\86153\Desktop\数据结构\线性表2.cpp    [Error] expected '}' at end of input

 

  • 写回答

2条回答 默认 最新

  • cpp_learners 2021-03-17 08:11
    关注

    这个函数少了一个半边大括号'}',加上去就好

    Status ListInsert1(SqList &L, int i, int e) {
    	int k; if (!L.elem) {
    		return ERROR;
    	}
    	if (i > L.length + 1 || i < 1) {
    		return ERROR;    //i 值不合法
    	} if (L.length >= L.listsize) {
    		int *newbase;
    		//空间不足,重新分配
    		newbase = (int*)realloc(L.elem, sizeof(int)*(L.listsize + LISTINCREMENT));
    		L.elem = newbase;
    		if (!L.elem) {
    			return OVERFLOW; //分配失败
    		}
    		L.elem = newbase;
    		L.listsize += LISTINCREMENT; //新空间容量
    	}
    
    }    // 少了这个大括号
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用