//封装数组 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXSIZE 20 typedef int ElemType; typedef struct{ ElemType data[MAXSIZE]; int length; }SqList;
//获取线性表元素 Status GetElem(SqList L,int i,ElemType *e){ if(L.length==0||i<1||i>L.length){ return ERROR; } *e=L.data[i-1]; return OK; }
//线性表插入 Status ListInsert(SqList *L,int i,ElemType e){ int k; if(i<1||i>L->length+1||L->length==MAXSIZE) return ERROR; for(k=L->length-1;k>=i-1;k--){ L->data[k+1]=L->data[k]; } L->data[i-1]=e; L->length++; return OK; }
//线性表删除 Status ListDelete(SqList *L,int i,ElemType *e){ int k; if(i<1||i>L->length||L->length==0) return ERROR; *e=L->data[i-1]; for(k=i-1;k<L->length-1;k++){ L->data[k]=L->data[k+1]; } L->length--; return OK; }