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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了