1.顺序表的存储结构
#define MAXSIZE 100
typedef struct{
ElemType *elem;
int length;
}SqList;
2.顺序表的初始化
Status InitList(SqList &L){
L.elem=new ElemType[MAXSIZE];
if(!L.elem) exit(OVERFLOW);
L.length=0;
return OK;
}
3.顺序表的取值
Status GetElem(SqList L,int i,ElemType &e){
if(i<1 || i>L.length) return ERROR;
e=L.elem[i-1];
return OK;
}
4.顺序表的查找
int LocateElem(SqList L,ElemType e){
for(i=0;i<L.length;i++)
if(L.elem[i]==e) return i+1;
return 0;
}
5.顺序表的插入
Status ListInsert(SqList &L,int i,ElemType e){
if((i<1)||(i>L.length+1)) return ERROR;
if(L.length==MAXSIZE) return ERROR;
for(j=L.length-1;j>=i-1;j--)
L.elem[j+1]=L.elem[j];
L.elem[i-1]=e;
++L.length;
return OK;
}