# 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