解惑:线性表长度
# include<stdio.h># include<stdlib.h>
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef int ElemType;
typedef int status;
typedef struct SqList
{ ElemType *elem;
int length;
}SqList;
status creatlist(SqList &L,int n)
{
//创建线性表
int i;
L.elem=new ElemType[MAXSIZE]; <————————————————
if(!L.elem) return ERROR;
L.length=n; <————————————————前面已经为线性表分配100的空间了,那这里为什么又要写线性表的长度呢?
printf(" Input the element: ");
for(i=0;i<L.length;i++) scanf("%d",&L.elem[i]);
printf(" The created list as follow: ");
for(i=0;i<L.length;i++) printf("%d ",L.elem[i]);
printf("\n");
return OK;
}