if(L.length>=L.listsize) /* 当前存储空间已满,增加分配 */
{
newbase=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!newbase)
return Error;; /* 存储分配失败 */
L.elem=newbase; /* 新基址 */
L.listsize+=LISTINCREMENT; /* 增加存储容量 */
}
上面这是正确的,怎么改成下面的就错了??
if(L.length>=L.listsize)
{
newbase= new ElemType[L.listsize+LISTINCREMENT];
if(!newbase)
return Error;
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
/*判断存储空间是否已满,增加储存容量*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define Error 0
typedef int Status;
typedef int Boolean;
typedef int ElemType;
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 10
typedef struct
{
ElemType *elem;
int length;
int listsize;
char name[10];
}SqList;
/*创建结构体(Sqlist)*/
int Empty(SqList L)
{
if(L.length==0)
return TRUE;
return FALSE;
}
/*创建子函数(子程序),这是用来判断顺序表是否为空*/
Status InitList(SqList &L)
{
L.elem=new ElemType[LIST_INIT_SIZE];
if(!L.elem)
return Error;
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}
/*初始化顺序表,产生一个新的存储空间*/
Status ListInsert(SqList &L,int i,ElemType e)
{
ElemType *newbase,*q,*p;
if(i<1||i>L.length+1)
return Error;
/*判断i值是否合法*/
if(L.length>=L.listsize)
{
newbase= new ElemType[L.listsize+LISTINCREMENT];
if(!newbase)
return Error;
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
/*判断存储空间是否已满,增加储存容量*/
q=L.elem +i-1;
for(p=L.elem+L.length-1;p>=q;--p)
*(p+1)=*p;
*q=e;
++L.length;
return OK;
//插入算法
}
Status ListDelete(SqList &L,int i,ElemType *e)
{
ElemType *p,*q;
if(i<1||i>L.length)
return Error;
if(Empty(L)) {printf("empty\n");return Error;}
p=L.elem+i-1;
*e=*p;
q=L.elem+L.length-1;
for(++p;p<=q;++p)
*(p-1)=*p;
L.length--;
return OK;
//删除算法
}
void output(SqList L){
int i;
if (L.length==0)printf("error\n");
for(i=0;i<L.length;i++)
printf("%d " ,L.elem[i]);
printf("\n");
}
main(){
int i;int x;
SqList L;
if(InitList(L)==Error) {printf("ERRor\n"); return Error;}
// if(Empty(L)) printf("empty\n");
ListInsert(L,1,90);
for(i=2;i<20;i++)
ListInsert(L,1,i);
output(L);
/*ListDelete(L,1,&x);
printf("%d\n",x);
output(L);*/
}