数据结构线性表
建表
struct SeqList { int *elem; int length; };
初始化表
bool InitList(SeqList &S) { S.elem = new int[Maxsize]; if (!S.elem) return false; S.length = 0; return 0; }
向表中插入元素
bool ListInsert(SeqList &S, int e, int i) { if (S.length == Maxsize) return false; if (i <= 0 || i > S.length + 1) return false; for (int n = S.length - 1; n >= i - 1; n--) S.elem[n+1] = S.elem[n]; S.elem[i - 1] = e;
++S.length; return true; }
删除第i个元素
bool ListDelete(SeqList &S, int i) { if (i < 1 || i>S.length) return false; for (int n = i - 1; n < S.length - 1; n++) { S.elem[n] = S.elem[n + 1]; } S.elem[S.length] = 0; S.length--; return true; }