#include <stdio.h>//顺序表的插入
#define MaxSize 50
typedef int ElemType;
typedef struct{
ElemType data[MaxSize];
int length;
}SqList;
bool ListInsert(SqList &l,int i,ElemType elem){
if(i<1||i>l.length){
return false;
}
if(MaxSize==l.length){
return false;
}
for(int j=l.length;j>=i;j--){
l.data[j]=l.data[j-1];
}
l.data[i-1]=elem;
l.length++;
return true;
}
bool DeleteList(SqList &l,int i,ElemType &element){
if(i<1||i>l.length){
return false;
}
element=l.data[i-1];
for(int j=i;j<=l.length;j++){
l.data[j-1]=l.data[j];
}
l.length--;
return true;
}
int FindElem(SqList l,ElemType elem){
for(int i=1;i<=l.length;i++){
if(elem==l.data[i-1]){
return i;
}
}
return 0;
}
void PrintSqList(SqList l){
for(int i=0;i<l.length;i++){
printf("%3d",l.data[i]);
}
printf("\n");
}
int main() {
SqList l;
bool ret;
int del;
int pos;
l.data[0]=1;
l.data[1]=2;
l.data[2]=3;
l.length=3;
ret= ListInsert(l,2,60);
if(ret){
printf("insert success\n");
PrintSqList(l);
}else{
printf("insert failed\n");
}
printf("-----------------------------\n");
ret=DeleteList(l,1,del);
if(ret){
printf("delete success\n");
PrintSqList(l);
}else{
printf("delete failed\n");
}
pos=FindElem(l,60);
if(pos){
printf("find element success\n");
printf("pos=%d",pos);
}else{
printf("find element failed\n");
}
return 0;
}