// 创建并返回一个空的线性表
List MakeEmpty()
{
List L;
L=(List)malloc(sizeof(struct LNode));
L->Last=-1;
return L;
}
// 返回线性表中X的位置。若找不到则返回ERROR
Position Find( List L, ElementType X )
{
int i;
for (i=0;i<=L->Last;i++)
{
if (L->Data[i]==X)
break;
}
if (i<=L->Last) return i;
else 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;
}
int i;
for (i=L->Last;i>=P;i--)
{
L->Data[i+1]=L->Data[i];
}
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;
}
int i;
for (i=P+1;i<=L->Last;i++)
{
L->Data[i-1]=L->Data[i];
}
L->Last--;
return true;
}
FBI WARNING:
- 错误原因:
- 将P当成排序次序,而非数组中的序号
- Insert算法判断P非法的条件:if (P>L->Last+1 || P<0) //可以插在Last后面一个位置,第一次写成了if (P>L->Last || P<0)