#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status; //函数的 返回值类型
typedef char ElemType; //元素的类型
typedef struct {
ElemType* elem;
int length;
}SqList;
int LocateELem(SqList L, ElemType e)
{
int i;
for (i = 0; i < L.length; i++)
{
if (L.elem[i] == e)
return i + 1; //查找成功返回序号
else
return 0;
}
}
Status Listnsert_Sq(SqList& L, int i, ElemType e)
{
int j;
if (i<1 || i>L.length +1 )
return ERROR; //插入的位置不合法
if (L.length == MAXSIZE)
return ERROR; //存储的空间已经满
for (j = L.length - 1; j >= i - 1; j--) //这里为L.length-1是因为在数组中下标从0开始的
L.elem[j + 1] = L.elem[j]; //从表的最后开始依次移动表中元素的位置知道遇到插入的位置i,也就是下标为i-1
L.elem[i-1] = e; //插入元素
L.length++; // 表长加一
return OK;
}/*平均移动次数为n/2,时间 复杂度为n*/
Status ListDelete_Sq(SqList &L,int i)
{
int j;
/*先判断删除的位置是否合法*/
if (i<1 || i>L.length - 1)
return ERROR;
for (j = i; j <= L.length - 1; j++)
L.elem[j - 1] = L.elem[j];
L.length--; //表长减一
return OK;
}