顺序表的操作
//顺序表的操作 #include<stdio.h>
#define MaxSize 100
typedef int ElemType;
typedef struct{
ElemType elem[MaxSize];
int length;
}SqList;
void ListInit(SqList &L)
{L.length=0;}
void ListInsert(SqList &L,int i,ElemType e)
{int j;
if(L.length>=MaxSize)printf("上溢\n");return;
if(i<1||i>L.length+1)printf("位置\n");return;
for(j=L.length-1;j>=i-1;j--)
L.elem[j+1]=L.elem[j];
L.elem[i-1]=e;
L.length++;
}
void ListDelete(SqList &L,int i,ElemType &e)
{int j,p=&(L.elem[i-1]),q=L.elem+L.length-1;
if((j<1)||(j>L.length))printf("位置\n");return;
e=*p;
for(++p;p<=q;++p)*(p-1)=*p;
L.length--;
}
//L=(1,3,5,7,9)
void main(){
SqList L;int k,e;
ListInit(L);
for(int i=1;i<=5;i++)
ListInsert(L,i,2*i-1);
for(int j=0;j<L.length;j++)
printf("%4d",L.elem[j]);
printf("\n");
scanf("%d",&k);//k=3
ListDelete(L,k,e);
printf("%d\n",e);//e=5
for(int j=0;j<L.length;j++)//L=(1,3,5,7,9)
printf("%4d",L.elem[j]);
printf("\n");
}