[PTA] 数据结构与算法题目集 6-2 顺序表操作集

it2025-11-13  4

//创建并返回一个空的线性表; 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; }

转载于:https://www.cnblogs.com/ruoh3kou/p/9975688.html

最新回复(0)