- // 创建并返回一个空的线性表;
- List MakeEmpty()
- {
- List L;
- L = (List)malloc(sizeof(struct LNode));
- L->Last = -1; // 因为插入一个时, Last++, 此时需为 - 1
- return L;
- }
- // 返回线性表中 X 的位置. 若找不到则返回 ERROR;
- Position Find(List L, ElementType X)
- {
- for (int i = 0; i <= L->Last; i++)
- {
- if (L->Data[i] == X)
- return i;
- }
- return ERROR;
- }
- // 将 X 插入在位置 P 并返回 true. 若空间已满, 则打印 "FULL" 并返回 false; 如果参数 P 指向非法位置, 则打印 "ILLEGAL POSITION" 并返回 false;
- bool Insert(List L, ElementType X, Position P)
- {
- // 表满
- if (L->Last == MAXSIZE - 1)
- {
- printf("FULL");
- return false;
- }
- // 非法位置
- if (P> L->Last + 1 || P <0)
- {
- printf("ILLEGAL POSITION");
- return false;
- }
- // 右移 P 后面的
- for (int i = L->Last + 1; i> P; --i)
- {
- L->Data[i] = L->Data[i - 1];
- }
- L->Data[P] = X;
- L->Last++;
- return true;
- }
- // 将位置 P 的元素删除并返回 true. 若参数 P 指向非法位置, 则打印 "POSITION P EMPTY"(其中 P 是参数值) 并返回 false.
- bool Delete(List L, Position P)
- {
- // 非法位置
- if (P> L->Last || P <0)
- {
- printf("POSITION %d EMPTY", P);
- return false;
- }
- for (int i = P; i < L->Last; i++)
- {
- L->Data[i] = L->Data[i + 1];
- }
- L->Last--;
- return true;
- }
[PTA] 数据结构与算法题目集 6-2 顺序表操作集
来源: http://www.bubuko.com/infodetail-2851167.html