顺序表
//结构体定义
typedef struct{
ElemType *data;
int Maxsize,length;
}List;
//静态分配
L.data = (ElemType*)malloc(sizeof(ElemType)*InitSize);
//动态分配
L.data = new ElemType[InitSize];
//初始化
void init(List &L){
L.length = 0;
}
//插入
bool insert(List &L,int i , ElemType e)
{
//判断范围
if(i<1 || i < L.length + 1)return false;
//判断是否满
if(L.length >= 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 ture;
}
//删除
bool Delete(List &L,int i,ElemType &e)
{
//判断范围
if(i<1 || i < L.length + 1)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 locateElem(List L,ElemType e)
{
int i;
for(int i = 0;i < L.Length;i ++)
if(L.data[i] == e)return i+1;
return false;
}