本题要求实现顺序表的操作集。
函数接口定义:
List MakeEmpty();
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P );
其中List
结构定义如下:
typedef int Position;
typedef struct LNode *List;
struct LNode {
ElementType Data[MAXSIZE];
Position Last; /* 保存线性表中最后一个元素的位置 */
};
各个操作函数的定义为:
List MakeEmpty()
:创建并返回一个空的线性表;
Position Find( List L, ElementType X )
:返回线性表中X的位置。若找不到则返回ERROR;
bool Insert( List L, ElementType X, Position P )
:将X插入在位置P并返回true。若空间已满,则打印“FULL”并返回false;否则,如果参数P指向非法位置,则打印“ILLEGAL POSITION”并返回false;
bool Delete( List L, Position P )
:将位置P的元素删除并返回true。若参数P指向非法位置,则打印“POSITION P EMPTY”(其中P是参数值)并返回false。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 5
#define ERROR -1
typedef enum {false, true} bool;
typedef int ElementType;
typedef int Position;
typedef struct LNode *List;
struct LNode {
ElementType Data[MAXSIZE];
Position Last; /* 保存线性表中最后一个元素的位置 */
};
List MakeEmpty();
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P );
int main()
{
List L;
ElementType X;
Position P;
int N;
L = MakeEmpty();
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
if ( Insert(L, X, 0)==false )
printf(" Insertion Error: %d is not in.\n", X);
}
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
P = Find(L, X);
if ( P == ERROR )
printf("Finding Error: %d is not in.\n", X);
else
printf("%d is at position %d.\n", X, P);
}
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &P);
if ( Delete(L, P)==false )
printf(" Deletion Error.\n");
if ( Insert(L, 0, P)==false )
printf(" Insertion Error: 0 is not in.\n");
}
return 0;
}
/* 你的代码将被嵌在这里 */
输入样例:
6
1 2 3 4 5 6
3
6 5 1
2
-1 6
输出样例:
FULL Insertion Error: 6 is not in.
Finding Error: 6 is not in.
5 is at position 0.
1 is at position 4.
POSITION -1 EMPTY Deletion Error.
FULL Insertion Error: 0 is not in.
POSITION 6 EMPTY Deletion Error.
FULL Insertion Error: 0 is not in.
代码如下:
List MakeEmpty()//创建并返回一个空的线性表
{
List L=(struct LNode*)malloc(sizeof(struct LNode));
L->Last=-1;//1,,
return L;
}
Position Find( List L, ElementType X )//返回线性表中X的位置
{
for(Position i=0;i<=L->Last;i++)
{
if(L->Data[i]==X)
{
return i;
}
}
return ERROR;
}
bool Insert( List L, ElementType X, Position P )//将X插入在位置P并返回true
{
if(L->Last==MAXSIZE-1)
{
printf("FULL");
return false;
}else if(P<0||P>L->Last+1)//2,,
{
printf("ILLEGAL POSITION");
return false;
}
for(int j=L->Last;j>=P;j--)
{
L->Data[j+1]=L->Data[j];
}
L->Data[P]=X;
L->Last++;
return true;
}
bool Delete( List L, Position P )//将位置P的元素删除并返回true
{
if(P<0||P>L->Last)
{
printf("POSITION %d EMPTY",P);
return false;
}
for(int j=P;j<L->Last;j++)
{
L->Data[j-1]=L->Data[j];
}
L->Last--;
return true;
}
一些注释:
1,,将空表的Last
初始化为 -1 :
*标识空表:使用 -1 作为特殊值来明确表示线性表为空。当Last的值为 -1 时,很容易通过判断Last == -1来确定线性表中没有元素,这种方式简洁明了。
*利于空表的插入操作:插入操作时可以直接将新元素放在下标为 0 的位置,并Last更新为 0,这与数组下标的使用习惯相符合,使得线性表的操作在逻辑上更加自然和连贯。若后续要访问线性表中的元素,就可以直接通过Data[0]到Data[Last]来进行访问。
2,,在顺序表的插入操作中,通常判断插入位置合法性的条件是(P < 0 || P > L->Last + 1)
,而不是(P < 0 || P > L->Last)的
原因:
*考虑表头插入:当需要在表头插入元素时,即P = 0,如果条件是(P < 0 || P > L->Last),那么当线性表为空,即L->Last = -1时,P = 0会被判定为非法位置,这与实际需求不符,因为我们应该是可以在空表的表头(也就是位置 0)插入元素的。而(P < 0 || P > L->Last + 1)这个条件在空表时,L->Last + 1 = 0,此时P = 0是合法的,符合在表头插入元素的逻辑。
*考虑表尾后插入:在某些情况下,可能需要支持在表尾的下一个位置插入元素,即将新元素插入到当前最后一个元素的后面,使线性表长度增加。如果条件是(P < 0 || P > L->Last),那么P = L->Last + 1会被判定为非法位置,无法实现表尾后插入的操作。而(P < 0 || P > L->Last + 1)允许P取L->Last + 1,使得在表尾后插入元素成为可能,这在一些算法或应用场景中是很有用的,比如动态扩展线性表的内容。
*保持逻辑一致性:(P < 0 || P > L->Last + 1)这个条件能够更完整地覆盖所有可能的非法插入位置情况,包括表头之前、表尾之后以及中间的非法位置。它与线性表的逻辑结构和操作语义更加一致,能够确保插入操作在各种情况下都能正确判断位置的合法性,避免出现逻辑错误或意外的行为。