#include<cstdio>
#include<cstdlib>
#include<iostream>
#define ElemType int
#define InitSize 10
using namespace std;
typedef struct {
ElemType *data;
int length,Maxsize;
}SL;
void InitSL(SL &L);
bool Insert(SL &L,int i,int e);
bool Print(SL &L);
int Length(SL &L);
bool DeleSL(SL &L,int i,int &e); //删除指定位置的元素并归返回
int EQ(SL &L,ElemType e); //按值查找返回位置
ElemType LQ(SL &L,int i); //按位置查找,返回查找的元素
int main(){
SL L;
InitSL(L);
Insert(L,1,1);
Insert(L,2,4);
Insert(L,3,53);
Insert(L,4,44);
Print(L);
int e = LQ(L,4);
cout<<e;
// int por = EQ(L,34); //按值查找
// cout<<por;
// int e=0; //按位删除
// DeleSL(L,2,e);
// Print(L);
// int len = Length(L);
// cout<<len<<" "<<e;
return 0;
}
void InitSL(SL &L){
L.data=new ElemType[InitSize]; //动态分配存储空间
L.length = 0;
L.Maxsize = InitSize;
}
bool Insert(SL &L,int i,int e)
{
if(i<1||i>L.length +1 )
return false;
if(L.length == L.Maxsize)
return false;
for(int j = L.length; j>=i;j++)
{
L.data[j] = L.data[j-1];
}
L.data[i-1] = e;
L.length++;
return true;
}
bool DeleSL(SL &L,int i,int &e){
if(i<1||i>L.length)
return false;
e = L.data[i-1];
for(int j = i ;j<L.length;j++)
{
L.data[j-1] = L.data[j];
}
L.length--;
return true;
}
int EQ(SL &L,ElemType e) //按值查找返回位置
{
int por=0;
for(int j = 0; j< L.length ;j++){
if(L.data[j] == e)
{
por = j+1 ;
break;
}
}
return por;
}
ElemType LQ(SL &L,int i){ //按位置查找,返回查找的元素
ElemType e;
if(i<1||i>L.length){
return 0;
}else{
e = L.data[i-1];
return e;
}
}
bool Print(SL &L){
if(!L.length) return false ;
for(int i = 0 ;i<L.length ;i++)
printf("%d ",L.data[i]);
cout<<endl;
return true;
}
int Length(SL &L){
if(L.length==0) return 0;
return L.length;
}