数据结构与算法基础(青岛大学-王卓)

/*
线性表基本操作:
InitList(&L) // 初始化操作,建立一个空的线性表L
DestroyList(&L) // 销毁已存在的线性表L
ClearList(&L) // 将线性表清空
ListInsert(&L, i, e) // 在线性表L中第i个位置插入新元素e
ListDelete(&L, i, &e) // 删除线性表L中第i个位置元素,用e返回
IsEmpty(&L) // 若线性表为空, 返回true, 否则false
ListLength(&l) // 返回线性表L的元素个数
LocateElem(&L, e) // L中查找与给定值e相等的原数,若成功,返回该元素在表中的序号,否则返回0
GetElem(&L, i, &e) //将线性表中L中的第i个位置元素返回给e
*/
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW - 2
#define MAXSZIE 100
typedef int Status;
typedef char ElemType;
typedef struct {
ElemType *elem;
int length;
} SqList;
Status InitList_Sq(SqList &L) {
L.elem = new ElemType[MAXSIZE];
if (!L.elem) exit(OVERFLOW);
L.length = 0;
return OK;
}
void DestroyList(SqList &L) {
if (L.elem) delete L.elem;
}
void ClearList(SqList &L) {
L.length = 0;
}
int GetLength(SqList &L) {
return (L.length);
}
int IsEmpty(SqList &L) {
if (L.length == 0) return 1;
return 0;
}
int GetElem(SqList &L, int i, ElemType &e) {
if (i < 1 ||| i > length) return ERROR;
e = L.elem[i - 1];
return OK;
}